Utc date to local date. - Android java.util

Android examples for java.util:Date Convert

Description

Utc date to local date.

Demo Code


//package com.java2s;
import android.text.TextUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;

import java.util.Date;

public class Main {
    public static final String DEFAULT_FORMAT = "yyyy-MM-dd HH:mm:ss";
    public static final String UTC_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";

    /**/*  www  .j a  v  a  2s  .  c  om*/
     * Utc to local.
     *
     * @param utcTime the utc time
     * @return the string
     */
    public static String utcToLocal(String utcTime) {
        String localTime = null;
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
                    UTC_FORMAT);
            Date date = simpleDateFormat.parse(utcTime);
            simpleDateFormat.applyPattern(DEFAULT_FORMAT);
            localTime = simpleDateFormat.format(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return localTime;
    }

    /**
     * Format long.
     *
     * @param time the time
     * @param format the format
     * @return the long
     */
    public static long format(String time, String format) {
        if (TextUtils.isEmpty(time)) {
            return 0;
        }

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);

        long modified = 0;
        try {
            Date date = simpleDateFormat.parse(time);
            modified = date.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return modified;
    }

    /**
     * Format string.
     *
     * @param time the time
     * @return the string
     */
    public static String format(long time) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
                DEFAULT_FORMAT);
        return simpleDateFormat.format(new Date(time));
    }

    /**
     * Format string.
     *
     * @param time the time
     * @param format the format
     * @return the string
     */
    public static String format(long time, String format) {
        if (TextUtils.isEmpty(format)) {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            return sdf.format(new Date(time));
        }

        return null;
    }

    /**
     * Format string.
     *
     * @param date the date
     * @param format the format
     * @return the string
     */
    public static String format(Date date, String format) {
        if (TextUtils.isEmpty(format) || date == null) {
            return null;
        }

        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(date);
    }

    /**
     * Format string.
     *
     * @param timeStr the time str
     * @param srcFormat the src format
     * @param dstFormat the dst format
     * @return the string
     */
    public static String format(String timeStr, String srcFormat,
            String dstFormat) {
        long time = format(timeStr, srcFormat);
        String result = format(time, dstFormat);
        return result;
    }
}

Related Tutorials