Calculate date / time difference in Java based on AM / PM

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

I applied my own method, as well as google, so that it works with others, but doesn’t get the right hint of handling AM and PM values.

The difference between date and time was confused whenever the time is 12 (AM / PM). For example, if I have a date / time of “2012-01-24 12:30:00 PM” and “2012-01-24 02:30:00 PM”, this shows that the difference is 10 hours.

Given the code for this link , how can it be changed to handle AM ​​and PM.

To convert a String date to Date, I use the 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(); } 
+7
source share
6 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 an hour per day (0-23), while HH will be an hour in AM / PM (1-12). Thus, with your date format 02:30:00, it will be analyzed just what instead of converting to the PM version (which will be 14:30:00 at one o'clock in the afternoon).

+6
source

The reason this shows 10 hours is because the difference is that you got an error in the template while analyzing the input.

Here is 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 .

If we slightly change the date format pattern using hh for the hour at am / pm (1-12) instead of hh for the hour per day (0-23):

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

It returns (expected) 2 .

See the documentation for SimpleDateFormat for the correct templates.

+4
source

So, do you have these dates as strings? Separate them using SimpleDateFormat with the appropriate format string and calculate 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 mistake is that you use HH (24 hours in hours) instead of hh (12 hours) in the format string.

+3
source

Do not create it yourself, use the installed library. Jodatime is widely used.

+1
source

onsider using the Apache Commons DateUtils DurationFormatUtils formatPeriod method will do the magic.

Good luck

EDIT Assuming that at this point you initialized date a and date b,

 String format="HH:mm:ss.SSS"; // whatever you wish boolean padWithZeros=true; // whatever you wish TimeZone timezone=null; // whatever you wish long timeA = a.getTime(); long timeB = b.getTime(); String period = DurationFormatUtils.formatPeriod(timeB, timeA, format, padWithZeros, timezone); 
+1
source

There is an add () method on the calendar, which can also be used for subtraction. Take a look at this. You should use Calendar instead of Date anyway, because most Date operations are deprecated.

-one
source

All Articles