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) {
rizzz86
source share