Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unexpected difference in dates parsed using yyyy-MM-dd hh:mm:ss format

I've run the below java code to get time difference.

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;


public class Test 
{
    public static SimpleDateFormat simpleDateFormat= new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
    public static Date date1,date2;
    public static long diff; 
    public static String TAG ="DateConversion";
    public static Calendar cal1,cal2;

    public static void main(String a[])
    {
        checkTimeDifference("2013-10-30 10:15:00", "2013-10-30 11:15:00");
        checkTimeDifference("2013-10-30 10:15:00", "2013-10-30 12:15:00");
        checkTimeDifference("2013-10-30 10:15:00", "2013-10-30 13:15:00");
    }

    public static  void checkTimeDifference(String strDate,String checkDate)
    {
        try 
        {
            simpleDateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
            date1    = simpleDateFormat.parse(strDate);
            date2    = simpleDateFormat.parse(checkDate);

            //in milliseconds
            diff = date2.getTime() - date1.getTime();
            System.out.println("Difference : "+diff);
            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.println(diffDays     + " days, ");
            System.out.println(diffHours    + " hours, ");
            System.out.println(diffMinutes+ " minutes, ");
            System.out.println(diffSeconds+ " seconds.");
        }
        catch (Exception e) 
        {
            System.out.println(""+e);
        }
    }
}

the output of above program is.,

Difference : 3600000
0 days, 
1 hours, 
0 minutes, 
0 seconds.
Difference : -36000000
0 days, 
-10 hours, 
0 minutes, 
0 seconds.
Difference : 10800000
0 days, 
3 hours, 
0 minutes, 
0 seconds.

its return minus value when executing "checkTimeDifference("2013-10-30 10:15:00", "2013-10-30 12:15:00");"

why its return minus value and how to solve it?

like image 395
RVG Avatar asked Oct 30 '13 07:10

RVG


Video Answer


2 Answers

This is the problem:

new SimpleDateFormat("yyyy-MM-dd hh:mm:ss")

The hh here means "12 hour hour-of-day" so 12 means midnight unless there's something to indicate that it's meant to be 12 PM. Your value of 13 only works because the parser is in a lenient mode. You want:

new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")

I'd also strongly advise you to use Joda Time for this task anyway, as it makes it a lot simpler.

like image 52
Jon Skeet Avatar answered Oct 07 '22 15:10

Jon Skeet


Changing the SimpleDateFormat pattern to yyyy-MM-dd HH:mm:ss fixes the issues.

This happens because in the yyyy-MM-dd hh:mm:ss case, 12 is evaluated as 0.

like image 30
Dan D. Avatar answered Oct 07 '22 16:10

Dan D.