Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Timezone Conversion

 
Pravin Johnson
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

Given a string in the format as below.
Mon Aug 18 10:30:02 IST 2008

How to convert it to GMT, EDT and other timezone formats..
Please help with this......
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34371
345
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pravin,
It is a two step process
1) Convert your string to a date using SimpleDateFormat
2) Convert your data back to a string using SimpleDateFormat, this time with a timezone argument.

Feel free to post if you have questions when you try this out.
 
Z Sugiarto
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also,

If you're doing timezone-sensitive operation in a country like Australia that have many different timezone with pretty complex daylight saving changes in between, and if your product will run on a UNIX/LINUX platform, then maintaining the timezone definition becomes quite important.

If I am not mistaken, Java doesn't use the olson database installed on local machine (i.e the "TZ" env variable), but rather maintain its own timezone database in the JRE distribution. It also make sense for Java to do this to make it cross-platform.

So somewhere in your product document, you'll also need to add section about updating the JRE installation with future updates, to ensure that the timezone definition is accurate & relevant.

But if you don't ever have to deal with daylight savings, then probably you don't have to worry about this too much.
 
Pravin Johnson
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky ,

I have tried that DateFormat before itself..

String input = "Mon Aug 18 10:00:00 IST 2008";
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date(input);

Its working fine when giving only few formats like GMT,EDT.

When the input in IST or many other formats, its throwing exception..
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34371
345
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pravin,
You should be using df.parse(input) rather than the Date constructor.

Also, the format string you provided and input don't match. One starts with the year and the other ends with the year.

 
Pat Farrell
Rancher
Posts: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, for most things these days, with recent Java versions, you should think in terms of Calendar, not Date. Convert to a Calendar, then you can print it in zillions of formats, time zones, languages, etc.
 
Mike Simmons
Ranch Hand
Posts: 3074
13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Pat Farrell]: Actually, for most things these days, with recent Java versions, you should think in terms of Calendar, not Date. Convert to a Calendar, then you can print it in zillions of formats, time zones, languages, etc.

Or you could just gouge your eyes out with sticks, if you're into that sort of thing.

Seriously, why would you want a Calendar for printing in different formats and time zones? Calendar doesn't do formats - you need to use a DateFormat (often SimpleDateFormat) for that. Which will then take a Date, not a Calendar, as the parameter for its format() method. Therefore Dates are easier to format. Calendar does have a time zone and locale, true - but so does DateFormat. Since you need a DateFormat anyway for formatting, you might as well use it to set the time zoneand/or locale too. Then you can ditch the god-awful, evil Calendar class entirely. Except, well, for the times when you actually need it. Which does happen, sure. Use it when you really need it. But for representing fixed date/times, Date is much less confusing, in my opinion -- despite the many deprecated methods, which are very clearly identified and should b avoided, and the amoral setter methods, which are not actually deprecated but which are totally unnecessary. Date should have been immutable, and anyone calling a setTime() method on a Date should be shot.
 
Pravin Johnson
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

This line is to get the output in this format...

I tried a lot..
But not able to convert this string "Mon Aug 18 10:00:00 IST 2008" to date.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15272
37
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Pravin Johnson:
I tried a lot..
But not able to convert this string "Mon Aug 18 10:00:00 IST 2008" to date.

Why not? Are you using the correct format specifier for the SimpleDateFormat object that you use to parse this string? Look at the API documentation of class SimpleDateFormat and find out what format specifier you need to use.

What did you try and what went wrong?
[ August 26, 2008: Message edited by: Jesper Young ]
 
Pat Farrell
Rancher
Posts: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mike Simmons:
But for representing fixed date/times, Date is much less confusing, in my opinion -- despite the many deprecated methods, which are very clearly identified and should be avoided, and the amoral setter methods, which are not actually deprecated but which are totally unnecessary. Date should have been immutable, and anyone calling a setTime() method on a Date should be shot.


Other than the tons of deprecated methods, amoral setters and general lack of functionality, Dates are great.

And, I can never remember what member function that I want to use is still available when I use Date. When I use Calendar, its easy. I'm too lazy to remember complicated stuff. Two lines with a Calendar and I'm done.


I will wish that in Java 1.10 they rename and refactor all this stuff, its clearly a kludge on top of a hack.
 
Mike Simmons
Ranch Hand
Posts: 3074
13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mmmm, when using Calendar, I can never remember which fields start at 0 and which start at 1. Or which of the many myriad class constants are intended for use as the type of a field, rather than the value of a field. In contrast, I have never had any trouble remembering how to use a Date. It's always seemed obvious: avoid anything that is dependent on timezone or locale. That's the job of a DateFormat. Also, you never explained how to format a Calendar for a specific date format, without converting to a Date at some point.
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Pravin Johnson:
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
...
But not able to convert this string "Mon Aug 18 10:00:00 IST 2008" to date.

Are you possibly using that DateFormat object for parsing the date string you posted? That won't work, since the format is different. You need to use two different DateFormat objects, one for parsing the date, and one for outputting it as whatever format you need.
[ August 27, 2008: Message edited by: Ulf Dittmer ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic