0

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:

  1. The time zone conversion to Eastern time zone. This isn't working.

  2. Need it not to be in military time.

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

PacificNW_Lover
  • 4,746
  • 31
  • 90
  • 144
  • What's the difference between `mm` and `mm`? – shmosel Nov 08 '18 at 01:03
  • 1
    I notice your String uses `-` between fields. But your format uses `/`. I suggest you fix that. – Elliott Frisch Nov 08 '18 at 01:05
  • @shmosel - how is this duplicate? I conducted numerous searchs on Stack Overflow before resorting to creating this post. – PacificNW_Lover Nov 08 '18 at 01:07
  • You're making the same mistake. How is it not a duplicate? – shmosel Nov 08 '18 at 01:08
  • @ElliottFrisch that worked! But the day is coming in as Sun instead of Sunday... – PacificNW_Lover Nov 08 '18 at 01:12
  • 1
    Change EEE to EEEE. – Elliott Frisch Nov 08 '18 at 01:14
  • @ElliottFrisch - got it working exactly how I wanted it. I edited the post to share my solution. – PacificNW_Lover Nov 08 '18 at 01:30
  • 2
    As an aside consider throwing away the long outmoded and notoriously troublesome `SimpleDateFormat` and friends, and adding [ThreeTen Backport](https://www.threeten.org/threetenbp/) to your project in order to use `java.time`, the modern Java date and time API. It is so much nicer to work with. Not least when it comes to conversion between timezones. – Ole V.V. Nov 08 '18 at 04:56
  • 1
    While I disagree that this question is a very exact duplicate of the linked one, each of your issues has certainly been asked about more than once before. So your search engine should be very useful. – Ole V.V. Nov 08 '18 at 04:59
  • Are you sure you want 05:42 PM? I believe that 14:42 UTC is the same as 09:42 AM in North American Eastern Standard Time (America/New_York time zone). – Ole V.V. Nov 08 '18 at 05:08
  • `LocalDateTime.parse("2018-11-07 14:42:39", DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss")).atOffset(ZoneOffset.UTC).atZoneSameInstant(ZoneId.of("America/New_York")).format(DateTimeFormatter.ofPattern("EEEE, MMM dd, uuuu hh:mm a", Locale.US))` gives `Wednesday, Nov 07, 2018 09:42 AM`. Please break up the code properly yourself. And make sure to add ThreeTen Backport and import `org.threeten.bp.*` and `org.threeten.bp.format.*`. – Ole V.V. Nov 08 '18 at 05:11

0 Answers0