Converts the time in a local time zone to UTC time - Android java.util

Android examples for java.util:Time

Description

Converts the time in a local time zone to UTC time

Demo Code


import android.content.ContentResolver;
import android.content.Context;
import android.net.Uri;
import android.provider.CalendarContract;
import com.android.calendar.CalendarEventModel;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Random;
import java.util.TimeZone;

public class Main{
    /**/*from   w  ww  .  j a  v a2  s .c o m*/
     * Converts the time in a local time zone to UTC time
     * @param millis epoch time in the local timezone
     * @param localTimeZone string id of the local time zone
     * @return
     */
    public static long convertTimeToUtc(long millis, String localTimeZone) {
        if (millis < 0)
            return 0;

        // remove the local time zone's UTC offset
        return millis - TimeZone.getTimeZone(localTimeZone).getRawOffset();
    }
}

Related Tutorials