0

I have a piece of code wriiten in java that gives me the correct time using EST time zone as below:

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

public class DateProgram {

    public static void main(String[] args) {
        TimeZone.setDefault(TimeZone.getTimeZone("EST"));
        String dateTime = new SimpleDateFormat("yyyyMMdd-HHmmss").format(new Date());
        System.out.println(dateTime);

    }
}

Here i wanted to add the code for Daylight Saving Time with using EST time zone , my output should give me correct time considering EST time zone and daylight savings also.

Any help Appreciated, Thanks in advance

Nyki
  • 97
  • 1
  • 9
  • @ZakiAnwarHamdani i read that question and its solutions , it doesn't answer my question asked here – Nyki Aug 30 '17 at 06:34
  • 1
    Considering day light saving in time zone. Thats what is asked and answered in the other question. How yours differ from that question? – Zaki Anwar Hamdani Aug 30 '17 at 06:48
  • As an aside, avoid the long outdated classes `Date` and friends if you can. You can. In particular, avoid `TimeZone.setDefault()`, it may break functionality not only in your own program, but in other programs running in the same JVM. I recommend `LocalDateTime.now(ZoneId.of("America/New_York")) .format(DateTimeFormatter.ofPattern("yyyyMMdd-HHmmss"))`. – Ole V.V. Aug 30 '17 at 07:02
  • 2
    Just to stress what Andy Turner already said in [his answer](https://stackoverflow.com/a/45953478/5772882): Avoid three and four letter time zone abbreviations whereever you can. They are ambiguous and not standardized. For example, according to [Time Zone Abbreviations – Worldwide List](https://www.timeanddate.com/time/zones/) EST may also mean Australian Eastern Standard Time (UTC+10). – Ole V.V. Aug 30 '17 at 07:33

2 Answers2

3

java.time

Use the modern java.time classes that supplant the troublesome old legacy date-time classes.

Avoid the 3-4 letter codes such as EST. These are not actual time zones, not standardized, and are not even unique! Use real time zone names in the form of continent/region.

ZoneId z = ZoneId.of( "America/Montreal" ) ;
ZonedDateTime zdt = ZonedDateTime.now( z ) ;

ISO 8601

I suggest you consider using the standard ISO 8601 formats when generating strings rather than your peculiar format. The standard formats are used by default in the java.time classes when generating/parsing strings.

But if you insist…

DateTimeFormatter f = DateTimeFormatter.ofPattern( "uuuuMMdd-HHmmss" ) ;
String output = zdt.format( f ) ;
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
1

Confusingly, TimeZone.getTimeZone("EST") doesn't observe daylight savings time in the same way that PST does. (The same is true of HST and MST).

Use TimeZone.getTimeZone("America/New_York") instead.

Generally, avoid using three-letter time zone abbreviations, as it states in the Javadoc.

Andy Turner
  • 137,514
  • 11
  • 162
  • 243
  • 1
    Right idea, but using the troublesome old date-time classes that are now legacy, supplanted by the java.time classes. – Basil Bourque Aug 30 '17 at 07:09