Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 386
  • Last Modified:

How do I print the time adjusted for Summer Time?

After reading many forum posts and web pages on the correct way to display local time adjusted for daylight savings (summer time) here in the UK, I came up with the code below. This displays raw GMT however with no adjustment.

I am running Java 1.5.0_06 in Netbeans 6.5 on a PC running Windows XP with the timezone set for GMT and "automatically adjust for daylight saving changes" ticked. Is there anything else I need to define for the Java environment or is my code just wrong? If I tear any more hair out I'll soon be bald :-(
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss zzzz");
sf.setTimeZone(TimeZone.getTimeZone("GMT"));
String lvTimestamp = df.format(new Date());
System.out.println(lvTimestamp);

Open in new window

0
Richard Jebb
Asked:
Richard Jebb
  • 3
1 Solution
 
objectsCommented:
> sf.setTimeZone(TimeZone.getTimeZone("GMT"));

you tell it to use GMT, not local time
To use local time, just remove that line
0
 
Richard JebbAuthor Commented:
If you look at my details below the original post you will see that my local time zone is currently British Summer Time (ie GMT + 1.0). According to the information I have read online, the Java timezone class is supposed to understand the dates for daylight saving for the time zones it supports, and so I am trying to set whatever is the correct  time zone ID for the UK, so that it picks up the fact that we are currently on summer time and so actually GMT+1 rather than GMT+0.

I did what you suggested but it made no difference. However it did make me  think that perhaps "GMT" was not the correct time zone to use, and was simply telling the system to always use GMT with no daylight saving applied.

I printed out the list of known time zone identifiers to see what they were, and if I use

df.setTimeZone(TimeZone.getTimeZone("Europe/London")

then it does correctly figure out the daylight savings
0
 
objectsCommented:
yes , am pretty sure the GMT timezones do not take daylightsaving into account (as they do not indicate the country), and are just straight GMT offsets
0
 
objectsCommented:
> I did what you suggested but it made no difference.

that would be because your pc's timezone is not setup correctly
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now