1

Please find my below Code what is does is it takes the TimeStamp Value of any timeZone Converts it to required TimeZone and gives you the Date of requiredTime Zone . It works the date variable is coming correctly but I need the value in (Date) Datatype so am parsing it back using the same SimpleDataFormat object but its returning me a value in different Format not the one mentioned in the SimpleDataFormat Object .

private Date getDateOfTimeZone(Timestamp timeStamp, String timeZoneCode)
        throws ParseException {
    SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("dd-MM-yy");
    DATE_FORMAT.setTimeZone(TimeZone.getTimeZone(timeZoneCode));
    String date = DATE_FORMAT.format(timeStamp);
    return DATE_FORMAT.parse(date);
}

Input Varibles : TimeStamp : 2013-11-01 16:19:37.0 , TimeZone : "IST"
Date value is coming as  : 02-11-13 (Correct)
But Parse() is returning me  : Fri Nov 01 14:30:00 EDT 2013.

I can see the date is converted according to timeZone but why parse is not returning it in required format i.e "dd-MM-yy".

1
  • Just print "date". Don't parse it again. Commented Nov 1, 2013 at 20:28

1 Answer 1

5

The parse method returns a Date.

When you print the Date, you get the output from the toString() method which is what you see (Fri Nov 01 14:30:00 EDT 2013)

To print it in the format you would like, you can use the format method to convert it into a String and then print it.

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

4 Comments

Ya I got it but I need to formatted String in (Date) Data Type
@Suraj sorry but; what? A Date is a data type; a format is a String. What you have just written is nonsensical. If you want a String then use a String.
@Suraj Date is container for the number of milliseconds since the Unix epoch, it by itself, has no format, it defaults to local format when required. You can use a DateFormatter to produce different format Strings of a Date, but they are not a Date
@MadProgrammer Oh Okay I got it .. I was thinking in a completely wrong way got it. Thanks for clarification.

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.