Java code to conevrt 1 timezone to another

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.

   

 
jnajayjainAsked:
Who is Participating?
 
objectsCommented:
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
 
CEHJCommented:
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
 
geekprogCommented:
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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
CEHJCommented:
java.sql.Timestamp is also free of TimeZone information and should be formatted according to the user's time zone
0
 
objectsCommented:
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
 
jnajayjainAuthor Commented:
tried with TimeStamp ts =  new TimeStamp("hour:minut:ss:AM");
got compilation errors.

Myn databas is Oracle 9i.
0
 
CEHJCommented:
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
 
objectsCommented:
following covers how to handle timezones with oracle

http://www.dbasupport.com/oracle/ora9i/TimeZone.shtml
0
 
jnajayjainAuthor Commented:
Still did not get any clue about how using JAVA I can convert timespamp Say(Asia/Calcutta IST) to EST and vise versa.
0
 
jnajayjainAuthor Commented:
Can smone post any exapmle code for this.?
0
 
CEHJCommented:
>>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
 
CEHJCommented:
:-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.