Java Date to String dateToString(final Date value, final boolean utc)

Here you can find the source of dateToString(final Date value, final boolean utc)

Description

Converts a date to an ISO8601 string.

License

Apache License

Parameter

Parameter Description
value The date.
utc Boolean, whether to format the time in UTC or the local time zone.

Return

The ISO8601 string.

Declaration

public static String dateToString(final Date value, final boolean utc) 

Method Source Code

//package com.java2s;
/*/*  w  w  w. ja v  a  2s .c  o  m*/
 * Copyright 2013
 * 
 * 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.TimeZone;

public class Main {
    /**
     * Initializes a SimpleDateFormat for use if UTC is not enabled
     * 
     * @since org.openntf.domino 1.0.0
     */
    private static ThreadLocal<SimpleDateFormat> ISO_LOCAL = new ThreadLocal<SimpleDateFormat>() {
        @Override
        protected SimpleDateFormat initialValue() {
            return new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
        }
    };
    /**
     * Initializes a SimpleDateFormat for use if UTC is enabled
     * 
     * @since org.openntf.domino 1.0.0
     */
    private static ThreadLocal<SimpleDateFormat> ISO_UTC_LOCAL = new ThreadLocal<SimpleDateFormat>() {
        @Override
        protected SimpleDateFormat initialValue() {
            SimpleDateFormat ISO8601_UTC = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); //$NON-NLS-1$;
            TimeZone tz = TimeZone.getTimeZone("UTC");
            ISO8601_UTC.setTimeZone(tz);
            return ISO8601_UTC;
        }
    };

    /**
     * Converts a date to an ISO8601 string.
     * 
     * @param value
     *            The date.
     * @param utc
     *            Boolean, whether to format the time in UTC or the local time zone.
     * @return The ISO8601 string.
     * @since org.openntf.domino 1.0.0
     */
    public static String dateToString(final Date value, final boolean utc) {

        String result = null;

        if (utc) {
            result = ISO_UTC_LOCAL.get().format(value);
        } else {
            result = ISO_LOCAL.get().format(value);
        }

        return result;
    }
}

Related

  1. dateToString(Date pd_fecha)
  2. dateToString(Date pDate, String pFormat)
  3. dateToString(Date pdttValue, String pstrDateFormat)
  4. DateToString(Date thisdate, Locale locale)
  5. dateToString(final Date date)
  6. dateToString(java.util.Date currdate, String strFormat)
  7. dateToString(String date, String format)
  8. dateToString(String str, DateTimeFormatter dateTimeFormatter)
  9. dateToString1(Date date, String formatIn)