-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.

4
  • 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 Commented Sep 7, 2015 at 7:20
  • 1
    Possibly the same question Converting String to date using SimpleDateFormat. Commented Sep 7, 2015 at 7:23
  • it should be timezone issue,dateFormat.setTimeZone(TimeZone.getTimeZone("${inputyourtimezone}")); you can set timezone before date parse. Commented Sep 7, 2015 at 7: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. Commented Sep 13, 2015 at 5:05

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

That's solve year problem and return 'Sat Mar 22 01:00:00 GMT+04:30 2014' But I want time be 00:00:00 Not 01:00:00
@HosseinR What does System.out.println(dateFormat.format(GreDate));? I suspect that your timezone might be interfering with your results
@MadProgrammer It's return "2014/03/22 01:00:00"
@HosseinR Hmm, works fine for :P. What's your TimeZone
@MadProgrammer My timezone is +4:30 (Tehran)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.