6

I have a Date & time "20140508063630" in (yyyyMMddHHmmss) format. I want to convert this Time into EST time zone. How can I do it? If there is any API for this conversion please let me know. Thanks in advance.

Ashish Tiwari
  • 2,168
  • 4
  • 30
  • 54
  • could be the answer http://stackoverflow.com/questions/16313087/how-to-convert-date-and-time-to-est-format-in-android – Syed Waqas May 08 '14 at 05:55
  • 1
    possible duplicate of [Timezone conversion](http://stackoverflow.com/questions/6567923/timezone-conversion) – Oleg Estekhin May 08 '14 at 05:58
  • 1
    [There](http://stackoverflow.com/questions/1694885/timezones-in-java) [are](http://stackoverflow.com/questions/9429357/date-and-time-conversion-to-some-other-timezone-in-java) [dozens](http://stackoverflow.com/questions/7670355/convert-date-time-for-given-timezone-java) [of questions](http://stackoverflow.com/questions/6567923/timezone-conversion) AND answers about converting between time zones in general and even between specific time zones. – Oleg Estekhin May 08 '14 at 06:00
  • @OlegEstekhin So vote to close the question. – Basil Bourque May 08 '14 at 06:17
  • The first thing i did. – Oleg Estekhin May 08 '14 at 07:05
  • @OlegEstekhin No, you didn't apparently. The "close" count is zero (at this moment… I'm about to vote to close myself). You may have clicked the "flag" link rather than the "close" link. – Basil Bourque May 09 '14 at 06:00

3 Answers3

9
    try {
          DateFormat gmtFormat = new SimpleDateFormat();
          TimeZone estTime = TimeZone.getTimeZone("EST");
          gmtFormat.setTimeZone(estTime);
          SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
          sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
          System.out.println("EST Time: " + gmtFormat.format(sdf.parse("20140508063630")));
        } catch (ParseException e) {
          e.printStackTrace();
        }
Suren Raju
  • 3,012
  • 6
  • 26
  • 48
3

Since you have tagged your question java-time, you do deserve a java.time answer.

    String dateAndTime = "20140508063630";
    DateTimeFormatter parseFormatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
    ZonedDateTime coralHarbourDateTime = LocalDateTime.parse(dateAndTime, parseFormatter)
            .atOffset(ZoneOffset.UTC)
            .atZoneSameInstant(ZoneId.of("America/Coral_Harbour"));
    System.out.println(coralHarbourDateTime);

This prints

2014-05-08T01:36:30-05:00[America/Coral_Harbour]

There are a number of things to note though:

  • You also tagged your question android. java.time seems to be coming to Android, but as of writing it is not on the majority of Android phones. Don’ t despair, though, get the ThreeTenABP and use the classes on Android.
  • The SimpleDateFormat and TimeZone classes used in the other answers are long outdated, so if your Android app is doing any work with dates and/or times and/or time zones, I do recommend ThreeTenABP and the modern classes as the programmer friendly and the futureproof way to go.
  • You asked for EST time zone and then gave us a date of May 8, which is in the summer time (DST) part of the year. First, three letter abbreviations are dangerous in being ambiguous and in this case not a full time zone, so avoid them where you can. I solved the problem by using America/Coral_Harbour since I read that this particular place uses EST all year round, no summer time. If this was not what you intended, please provide a different location. If instead I use America/Montreal, I get 2014-05-08T02:36:30-04:00[America/Montreal], for example, with time zone offset -4 instead of -5.

Furhter link: How to use ThreeTenABP in Android Project

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
1

Following code for convert date from one timezone to another timezone with any date format

{   String parsedDate = null;

    try {
        SimpleDateFormat dbDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
    dbDateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
        Date date = dbUtcDateFormat.parse("20140508063630");

        SimpleDateFormat userDateFormat = new SimpleDateFormat(yyyyMMddHHmmss);
        userDateFormat.setTimeZone(TimeZone.getTimeZone("EST"));
        parsedDate = userDateFormat.format(date);
    } catch (Throwable t) {
        logger.warn("Date Parse Faied : ", t);
    }

    return parsedDate;
}
Sameer Kazi
  • 17,129
  • 2
  • 34
  • 46