I have a String representing a date with format yyyy/mm/dd and want to calculate the number of days between that day and today. How is this done?
So It may be something like
String aDate = "1934/05/24"
解决方案
Way to do this -
Parse the date string to Date object using SimpleDateFormat.
Get the date difference between todays date and parsed date.
long diff = d2.getTime() - d1.getTime();//in milliseconds
Now convert it to days, hours, minutes and seconds.
1000 milliseconds = 1 second
60 seconds = 1 minute
60 minutes = 1 hour
24 hours = 1 day
Other way using joda date time
DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy/mm/dd");
DateTime dt1 = formatter.parseDateTime(aDate);
And
Duration duration = new Duration(dt1 , new DateTime());
System.out.println("Days "+duration.getStandardDays()+" Hours "+duration.getStandardHours()+" Minutes "+duration.getStandardMinutes());