0

I have a SimpleDateFormat parser that parse in this way:

sdf = new java.text.SimpleDateFormat("yyyy-MM-DD HH:mm:ss z").parse("2013-10-25 17:35:14 EDT");

log.debug(sdf);

This give me Sat Jan 26 03:05:14 IST 2013

What am i missing here?

Dhinakar
  • 4,061
  • 6
  • 36
  • 68
Zeta
  • 534
  • 6
  • 17
  • 1
    Why the javascript tag? – MarsOne Oct 30 '13 at 09:57
  • 1
    It's giving the time in your computer. I think I don't understand the question. What did you expect to be the output? – luanjot Oct 30 '13 at 10:01
  • 1
    I'm pretty sure it's the `DD` there - see [SimpleDateFormat](http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html). It should be `dd`. – S.R.I Oct 30 '13 at 10:03
  • You're getting the `IST` time because `Date` has no details about the `TimeZone`. Its just the representation of Time since *Epoch*. Have a look at [this question](http://stackoverflow.com/q/7533290/2024761) and the [Jon Skeet answer](http://stackoverflow.com/a/7533330/2024761) for that. – Rahul Oct 30 '13 at 10:19

4 Answers4

2

First of all, DD stands for day in year, You should use dd instead.

Also, if you want to print a date in a specific format, you need to use two SimpleDateFormats.

SimpleDateFormat.parse returns a Date object represents the date you specified at the given format.

The Date object itself is saved as a regular Date, no format attached to it.

If you want to print it in a specific format, you need to use another SimpleDateFormat and call format method.

BobTheBuilder
  • 18,858
  • 6
  • 40
  • 61
1

you should use Format

SimpleDateFormat sdf1 =  new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:SS z");
String sdf = sdf1.format(sdf1.parse("2013-10-25 17:35:14 EDT"));
Dhinakar
  • 4,061
  • 6
  • 36
  • 68
1

There are two things.

sdf is an object of Date, which represents a specific instant in time (milliseconds elapsed since another instant known as "the epoch"). There is no format which is known to this object. And how this object is printed is solely handled by its class' toString method, which prints the date in this format:

dow mon dd hh:mm:ss zzz yyyy

This is exactly what you see in your output. Note that the timezone of the machine running the program is printed in this case. If you wish to print this object in a format of your choice you should use a DateFormat. To get output for a specific timezone you have to explicitly tell it to the DateFormat object (see below).

Another thing is you should be using dd instead of DD in the pattern. D is for day in year and d is for day in month, which I believe is what you want. Now keeping in mind both the points, this is one of the solutions to your problem:

DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
sdf.setTimeZone(TimeZone.getTimeZone("EDT")); // Time Zone for output
Date d = sdf.parse("2013-10-25 17:35:14 EDT");
System.out.println(sdf.format(d)); // You need format method

which outputs:

2013-10-25 17:35:14 EDT
1

What are the people answering not getting here is more the question:

2013-10-25 17:35:14 EDT != Sat Jan 26 03:05:14 IST 2013

I think it is because 'EDT' is the timezone and so, when it is 17:35 in EDT is is 3:05 in the UK, ignoring Daylight saving adjustments.

Petter Friberg
  • 21,252
  • 9
  • 60
  • 109
peter
  • 11
  • 1