Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java DateFormat

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

like image 706
Faheem Avatar asked Nov 28 '22 11:11

Faheem


2 Answers

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.

like image 58
Nikita Rybak Avatar answered Dec 05 '22 16:12

Nikita Rybak


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

like image 41
Julien Hoarau Avatar answered Dec 05 '22 16:12

Julien Hoarau