Java UTC Date utcToLocal(Date date)

Here you can find the source of utcToLocal(Date date)

Description

Utc to local string.

License

Apache License

Parameter

Parameter Description
date the date

Return

the string

Declaration

public static String utcToLocal(Date date) 

Method Source Code

//package com.java2s;
/*//from  w  ww.  java  2 s.co m
 * Copyright (c) 2016 Martin Pfeffer
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.Locale;

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";

    /**
     * Utc to local string.
     *
     * @param date the date
     * @return the string
     */
    public static String utcToLocal(Date date) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(UTC_FORMAT);
            sdf.applyPattern(DEFAULT_FORMAT);
            return sdf.format(date);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * Utc to local string.
     *
     * @param date   the date
     * @param locale the locale
     * @return the string
     */
    public static String utcToLocal(Date date, Locale locale) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(UTC_FORMAT, locale);
            sdf.applyPattern(DEFAULT_FORMAT);
            return sdf.format(date);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}

Related

  1. toUTCString(long t)
  2. utcTime(Locale locale)
  3. utcTimestamp(Date date)
  4. utcTimeToString(String dateStr)
  5. utcToDate(String utcTimestamp)
  6. utcTolocal(String utc)