Joda time find difference between Hours
I have a date String:
Thu, 15 Jan 2015, 9:56 AM
I convert it into a date variable:
Thu Jan 15 09:56:00 GMT+05:30 2015
using:
String pattern = "EEE, d MMM yyyy, hh:mm a";
try {
date = new SimpleDateFormat(pattern).parse(getPref("refresh", getApplicationContext()));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Now I have the following function and pass the date variable to this below function:
public static int getDiffHour(Date first) {
int hoursBetween = Hours.hoursBetween(new LocalDate(first), new LocalDate()).getHours();
return hoursBetween;
}
Which always returns 0. What is the possible cause?
像这样尝试,
int diff_hrs = getDiffHours(date,new Date());// pass your date object as startDate and pass current date as your endDate
public int getDiffHours(Date startDate, Date endDate){
Interval interval = new Interval(startDate.getTime(), endDate.getTime());
Period period = interval.toPeriod();
return period.getHours();
}
Try below code:-
public static void main(String[] args) {
String dateStart = "01/14/2012 09:29:58";
String dateStop = "01/15/2012 10:31:48";
//HH converts hour in 24 hours format (0-23), day calculation
SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
Date d1 = null;
Date d2 = null;
try {
d1 = format.parse(dateStart);
d2 = format.parse(dateStop);
//in milliseconds
long diff = d2.getTime() - d1.getTime();
long diffSeconds = diff / 1000 % 60;
long diffMinutes = diff / (60 * 1000) % 60;
long diffHours = diff / (60 * 60 * 1000) % 24;
long diffDays = diff / (24 * 60 * 60 * 1000);
System.out.print(diffDays + " days, ");
System.out.print(diffHours + " hours, ");
System.out.print(diffMinutes + " minutes, ");
System.out.print(diffSeconds + " seconds.");
} catch (Exception e) {
e.printStackTrace();
}
}
for more info see below link :-
http://www.mkyong.com/java/how-to-calculate-date-time-difference-in-java/
DateTimeUtils obj = new DateTimeUtils();
SimpleDateFormat simpleDateFormat =
new SimpleDateFormat("EEE, d MMM yyyy, hh:mm a");
try {
Date date1 = simpleDateFormat.parse("Thu Jan 15 09:56:00 GMT+05:30 2015");
Date date2 = simpleDateFormat.parse("Thu Jan 16 09:56:00 GMT+05:30 2015");
obj.printDifference(date1, date2);
} catch (ParseException e) {
e.printStackTrace();
}
}
//1 minute = 60 seconds
//1 hour = 60 x 60 = 3600
//1 day = 3600 x 24 = 86400
public void printDifference(Date startDate, Date endDate){
//milliseconds
long different = endDate.getTime() - startDate.getTime();
System.out.println("startDate : " + startDate);
System.out.println("endDate : "+ endDate);
System.out.println("different : " + different);
long secondsInMilli = 1000;
long minutesInMilli = secondsInMilli * 60;
long hoursInMilli = minutesInMilli * 60;
long daysInMilli = hoursInMilli * 24;
long elapsedDays = different / daysInMilli;
different = different % daysInMilli;
long elapsedHours = different / hoursInMilli;
different = different % hoursInMilli;
long elapsedMinutes = different / minutesInMilli;
different = different % minutesInMilli;
long elapsedSeconds = different / secondsInMilli;
System.out.printf(
"%d days, %d hours, %d minutes, %d seconds%n",
elapsedDays,
elapsedHours, elapsedMinutes, elapsedSeconds);
}
链接地址: http://www.djcxy.com/p/36672.html
上一篇: Java代码来计算日期范围中的夜晚(00:00:00)的数量
下一篇: 乔达时间发现小时之间的差异