Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculate Date/Time Difference in Java considering AM/PM

I want to calculate the difference between two date/time in java using Date and Calendar classes. The format that I have is "2012-01-24 12:30:00 PM".

I have implemented my own method and also google it to work with others, but haven't getting the right hint to handle AM and PM values.

The date/time difference got troubled whenever the time have 12(AM/PM) in it. For example if I have date/time "2012-01-24 12:30:00 PM" and "2012-01-24 02:30:00 PM" it shows the difference is 10 hours.

Considering the code on this link how can it be modified to handle AM and PM.

To convert String date into Date I use following code:

    String sessionTimeStart = "2012-01-24 12:30:00 PM";
    String sessionTimerEndOrCurrent = "2012-01-24 02:30:00 PM";
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss a"); 

    Date d1 = null;
    Date d0 = null; 
    try {
        d1 = format.parse(sessionTimeStart);
        d0 = format.parse(sessionTimerEndOrCurrent);
    } catch (ParseException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
like image 499
rizzz86 Avatar asked Jan 25 '12 10:01

rizzz86


3 Answers

The problem is your date format: instead of yyyy-MM-dd HH:mm:ss a use yyyy-MM-dd hh:mm:ss a.

HH will be the hour in day (0-23) whereas hh will be the hour in AM/PM (1-12). Thus with your date format 02:30:00 will be parsed as just that instead of being converted to the PM version (which in hour of day would be 14:30:00).

like image 117
Thomas Avatar answered Nov 05 '22 07:11

Thomas


The reason why it shows 10 hours as the difference is that you've got an error in the pattern when parsing the input.

Here's an example using SimpleDateFormat:

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss a");

Date date1 = df.parse("2012-01-24 12:30:00 PM");
Date date2 = df.parse("2012-01-24 02:30:00 PM");

long differenceInHours = Math.abs(date1.getTime() - date2.getTime()) / 1000 / 60 / 60);

Will return 10.

When we just slightly change the date format pattern, using hh for hour in am/pm (1-12) instead of HH for hour in day (0-23):

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss a");

It returns (the expected) 2.

See the documentation for SimpleDateFormat to get your patterns right.

like image 44
hleinone Avatar answered Nov 05 '22 07:11

hleinone


So, you have these dates as strings? Parse them with a SimpleDateFormat with the appropriate format string, and compute the difference in hours:

DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss a");

Date d1 = df.parse("2012-01-24 12:30:00 PM");
Date d2 = df.parse("2012-01-24 02:30:00 PM");

int hoursDifference = (int)((d2.getTime() - d1.getTime()) / 3600000L);
System.out.println("Difference in hours: " + hoursDifference);

Your error is that you are using HH (24-hour hours) instead of hh (12-hour hours) in your format string.

like image 30
Jesper Avatar answered Nov 05 '22 08:11

Jesper