문제

I want to get the eastern standard time /EST current time but it keeps on showing my current time... Any idea what I am doing wrong?

DateFormat format = new SimpleDateFormat("yyyy.MM.dd G 'at' HH:mm:ss z");
        TimeZone utc = TimeZone.getTimeZone("America/New_York");
        GregorianCalendar gc = new GregorianCalendar(utc);
        Date now = gc.getTime();
        String strDateString = format.format(now);
도움이 되었습니까?

해결책

The Date class is intended to reflect coordinated universal time (UTC), so it does not reflect timezone you use in GregorianCalendar. Also by default SimpleDateFormat uses system default timezone and if you want to override it you should do it explicitly:

DateFormat format = new SimpleDateFormat("yyyy.MM.dd G 'at' HH:mm:ss z");
TimeZone la = TimeZone.getTimeZone("America/Los_Angeles");
format.setTimeZone(la);

Another option is to switch to Joda Time library:

DateTimeZone la = DateTimeZone.forTimeZone(TimeZone.getTimeZone("America/Los_Angeles"));
DateTimeFormat.forPattern(dateTimeFormat).withZone(la).print(System.currentTimeMillis());
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top