Parsing Date
See the parse(String, ParsePosition) method for more information on date parsing.
Parameters:
source - A String whose beginning should be parsed.
Returns:
A Date parsed from the string.
Throws: ParseException - if the beginning of the specified string cannot be parsed.
package com.javasunderesan; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class CompareDateTime { public static void main(String[] args) { SimpleDateFormat sdf = new SimpleDateFormat("dd/mm/yyyy"); Date start = null, end = null; String fromDate = null; String toDate = null; try { CompareDateTime compareDate = new CompareDateTime(); fromDate = "24/05/2016"; toDate = "25/05/2016"; start = sdf.parse(fromDate); end = sdf.parse(toDate); System.out.println("Days Between " + start + " and " + end + ":::" +compareDate.daysBetween(start, end)); } catch (ParseException e) { e.printStackTrace(); } } private Long daysBetween(Date d1, Date d2) { return (Long) ((d2.getTime() - d1.getTime()) / (1000 * 60 * 60 * 24)); } }
output: Days Between Sun Jan 24 00:05:00 IST 2016 and Mon Jan 25 00:05:00 IST 2016:::1
No comments :
Post a Comment