Solved

How do I print the time adjusted for Summer Time?

Posted on 2009-07-08
4
379 Views
Last Modified: 2012-05-07
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
Comment
Question by:rjebb
  • 3
4 Comments
 
LVL 92

Expert Comment

by:objects
ID: 24809417
> sf.setTimeZone(TimeZone.getTimeZone("GMT"));

you tell it to use GMT, not local time
To use local time, just remove that line
0
 

Accepted Solution

by:
rjebb earned 0 total points
ID: 24811246
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
 
LVL 92

Expert Comment

by:objects
ID: 24811260
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
 
LVL 92

Expert Comment

by:objects
ID: 24811267
> 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
best (free) software to access postgres db (java) 1 32
base64 decode encode 12 119
GUI builder for Eclipse? 8 27
split string containing \r\n in Java 46 38
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now