utc time to Local time - Android java.util

Android examples for java.util:Time

Description

utc time to Local time

Demo Code


//package com.java2s;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

public class Main {

    public static String utc2Local(long utcTime, String dateFormat) {
        String localTime = "";
        if (utcTime > 0) {
            Date localDate = utcTimeToDate(secondTurnMs(utcTime));
            SimpleDateFormat localFormater = new SimpleDateFormat(
                    dateFormat);/* w  w  w  . ja v a 2s.  c o m*/
            localFormater.setTimeZone(TimeZone.getDefault());
            localTime = localFormater.format(localDate);
        }
        return localTime;
    }

    public static Date utcTimeToDate(long utcTime) {
        Date resultDate = null;
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(secondTurnMs(utcTime));
        resultDate = calendar.getTime();
        return resultDate;
    }

    public static long secondTurnMs(long utcTime) {
        if (String.valueOf(utcTime).length() == 10) {
            utcTime *= 1000;
        }
        return utcTime;
    }
}

Related Tutorials