Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Java code to conevrt 1 timezone to another

Posted on 2006-04-20
12
Medium Priority
?
278 Views
Last Modified: 2010-03-31
guys,

can someone post any java code/help to converting one timezone to another.

I want to develop a small utility where user can select timezone and time (hour:minut:ss:AM) and then when he submits the page, timestamp should be converted to US EST and saved in database. Later on when same info is retrieved (from database in EST) should be dispalyed in USER'S DEFAULT TIME ZONE (PC'S SET TIMEZONE) on fronend.

Also want to know if "TimeZone.getDefault()" is the correct way to know the user's default timezone.

Need to include the daylight saving time also.

any help will be greatly appreciated.

Thanks,
Ajay.

   

 
0
Comment
Question by:jnajayjain
  • 5
  • 3
  • 3
  • +1
12 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 16499230
Date is actually quite free of TimeZone information. All you need to do is to save the user's TimeZone then apply it later to the date formatting
0
 
LVL 1

Expert Comment

by:geekprog
ID: 16501296
TimeStamp ts =  new TimeStamp("hour:minut:ss:AM");

and now converting it into US EST depends what time zone you entered

look at the TimeStamp API and you can modify the hours accordingly

then you store in the database

----

I think you want to display the same time that was stored in the database ( not the current time )
In that case, add or remove the hours according to the Timezone
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16501554
java.sql.Timestamp is also free of TimeZone information and should be formatted according to the user's time zone
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 92

Expert Comment

by:objects
ID: 16503117
Best would be to store the date in UTC, let me know what database you are using and will let you know what you need.
You can then display it in any timezone once retrieved.

> TimeZone.getDefault()

you can set use the default timezone, another option is to set the timezone of the SimpleDateFormat used to display the date once retrieved.
0
 

Author Comment

by:jnajayjain
ID: 16508379
tried with TimeStamp ts =  new TimeStamp("hour:minut:ss:AM");
got compilation errors.

Myn databas is Oracle 9i.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16508415
You have a typoe This is the only ctor that's usable:

http://java.sun.com/j2se/1.5.0/docs/api/java/sql/Timestamp.html#Timestamp(long)
0
 
LVL 92

Expert Comment

by:objects
ID: 16512188
following covers how to handle timezones with oracle

http://www.dbasupport.com/oracle/ora9i/TimeZone.shtml
0
 

Author Comment

by:jnajayjain
ID: 16523236
Still did not get any clue about how using JAVA I can convert timespamp Say(Asia/Calcutta IST) to EST and vise versa.
0
 

Author Comment

by:jnajayjain
ID: 16523239
Can smone post any exapmle code for this.?
0
 
LVL 92

Accepted Solution

by:
objects earned 900 total points
ID: 16523264
Timestamp class does not have a timezone associated with it. Only a Calendar instance does and it has a method to set the timezone

http://java.sun.com/j2se/1.5.0/docs/api/java/util/Calendar.html#setTimeZone(java.util.TimeZone)

http://javaalmanac.com/egs/java.util/GetTimeOtherZone.html
http://javaalmanac.com/egs/java.util/GetTimeOtherZone2.html

Or you can set the timezone of the formatter used to format the toimezone

http://java.sun.com/j2se/1.5.0/docs/api/java/text/DateFormat.html#setTimeZone(java.util.TimeZone)
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 600 total points
ID: 16523356
>>Timestamp class does not have a timezone associated with it.

I already mentioned that earlier.

What you need first of all is to establish your mechanism for saving the user's time zone in the first place and then secondly you need to retrieve it. Saving their locale would be useful as well for formatting purposes. When you've done that, as i mentioned earlier:

>>then apply it later to the date formatting

// (if saved locale == US and TimeZone == CET)
              DateFormat df = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, Locale.US);
              df.setTimeZone(TimeZone.getTimeZone("CET"));
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16524907
:-)
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

810 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