We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Java and datetime

nuetrino
nuetrino asked
on
Medium Priority
431 Views
Last Modified: 2012-08-14
Hi,

Having an unusual probem with dates on a windows 7 laptop. Say if i run the below code in eclipse (for java 1.4 and/or java 1.6) I get the following printed on the console "Fri Apr 29 11:12:34 BST 2011" which is correct as the time being shown on the pc is  "Fri Apr 29 11:12:34 BST 2011".

However when I run this as a standalone application (which runs inside a window service) on the same pc I get the following  "Fri Apr 29 10:12:34 BST 2011".

Any ideas on what im doing wrong?


Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT+00"));
Date date = cal.getTime();
System.out.println(date .toString());
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2016

Commented:
Using three-letter timezones is deprecated (although 'GMT' / 'UTC' should be OK)

Try
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));

Open in new window

Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
are you sure both are running on the same jre
you may need to run the tz updater

btw you need to use DateFormat to specify the tz

CERTIFIED EXPERT
Top Expert 2016

Commented:
Your server might also be already running in UTC- check at the command line
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
theres an example at the bottom of this post for specify the timezone. just change the format to meet your needs

http://helpdesk.objects.com.au/java/how-to-calculate-the-difference-between-two-dates-or-times
CERTIFIED EXPERT
Top Expert 2016

Commented:
What you must be aware of is that the date will print with the default timezone the way you're doing it, and not with the timezone you set explicitly

Author

Commented:
"Your server might also be already running in UTC- check at the command line "

How do i check this?  This sounds probable , would this explain why it works fine in eclipse & not within the service when i run it on my laptop
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
> would this explain why it works fine in eclipse & not within the service when i run it on my laptop

no

Author

Commented:
Interesting, think i found the problem. When i switch the JRE inside eclipse to use java 1.4_2 it shows the incorrect time  "Fri Apr 29 10:12:34 BST 2011".
Does the jdk1.4.2 handle date times differently than java 1.6??
CERTIFIED EXPERT
Top Expert 2016

Commented:
>>Does the jdk1.4.2 handle date times differently than java 1.6??

The short answer is yes - tz info is constantly changing
CERTIFIED EXPERT
Top Expert 2016

Commented:
It's also worth running the following in each jre:

System.out.println(System.getProperty("user.timezone"));

Open in new window

Author

Commented:
System.out.println(System.getProperty("user.timezone")); 

Open in new window


Shows im using GMT. Is there anyway way I can get the jdk1.4 to use the correct time?

By correct time i mean that the time being used is similar to what happens in java 1.6....ie if the localtime on the machine is "Fri Apr 29 10:12:34 BST 2011".
 I want to be able to show that time as oppose to the incorrect time of ""Fri Apr 29 10:12:34 BST 2011".

Author

Commented:
By correct time i mean that the time being used is similar to what happens in java 1.6....ie if the localtime on the machine is "Fri Apr 29 11:12:34 BST 2011". I want to be able to show that time as oppose to the incorrect time of "Fri Apr 29 10:12:34 BST 2011".
edited cause of typo..apologies

Author

Commented:
The timezone seems to be "Europe/London" when running it under java 1.6. Should making my code in in java1.4 use this timezone solve my problem? Or is the "Europe/London" timezone only availble since java 1.5 upwards
CERTIFIED EXPERT
Top Expert 2016

Commented:
The most predictable way is to set user.timezone as a vm parameter
CERTIFIED EXPERT
Top Expert 2016
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT
Top Expert 2016

Commented:
>>Should making my code in in java1.4 use this timezone solve my problem?

Probably - see my last

>>Or is the "Europe/London" timezone only availble since java 1.5 upward

Full country/city timezones are being used in favour of the old 3-letter ones

Author

Commented:
I can only use jdk 1.4 on my client machinces so can i use "Europe/London" for the timezone ?
CERTIFIED EXPERT
Top Expert 2016

Commented:
Give it a try - you should be OK

Author

Commented:
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("Europe/London"));
	Date date = cal.getTime();
	System.out.println(date .toString()); 

Open in new window


On the jdk.14 this still gives the wrong time, im i doing it wrong or the timezone not reconigised in this version of java. If so is there anything else i can do?
CERTIFIED EXPERT
Top Expert 2016

Commented:
You're makingthe mistake i warned against at  http:#35490288. If you want to do it in code, try
TimeZone.setDefault(TimeZone.getTimeZone("Europe/London"));
Calendar cal = Calendar.getInstance();
Date date = cal.getTime();
System.out.println(date .toString());

Open in new window

Author

Commented:
O.k. it works if i use "-Duser.timezone=Europe/London" VM parmater when the application is starting, this works fine for me.

Wondering though is there anyway i can set  the timezone through code as the below doesnt seem to work for me?

Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("Europe/London"));
	Date date = cal.getTime();
	System.out.println(date .toString());  

Open in new window

CERTIFIED EXPERT
Top Expert 2016

Commented:
>>Wondering though is there anyway i can set  the timezone through code as the below doesnt seem to work for me?

See my last comment
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
> Interesting, think i found the problem. When i switch the JRE inside eclipse to use java 1.4_2 it shows the incorrect time  "Fri Apr 29 10:12:34 BST 2011".
> Does the jdk1.4.2 handle date times differently than java 1.6??

I mentioned earlier that the problem may be that may be the problem :)
not sure why you didn't accept my comment as the answer

> Wondering though is there anyway i can set  the timezone through code as the below doesnt seem to work for me?

I also explained how to do that
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
> are you sure both are running on the same jre

> btw you need to use DateFormat to specify the tz

> theres an example at the bottom of this post for specify the timezone. just change the format to meet your needs

http://helpdesk.objects.com.au/java/how-to-calculate-the-difference-between-two-dates-or-times
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
split 35490200 35490223 35491125
CERTIFIED EXPERT
Top Expert 2016

Commented:
This seems to be becoming a habit. Precisely WHY has this question been reopened? And what has the OP to say about it?
CERTIFIED EXPERT
Top Expert 2016

Commented:
?
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
> java -Duser.timezone=Europe/London YourApp

that does not fix your problem, it just hides it
CERTIFIED EXPERT
Top Expert 2016

Commented:
>>that does not fix your problem, it just hides it

It does fix the problem, since the intent is to force the timezone for a given app
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
the two apps were already using the same timezone
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.