views:

67

answers:

3

Hi there, I know Java Date Time is not a good way to go forward but I was just curious as to what's happening:

Why does the following line:

DateFormat df = new SimpleDateFormat("dd-MMM-yyyy", Locale.US)

not produce any errors and the following lines do:

DateFormat df = new SimpleDateFormat("DD-MMM-YYYY", Locale.US)

DateFormat df = new SimpleDateFormat("dd-mm-YYYY", Locale.US)

The following exception gets thrown:

Exception in thread "main" java.lang.IllegalArgumentException: Illegal pattern character 'Y'

    at java.text.SimpleDateFormat.compile(SimpleDateFormat.java:769)
    at java.text.SimpleDateFormat.initialize(SimpleDateFormat.java:576)
    at java.text.SimpleDateFormat.<init>(SimpleDateFormat.java:501)
    at testing.MySchedule.main(MySchedule.java:18)

I mean I'm just changing the case right? but is DateFormat really that dumb or am I doing something wrong? or does it have something to do with the Locale I'm using?

Cheers

+3  A: 

m and D have their own meaning in SimpleDateFormat pattern:
http://download.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html

m   Minute in hour
D   Day in year

But you won't find Y in that table.

Nikita Rybak
+2  A: 

It's not "dumb", it's just an invalid pattern. Have a look at the API: http://download.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html

cherouvim
http://download-llnw.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html#year
org.life.java
In particular "unquoted letters from 'A' to 'Z' and from 'a' to 'z' are interpreted as pattern letters". "Y" isn't an allowed pattern.
Ben Lings
+1  A: 

You aren't changing only the case, you are changing the meaning of the format :

  • Y doesn't exist.
  • M stands for Month in year
  • m stands for Minute in hour
  • D stands for Day in year
  • d stands for Day in month

DD-MMM-YYYY and dd-mm-YYYY formats have no meaning.

More info on SimpleDateFormat

madgnome