Jimmy
Jimmy

Reputation: 10341

How to convert java date to a date object based on time zone?

So i save the time-stamp as a Date object and a timezone as a TimeZone object.

Now i want to make a function that takes a Date object and a TimeZone object as argument and returns a Date object adjusted using the time-stamp.

For example :

Input :

Date    TimeZone 

12:00   Moscow Standard Time (UTC+3)

Output :

Date 

3:00    

Edit: Removed Note about Calendar

Upvotes: 1

Views: 12744

Answers (4)

Thom
Thom

Reputation: 15042

Here you go:

/**
 * Convert a calendar from its current time zone to UTC (Greenwich Mean Time)
 * @param local the time
 * @return a calendar with the UTC time
 */
public static Calendar convertTimeToUtc(Calendar local){
    int offset = local.getTimeZone().getOffset(local.getTimeInMillis());
    GregorianCalendar utc = new GregorianCalendar(TZ_UTC);
    utc.setTimeInMillis(local.getTimeInMillis());
    utc.add(Calendar.MILLISECOND, -offset);

    return utc;
}

/**
 * Convert a UTC date into the specified time zone
 * @param tzName the name of the time zone for the output calendar
 * @param utc the UTC time being converted
 * @return a calendar in the specified time zone with the appropriate date
 */
public static Calendar convertTimeToLocal(String tzName, Calendar utc) {
    TimeZone zone = TimeZone.getTimeZone(tzName);
    int offset = zone.getOffset(utc.getTimeInMillis());
    GregorianCalendar local = new GregorianCalendar(zone);
    local.setTimeInMillis(utc.getTimeInMillis());
    local.add(Calendar.MILLISECOND, offset);

    return local;
}

/**
 * Convert a UTC date into the specified time zone
 * @param zone the time zone of the output calendar
 * @param utc the UTC time being converted
 * @return a calendar in the specified time zone with the appropriate date
 */
public static Calendar convertTimeToLocal(TimeZone zone, Calendar utc) {
    int offset = zone.getOffset(utc.getTimeInMillis());
    GregorianCalendar local = new GregorianCalendar(zone);
    local.setTimeInMillis(utc.getTimeInMillis());
    local.add(Calendar.MILLISECOND, offset);

    return local;
}

Upvotes: 1

Jimmy
Jimmy

Reputation: 10341

I found an easy way to accomplish that using the rowOffset of the desired time zone :

Date date = new Date();
int rawOffset = TimeZone.getTimeZone("EST").getRawOffset(); 
Date adjustedDate = new Date(date.getTime() + rawOffset)

Edit: as Affe point out this will ignore leap seconds and daylight saving.

Upvotes: 0

Affe
Affe

Reputation: 47954

A java.util.Date is an absolute point in time. 0900 hours UTC and 1200 hours UTC+3 are the exact same java.util.Date object. There is no 'adjustment' to be made to it in order to represent one or the other.

To get the human readable representation accounting for a particular timezone, you can set a timezone on a DateFormat object.

DateFormat format = new SimpleDateFormat("HH:mm");
format.setTimeZone(TimeZone.getTimeZone("UTC+3"));
String time = format.format(yourDate);

Solution for the question posed in comment:

Calendar cal1 = Calendar.getInstance(TimeZone.getTimeZone("UTC+3"));
cal1.setTime(yourDate);
Calendar cal2 = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
cal2.clear();
cal2.set(Calendar.YEAR, cal1.get(Calendar.YEAR));
cal2.set(Calendar.MONTH, cal1.get(Calendar.MONTH));
cal2.set(Calendar.DATE, cal1.get(Calendar.DATE));
cal2.set(Calendar.HOUR_OF_DAY, cal1.get(Calendar.HOUR_OF_DAY));
//simile for whatever level of field precision is needed
Date shiftedDate = cal2.getTime();

Upvotes: 8

Cody S
Cody S

Reputation: 4824

If you aren't using Calendar, what are you using? Another library perhaps?

If not, you have that +3 there at the end of your timezone...you could use that to bump the hour (+/-)X; in this case, +3. Just remember that, in this case (for instance) 11+3=2. This math could be accomplished by adding hour + offset, taking that value % 12, and setting that answer as the hour (setting an answer of 0 to 12 if so desired). Does that make sense?

Upvotes: 0

Related Questions