1

I have following piece of code, which is just calculating time in milliseconds for their respective date objects

     public static void main(String... args) throws ParseException
    {
        SimpleDateFormat APIformatter = new SimpleDateFormat("MM-dd-yyyy HH:MM");
         Calendar calendar = Calendar.getInstance();
         Date date = APIformatter.parse("01-23-2017 17:58:23");
         calendar.setTime(date);

         System.out.println(calendar.getTimeInMillis());

         Calendar calendar2 = Calendar.getInstance();
         Date date2 = APIformatter.parse("01-24-2017 11:55:54");
         calendar2.setTime(date2);

         System.out.println(calendar2.getTimeInMillis());


    }

clearly date2 is recent than date object, hence I expect that the value returned by getTimeInMillis() will be greater for date2 as compared to date1. However that is not the case

System.out.println(calendar.getTimeInMillis()); returning me 1634988600000

and

System.out.println(calendar2.getTimeInMillis());  returning me 1627104600000

clearly date > date2 in returned values. Am I missing something here?

nobalG
  • 4,544
  • 3
  • 34
  • 72
  • 8
    Did you notice that you provided a capital M for both *months* and *minutes* in your date format? `MM-dd-yyyy HH:MM`. Change it to `MM-dd-yyyy HH:mm` – Erwin Bolwidt Jan 24 '17 at 07:28
  • @ErwinBolwidt This is it.... – nobalG Jan 24 '17 at 07:33
  • @ErwinBolwidt please post this as answer, so we can upvote – Krzysztof Cichocki Jan 24 '17 at 07:34
  • You should use slashes(`MM/dd/yyyy`) instead of dashes (`MM-dd-yyyy`). I think it's more common. Most European countries use dashes, but with the day of the month preceding. PHP, for instance, will interpret dashes as of a European date, see [this post](http://stackoverflow.com/a/2891949/507738). – MC Emperor Jan 24 '17 at 07:43
  • Hi @nobalG I know what you are expecting but here the problem is when you are trying to parse your datetime then after parsing it returns a numeric value and clearly what we can see the 2 objects "date" and "date2" has values 23 and 24 respectively that means that "date2" value is one day after "date" value hence value of date > date2. – Ashraf.Shk786 Jan 24 '17 at 07:53
  • @KrzysztofCichocki Missed your comment. The duplicate is fine though. – Erwin Bolwidt Jan 24 '17 at 08:01

0 Answers0