Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

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

Java and datetime

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());
0
nuetrino
Asked:
nuetrino
  • 14
  • 8
  • 8
1 Solution
 
CEHJCommented:
Using three-letter timezones is deprecated (although 'GMT' / 'UTC' should be OK)

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

Open in new window

0
 
objectsCommented:
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

0
 
CEHJCommented:
Your server might also be already running in UTC- check at the command line
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
objectsCommented:
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
0
 
CEHJCommented:
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
0
 
nuetrinoAuthor 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
0
 
objectsCommented:
> would this explain why it works fine in eclipse & not within the service when i run it on my laptop

no
0
 
nuetrinoAuthor 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??
0
 
CEHJCommented:
>>Does the jdk1.4.2 handle date times differently than java 1.6??

The short answer is yes - tz info is constantly changing
0
 
CEHJCommented:
It's also worth running the following in each jre:

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

Open in new window

0
 
nuetrinoAuthor 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".
0
 
nuetrinoAuthor 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
0
 
nuetrinoAuthor 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
0
 
CEHJCommented:
The most predictable way is to set user.timezone as a vm parameter
0
 
CEHJCommented:
e.g.
java -Duser.timezone=Europe/London YourApp

Open in new window

0
 
CEHJCommented:
>>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
0
 
nuetrinoAuthor Commented:
I can only use jdk 1.4 on my client machinces so can i use "Europe/London" for the timezone ?
0
 
CEHJCommented:
Give it a try - you should be OK
0
 
nuetrinoAuthor 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?
0
 
CEHJCommented:
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

0
 
nuetrinoAuthor 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

0
 
CEHJCommented:
>>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
0
 
objectsCommented:
> 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
0
 
objectsCommented:
> 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
0
 
objectsCommented:
split 35490200 35490223 35491125
0
 
CEHJCommented:
This seems to be becoming a habit. Precisely WHY has this question been reopened? And what has the OP to say about it?
0
 
CEHJCommented:
?
0
 
objectsCommented:
> java -Duser.timezone=Europe/London YourApp

that does not fix your problem, it just hides it
0
 
CEHJCommented:
>>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
0
 
objectsCommented:
the two apps were already using the same timezone
0

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

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