Java Timestamp Format formatDate(java.util.Date d)

Here you can find the source of formatDate(java.util.Date d)

Description

format Date

License

Apache License

Declaration

public static String formatDate(java.util.Date d) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright 2014 Miami-Dade County/*ww w.  j ava2 s  .  c o m*/
 * 
 * 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.sql.Timestamp;

import java.text.SimpleDateFormat;

import java.util.HashSet;
import java.util.Set;

public class Main {
    private static final ThreadLocal<SimpleDateFormat> ISO_DATE_FORMATS = new ThreadLocal<SimpleDateFormat>();
    public static final String isoDatePattern = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";

    public static String formatDate(java.util.Date d) {
        SimpleDateFormat myDateFormat = ISO_DATE_FORMATS.get();
        if (myDateFormat == null) {
            myDateFormat = new SimpleDateFormat(isoDatePattern);
            ISO_DATE_FORMATS.set(myDateFormat);
        }
        return myDateFormat.format(d);
    }

    /**
     * This method returns the timestamp in the given format.
     * In case of null timestamp and exceptions return "N/A"
     */
    public static String formatDate(Timestamp ts, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        try {
            if (ts == null)
                return "N/A";
            else
                return sdf.format(ts);
        } catch (Exception e) {
            return "N/A";
        }
    }

    public static <T> Set<T> set(T... elements) {
        HashSet<T> S = new HashSet<T>();
        for (T x : elements)
            S.add(x);
        return S;
    }
}

Related

  1. formatCurrentDateTimeStamp(final String dateFormatString)
  2. formatDataTime(Timestamp intime)
  3. formatDate(Date date, String pattern)
  4. formatDate(Date date, String pattern)
  5. formatDate(java.sql.Timestamp timestamp)
  6. formatDate(java.util.Date date)
  7. formatDate(Timestamp date)
  8. formatDate(Timestamp time)
  9. formatDate(Timestamp time, String pattern)