-2

Update: Really?!!! Duplicate??? My format is correct (yyyy/MM/dd HH:mm:ss) but return time is incorrect. How this is similar to another question????

I'm trying to create java Date but it's always return wrong value. This is my code:

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:MM:SS");
Date GreDate = dateFormat.parse("2014/03/22 00:00:00");

And GreDate return Sun Dec 22 00:00:00 GMT+03:30 2013 as value. Please don't suggest to use external library for date type.

Update:

I changed my pattern to this:

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");

Now GreDate returns Sat Mar 22 01:00:00 GMT+04:30 2014. Year is correct but time still not 00:00:00.

peterh
  • 11,875
  • 18
  • 85
  • 108
Hossein Rashno
  • 3,073
  • 1
  • 25
  • 49
  • 3
    *"M | Month in year (context sensitive) | Month | July; Jul; 07"*; *"m | Minute in hour Number | 30"* ; *"S | Millisecond | Number | 978"*. Don't forget to have a read through [the JavaDocs](https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html) – MadProgrammer Sep 07 '15 at 07:20
  • 1
    Possibly the same question [Converting String to date using SimpleDateFormat.](http://stackoverflow.com/questions/9872419/converting-string-to-date-using-simpledateformat) – Naman Gala Sep 07 '15 at 07:23
  • it should be timezone issue,`dateFormat.setTimeZone(TimeZone.getTimeZone("${inputyourtimezone}"));` you can set timezone before date parse. – Kerwin Sep 07 '15 at 07:51
  • I also think your question has the answer there. If you don't agree, edit your question to make clear the difference, and explain it in comment. – peterh Sep 13 '15 at 05:05

1 Answers1

0

Note that:

  • MM are the months, mm are the minutes.
  • SS are the milliseconds, ss are the seconds.

So you need to change your dateFormat to

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");

Basically there are two errors in your pattern, both in the time part (seconds and minutes).

Here is the link to the complete documentation link.

peterh
  • 11,875
  • 18
  • 85
  • 108
Davide Lorenzo MARINO
  • 26,420
  • 4
  • 39
  • 56