?
Solved

Sun, jdk, 1.5.0_06, Timezone issue with SimpleDateFormat object

Posted on 2007-08-10
10
Medium Priority
?
1,674 Views
Last Modified: 2013-11-05
I am  running the following code in linux. I am using jdk1.5.0_06.

SimpleDateFormat sdf =  new SimpleDateFormat("yyyy-MM-dd z HH:mm");                              
sdf.setTimeZone(TimeZone.getTimeZone("US/Eastern"));
Date  date = sdf.parse("2007-08-10 EDT 12:30:00");
System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm").format(date));

The above code prints out  =>  2007-08-10 11:30


SimpleDateFormat sdf =  new SimpleDateFormat("yyyy-MM-dd z HH:mm");                              
sdf.setTimeZone(TimeZone.getTimeZone("US/Eastern"));
Date  date = sdf.parse("2007-08-10 EST 12:30:00");
System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm").format(date));

This code prints out  :  2007-08-10 12:30

I suppose the daylight saving is in effect so EDT should be okay.
Am I missing something. Could anyone help me understand this behaviour of the date and the SimpleDateFormat object.



0
Comment
Question by:rnicholus
  • 4
  • 4
9 Comments
 
LVL 5

Expert Comment

by:mkatmonkey
ID: 19672306
The root cause of this non-intuitive behavior is described in this bug report.
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4340168

As the bug reports suggests, you may be better off using joda's classes for handling time. See http://joda-time.sourceforge.net
0
 
LVL 92

Expert Comment

by:objects
ID: 19673679
that behaviour looks correct to me. you are parsing two different times (as they are in different timezone). The SDF timezone doesn't have any effect when parsing as the tz is expicitly specified in the time.
Then when you format it it uses the SDF tz.

0
 

Author Comment

by:rnicholus
ID: 19686834
Thanks objects.

SimpleDateFormat sdf =  new SimpleDateFormat("yyyy-MM-dd z HH:mm");                              

sdf.setTimeZone(TimeZone.getTimeZone("US/Eastern")); -------> did you mean setting the timezone here does not really help?

System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm").format(date)); ------> the time zone NEEDS to be set for this SimpleDateFormat object
here ?


Okay. I think  now I understand the concept.


But, I am now confused as I get different result on different OS.

lets take the the example

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd z HH:mm:ss");
Date  date = sdf.parse("2007-08-10 EDT 12:30:00");
SimpleDateFormat sd =  new SimpleDateFormat("yyyy-MM-dd HH:mm");            System.out.println(sd.format(date));      

In Windows it prints out ->  2007-08-10 12:30
In Linux it prints out -> 2007-08-10 11:30


0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
LVL 92

Expert Comment

by:objects
ID: 19688173
> -------> did you mean setting the timezone here does not really help?

I mean it is only taken into account when formatting (as it explicitly specified in the string when parsed)

> In Windows it prints out ->  2007-08-10 12:30
> In Linux it prints out -> 2007-08-10 11:30

What is the timezone set to on two OS'es
0
 

Author Comment

by:rnicholus
ID: 19696052
okay.

The default time zone is the Central timezone (CDT) on both the systems.



0
 
LVL 92

Accepted Solution

by:
objects earned 750 total points
ID: 19696248
is that java thats thinks its CDT or the OS?

maybe related to this bug

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6466476
0
 

Author Comment

by:rnicholus
ID: 19703879
I will check out the link.

Objects:

Could you please also confirm that sun does not  recommend using EST or EDT while instantiating  a TimeZone object. Instead recommends 'US/Eastern'

i.e TimeZone tz = TimeZone.getTimeZone("US/Eastern");

and not TimeZone tz = TimeZone.getTimeZone("EDT");
0
 

Author Comment

by:rnicholus
ID: 19708796
OS thinks it is CDT
also when I print the current time in the default time zone java uses "CDT" so I think java also thinks the timezone is CDT.
0
 
LVL 92

Expert Comment

by:objects
ID: 19713161
> Could you please also confirm that sun does not  recommend using EST or EDT while instantiating  a TimeZone object.

not sure sorry
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Question has a verified solution.

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

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
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 will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses
Course of the Month4 days, 18 hours left to enroll

601 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