Link to home
Start Free TrialLog in
Avatar of terajiv
terajiv

asked on

Getting Time in Milis from International Date/Times

Hello All,

I want to Convert all the incoming Date/Time String into Miliseconds.

e.g. I may get Following dates in String Format
 1. 08 May 2002 08:24:37 GMT+10:00
 2. 13 Jun 2002 23:58:06 EST
 3. 24 Apr 2002 13:12:31 PDT
 4. Sat Mar 30 00:23:53 GMT+02:00 2002
 5. 02 Apr 2002 15:59:26 CST

I want a generalized Solution for getting Above Dates into GMT Specific Miliseconds... I want those with respect to GMT and not EST, PDT or CST etc.

Help me out of this please...

Raj
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

long l = new Date().getTime() will put the GMT milliseconds into l
According to the docs, it would seem the situation is more like:

import java.util.*;

class DateOffs {
      static int MS_CONV = 60 * 1000;
     
      public static void main(String[] args) {
           Date d = new Date();
           long t = d.getTime();
           System.out.println("GMT (UTC) in milliseconds is " + t);
           int offsetZ =  Calendar.ZONE_OFFSET;
           System.out.println("Timezone offset in minutes is " + offsetZ);
           int offsetDST = Calendar.DST_OFFSET;
           System.out.println("Daylight Saving Time  offset in minutes is " + offsetDST);
           int offset_ms = offsetZ + offsetDST * MS_CONV;
           System.out.println("GMT (UTC) in milliseconds, allowing for offsets, is " + (t - offset_ms));  
      }
 }
 
 The following should work for the four of them:

String s = "08 May 2002 08:24:37 GMT+10:00";
String s2 = "13 Jun 2002 23:58:06 EST";
String s3 = "24 Apr 2002 13:12:31 PDT";
//String s4 = "Sat Mar 30 00:23:53 GMT+02:00 2002";
String s5 = "02 Apr 2002 15:59:26 CST";
DateFormat df = DateFormat.getDateInstance(DateFormat.FULL);
Date d = df.parse(s5); // change the s5 for every Sting variable
long l = d.getTime();
System.out.println("l: " + l);

  As for the s4 string I have never seen this date format before.

  Hope it helps.
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland 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
>I want those with respect to GMT

  Add Locale.UK at the getDateInstance method:

DateFormat df = DateFormat.getDateInstance(DateFormat.FULL, Locale.UK);
Sorry about that - refreshed and reposted by accident. Also, have not read your question properly!
Avatar of terajiv
terajiv

ASKER

The Problem is solved now...  I appreciate Answer from CEHJ

Thanks all,

Raj