utc To Native Time - Android java.util

Android examples for java.util:Time

Description

utc To Native 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 utcToNativeTime(long utcTime, String dateFormat) {

        String localTime = "";

        final long TIME = 8 * 60 * 60 * 1000;

        utcTime = secondTurnMs(utcTime);
        utcTime -= TIME;//from www.j a v a2s. co m
        if (utcTime > 0) {
            Date localDate = utcTimeToDate(utcTime);
            SimpleDateFormat localFormater = new SimpleDateFormat(
                    dateFormat);
            localFormater.setTimeZone(TimeZone.getDefault());
            localTime = localFormater.format(localDate);
        }
        return localTime;
    }

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

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

Related Tutorials