Hero
Hero

Reputation: 647

SimpleDateFormat Parse not returning Date in required Format

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

Upvotes: 1

Views: 865

Answers (1)

Kal
Kal

Reputation: 24910

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.

Upvotes: 5

Related Questions