1

My program converts UTC time to localtime but not in the format that I want. I took an example from the following link Convert UTC to current locale time

 public static void main(String[] args) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
        Date myDate = simpleDateFormat.parse("2015-08-19 05:30:00.049 UTC+0000");
        System.out.println("**********myDate:" + myDate);
    }

Output:

**********myDate:Wed Aug 19 01:30:00 EDT 2015

My expected output format is:

2015-08-19 01:00:14

Please advise.

Community
  • 1
  • 1
Nital
  • 5,784
  • 26
  • 103
  • 195
  • 1
    I recommend you to look for Joda Time Library... – melli-182 Aug 19 '15 at 20:55
  • Read the [documentation](http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html). If you have Java 8 switch to `java.time`, but you'll still need to write your own formatter. – RealSkeptic Aug 19 '15 at 20:55
  • Why do you expect a time of "01:00:14"? If you want EDT time, I would expect you want "01:30:00", to match the unformatted output. – rgettman Aug 19 '15 at 21:04

1 Answers1

6

You parsed the text to a date successfully:

Date myDate = simpleDateFormat.parse("2015-08-19 05:30:00.049 UTC+0000");

However, you then proceeded to print myDate.toString().

System.out.println("**********myDate:" + myDate);

You won't get your expected format that way. Use (another) SimpleDateFormat to format myDate the way you want

final SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm")
outputFormat.setTimeZone(TimeZone.getTimeZone("EDT"));
System.out.println("**********myDate:" + ouputFormat.format(myDate));
dsh
  • 12,037
  • 3
  • 33
  • 51