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?

4
  • 1
    Why the javascript tag? Commented Oct 30, 2013 at 9: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? Commented Oct 30, 2013 at 10:01
  • 1
    I'm pretty sure it's the DD there - see SimpleDateFormat. It should be dd. Commented Oct 30, 2013 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 and the Jon Skeet answer for that. Commented Oct 30, 2013 at 10:19

4 Answers 4

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.

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

Comments

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"));

Comments

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

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.