cgray1223
asked on
Java to calculate the current GMT date
Hello,
I'm trying to compare an existing gmt date/time to the current GMT date/time and I think the below code is an hour off when calculating the current GMT time in milliseconds. Any ideas?
Date today = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yy yy HH:mm");
long offset = TimeZone.getDefault().getR awOffset() ;
Timestamp time = new Timestamp(today.getTime() - offset);
long currentMillis = time.getTime();
I'm trying to compare an existing gmt date/time to the current GMT date/time and I think the below code is an hour off when calculating the current GMT time in milliseconds. Any ideas?
Date today = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yy
long offset = TimeZone.getDefault().getR
Timestamp time = new Timestamp(today.getTime() - offset);
long currentMillis = time.getTime();
I've had to do this myself....
TimeZone timeZone = TimeZone.getTimeZone( "GMT" );
SimpleDateFormat gmtSeconds = new SimpleDateFormat( "yyyy/MM/dd HH:mm:ss z" );
gmtSeconds.setCalendar( Calendar.getInstance( timeZone ) );
String gmtDate = gmtSeconds.format( date );
Ps- you can, of course, season the format string in line 3 to be whatever you want.
Bleah. I misread your question as a "how do I get this in GMT format..."
The problem with your original solution is that you're using offsets. The reason one would use GMT time is because it's universal, and you don't need to worry about offsets (which change, and are influenced by things like daylight time, leap time, etc).
My code above will convert any date into GMT. Then you can do a simple string compare... You may want to adjust the format in SimpleDateFormat to include millis. Refer to the javadocs for SimpleDateFormat.
The problem with your original solution is that you're using offsets. The reason one would use GMT time is because it's universal, and you don't need to worry about offsets (which change, and are influenced by things like daylight time, leap time, etc).
My code above will convert any date into GMT. Then you can do a simple string compare... You may want to adjust the format in SimpleDateFormat to include millis. Refer to the javadocs for SimpleDateFormat.
ASKER
Thanks for the possible solutions. I need to subtract the two dates (one being current GMT and one being in the past) after converting the date to milliseconds. I'm guessing I have to convert the date to long data types? Basically, I need to see what the difference is between the two dates in milliseconds.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
How was you checking the differences?
Sun have a tz updater tool. I don't really know if that would help or not though http://www.oracle.com/technetwork/java/javase/tzupdater-readme-136440.html
Open in new window