Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wonky dating in Java

The following Java code just parses a date (with time portion) 2009-01-28-09:11:12 using SimpleDateFormat. Let's have look at it.

final public class Main
{
    public static void main(String[] args)
    {            
        try
        {
            DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
            Date d = df.parse("2009-01-28-09:11:12");
            System.out.println(d);
        }
        catch (ParseException ex)
        {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

The date (with time) displayed (after parsing) by the above code is as follows,

Sun Nov 30 22:07:51 IST 2008

even though we are attempting to parse the date 2009-01-28-09:11:12. It looks somewhat wonky. Why does it parse so?

like image 383
Lion Avatar asked Mar 05 '26 05:03

Lion


1 Answers

Shouldn't your date format be something like this:

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

to match this format:

Date d = df.parse("2009-01-28-09:11:12");

?

As for why, per this:

  • http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html

the parser actually looks at these as numbers, and the trick is that - is a part of a number, representing negative numbers. So if you do:

df.parse("2009-01-02-00:00:00")

it gives:

Mon Dec 01 00:02:00 EST 2008

That parses 2009 as yyyy, then -0 as MM (which is previous month as months start from 1), then 1 as dd, etc.

As per parse in DateFormat:

  • http://docs.oracle.com/javase/6/docs/api/java/text/DateFormat.html#parse(java.lang.String)

By default, parsing is lenient: If the input is not in the form used by this object's format method but can still be parsed as a date, then the parse succeeds. Clients may insist on strict adherence to the format by calling setLenient(false).

I guess, if you have an option, it would be better to use slashes instead of dashes, if you like formats like 2009/01/02 12:34:56. This:

df.parse("2009/01/02-00:00:00")

will throw an exception:

ERROR java.text.ParseException:
Unparseable date: "2009/01/02-00:00:00"

I can only conclude it's a very good thing that / is not considered a number division by DateFormat...

like image 194
icyrock.com Avatar answered Mar 07 '26 18:03

icyrock.com



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!