Am using Java 1.7
Need to convert a date string from this:
2018-11-07 14:42:39 (which is in UTC timezone)
to
Wednesday, Nov 07, 2018 05:42 PM (which is in EST timezone)
Here's my code:
public class DateUtils {
public static String customizeDateString(String dateStr) throws ParseException {
TimeZone est = TimeZone.getTimeZone("America/New_York");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", java.util.Locale.ENGLISH);
sdf.setTimeZone(est);
Date myDate = sdf.parse(dateStr);
sdf.applyPattern("EEE, MMM d yyyy HH:mm:ss");
String customizedDateString = sdf.format(myDate);
return customizedDateString;
}
public static void main(String[] args) throws ParseException {
String dateString = customizeDateString("2018-11-07 14:42:39");
System.out.println(dateString);
}
}
When I run it I get this:
Wednesday, Nov 7 2018 14:42:39
What am I possibly doing wrong?
My requirements:
The time zone conversion to Eastern time zone. This isn't working.
Need it not to be in military time.
Need AM / PM at the end.
Got it working like this:
public static String customizeDateString(String dateStr) throws ParseException {
TimeZone est = TimeZone.getTimeZone("America/New_York");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", java.util.Locale.ENGLISH);
Date myDate = sdf.parse(dateStr);
sdf.setTimeZone(est);
sdf.applyPattern("EEEE, MMM d yyyy hh:mm:ss a");
String customizedDateString = sdf.format(myDate);
return customizedDateString;
}
Now it shows:
Wednesday, Nov 7 2018 05:42:39 PM