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();  
cgray1223Asked:
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.

rockiroadsCommented:
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

rodnessCommented:
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

rodnessCommented:
Ps-  you can, of course, season the format string in line 3 to be whatever you want.
Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

rodnessCommented:
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.
cgray1223Author Commented:
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.
rodnessCommented:
Ok...  this should convert a Date object to GMT, and then to a long (in millis).
Then you can subtract them for the difference.

(Note I just hacked it together.. it's not tested, but should give you the idea.)
Date date = ... // some date you need to convert to GMT
Calendar cal = Calendar.getInstance( TimeZone.getTimeZone( "GMT" ) );
cal.setTime( date );
long dateInMillis = cal.getTimeInMillis();

Open in new window

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
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.