Date/time conversion in java

Have a date string "12/29/2010 12:53:11" in Honkong timezone. I need to convert this time to GMT and then add/subtract some time(+5.30 or different) as per the user location and get the correspoding date value. Please help through java coding.
-Aneesh.
ganeshaneeshAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mick BarryJava DeveloperCommented:

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
garypfirstechCommented:
When you convert to a java Calendar object, you are representing a given point in time.  You can then have that represented in whatever time zone you'd like.

I would do something like this:

SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
sdf.setTimeZone(TimeZone.getTimeZone("Asia/Hong_Kong"));
Date myDate = sdf.parse(incoming_date_string);

Open in new window


You can manipulate the date from there either in milliseconds (via myDate.getTime()) or you can create a Calendar from the date and do your manipulation there.

If you have more specific questions, please let me know.
ganeshaneeshAuthor Commented:
Now i got the time in GMT, have to add 5.30 hrs. Any help?
-Aneesh.
Exploring SharePoint 2016

Explore SharePoint 2016, the web-based, collaborative platform that integrates with Microsoft Office to provide intranets, secure document management, and collaboration so you can develop your online and offline capabilities.

Mick BarryJava DeveloperCommented:
see here
http://helpdesk.objects.com.au/java/how-do-i-add-a-specified-number-of-hours-to-a-java-date

though better to use a Calendar object and just set the timezone instead of adding the time yourself
CEHJCommented:
>>... and then add/subtract some time(+5.30 or different) as per the user location

You only need to print the time in the timezone of that user - that will do the 'addition' automatically
ganeshaneeshAuthor Commented:
Hi All, thanks for your reply. But, still my requirement is not met. I will write you the actual requirement.

My server runs in india. Users from different countries will access the webpages. There are two date values also to be displayed. one is last accessed date(is in ist comes from db), second date is current date. These dates has to be displayed in user timezone. I have timezone in db(like GMT-2.30 ...). I have to complete it in servlet class itself. I was considering like converting IST to GMT, then getting the user date by doing arithmetic like -2.30 I couldnt succeed. Please provide help/code to achieve it. Again i tell you I have "12/29/2010 14:22:51" in IST, has to be displayed in user timezone.
-Aneesh.
CEHJCommented:
As what type are you storing the date in the db
CEHJCommented:
If you store the date in the db as type datetime/timestamp, all you need to do is to format the date using a DateFormat set with the TimeZone of the user. No calculation should be required
Mick BarryJava DeveloperCommented:
the simplest solution is to store your dates in the database in GMT
that way you can display them in any timezone using SimpleDateFormat
http://helpdesk.objects.com.au/java/how-do-i-format-a-date-as-a-string
and setting the timezone as I explained above
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.