aspose file tools*
The moose likes Java in General and the fly likes Converting dates to GMT Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Converting dates to GMT" Watch "Converting dates to GMT" New topic
Author

Converting dates to GMT

Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1083
I have some code that takes a date, converts it to a String, then parses it back as a GMT date.

This back and forth conversion make me think there should be an easier way to do this. The toGMTString on the date object is depricated.

Thanks for any ideas how I can shorten this code.

Mike

----------------------------------

Date initial = getTimeStamp():
DateFormat dateFormatter = DateFormat.getInstance();
dateFormatter.setTimeZone (TimeZone.getTimeZone("GMT"));
String gmtS = dateFormatter.format(initial);

Date gmt = null ;
try
{
gmt= formatter.parse(gmtS);
System.out.println(gmt); // prints correct GMT date.
}
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Err, are you aware that your code really isn't doing anything other than truncating the seconds and milliseconds from a timestamp?

Also, it is unclear to me what you are trying to do. What is your spec? You are given a java.util.Date as input. What is your output? Another Date? A String?

One thing to keep in mind is that java.util.Date objects are essentially timestamps -- just a long value. They have no notion of timezone associated with their state. Or you can thing of them as all being in UTC. In either case, "converting the time zone" of a Date object doesn't make sense. But then, I'm not clear on what you are trying to do.
[ May 17, 2006: Message edited by: Jeff Albertson ]

There is no emoticon for what I am feeling!
Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1083
The code as I posted it takes a current Timestamp and stores it in GMT format.

I've tested it and it works fine.

I was just wondering if it could be shortened.

Thanks.
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
I'm still confused. The input to your "function" is a date given by getTimestamp. What is the output? String gmtS or the output of System.out.println(gmt)? What is the purpose of the second date object?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Converting dates to GMT