-1

From this question and its answers, I tried to convert string to date. But it seems to strange with me.

String test = "2015/01/01 11:56:00 ";
SimpleDateFormat df = new SimpleDateFormat("YYYY/MM/dd HH:mm:ss");      
System.out.println(df.parse(test));

It returns

Mon Dec 29 11:56:00 ICT 2014

I have tried with other days but the results is not in the rule (i.e. it have the same distance from the input string and the output date). I am curious. Can anyone explain this for me?

Community
  • 1
  • 1
GAVD
  • 1,977
  • 3
  • 22
  • 40

2 Answers2

3

First of all its yyyy not YYYY for year and try the below code to create a Date object from a String.

String test = "2015/01/01 11:56:00 ";
SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = df.parse(test);
System.out.println(date);
Aakash
  • 1,751
  • 1
  • 14
  • 21
1

The date format is case-sensitive and therefore the parsing is evaluated differently from what you expected.

Try this:

String test = "2015/01/01 11:56:00 ";
SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
System.out.println(df.parse(test));
Avihoo Mamka
  • 4,656
  • 3
  • 31
  • 44
  • `Date date = new Date("Nov 26 2015 00:00:00");` is superfluous. Dare to explain why `YYYY` doesn't work as expected? – m0skit0 Nov 26 '15 at 09:09
  • The pattern is wrong. For month it should be MM not mm. mm is for minutes. – Aakash Nov 26 '15 at 09:10