Link to home
Start Free TrialLog in
Avatar of cgray1223
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/yyyy HH:mm");
            long offset = TimeZone.getDefault().getRawOffset();
            Timestamp time =  new Timestamp(today.getTime() - offset);
            long currentMillis = time.getTime();  
Avatar of rockiroads
rockiroads
Flag of United States of America image

Im not sure about this but could it be related to daylight savings time?

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


Date today = new Date();
         SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy HH:mm");
         long offset = TimeZone.getDefault().getRawOffset();
         Timestamp time =  new Timestamp(today.getTime() - offset);
         long currentMillis = time.getTime();

         System.out.println(today.toString());
         System.out.println(time.toString());
         System.out.println(currentMillis);
         System.out.println(System.currentTimeMillis());

         SimpleDateFormat sdf1 = new SimpleDateFormat("MMM dd,yyyy HH:mm");
         Date resultdate1 = new Date(currentMillis);
         System.out.println(sdf1.format(resultdate1));

         SimpleDateFormat sdf2 = new SimpleDateFormat("MMM dd,yyyy HH:mm");
         Date resultdate2 = new Date(currentMillis);
         System.out.println(sdf1.format(resultdate2));

Open in new window

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 );

Open in new window

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.
Avatar of cgray1223
cgray1223

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
Avatar of rodness
rodness
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial