0

My client told me that he will provide me timezone offset and date. I need to converted date in GMT accordingly.

I am not sure If I am correct side or not. I think offset means how many hours up and down from GMT and after getting that apply that difference to date time for getting converted one.

Please look at below what I am trying to achieve client expectation.

TimeZone timezone = TimeZone.getTimeZone("GMT");
timezone.setRawOffset(28800000);

If above code lines are correct, what would be code for getting converted date? Please suggest...

Regards

S Singh
  • 1,403
  • 9
  • 31
  • 47
  • 1
    I would suggest you to use Joda Time, to use DateTimes with TimeZones: http://www.joda.org/joda-time/ – facundofarias Feb 05 '15 at 19:29
  • (a) You need to be more specific in your Question. Give examples of your data inputs and expected/desired outputs. (b) search StackOverflow before posting. Time zone conversions have been handled many hundreds of times already. (c) Tip: search for "joda" and search for "java.time". – Basil Bourque Feb 07 '15 at 07:35

1 Answers1

0

You'll want to use the Calender Class

public static Calendar convertToGmt(Calendar cal) {

    Date date = cal.getTime();
    TimeZone tz = cal.getTimeZone();

    log.debug("input calendar has date [" + date + "]");

    //Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT 
    long msFromEpochGmt = date.getTime();

    //gives you the current offset in ms from GMT at the current date
    int offsetFromUTC = tz.getOffset(msFromEpochGmt);
    log.debug("offset is " + offsetFromUTC);

    //create a new calendar in GMT timezone, set to this date and add the offset
    Calendar gmtCal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
    gmtCal.setTime(date);
    gmtCal.add(Calendar.MILLISECOND, offsetFromUTC);

    log.debug("Created GMT cal with date [" + gmtCal.getTime() + "]");

    return gmtCal;
}

Taken from here

Community
  • 1
  • 1
drop27
  • 143
  • 2
  • 11