Java SQL Date Convert dateToString(Date date)

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

Description

date To String

License

Apache License

Declaration

public static String dateToString(Date date) 

Method Source Code

//package com.java2s;
/**/*from   w  ww. j ava  2s  .c  om*/
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.Date;
import java.text.SimpleDateFormat;
import java.util.Map;
import java.util.TimeZone;
import java.util.concurrent.ConcurrentHashMap;

public class Main {
    public static final String DEFAULT_DATETIME_PATTERN_WITHOUT_MILLISECONDS = "yyyy-MM-dd HH:mm:ss";
    static final private Map<String, ThreadLocal<SimpleDateFormat>> threadLocalMap = new ConcurrentHashMap<String, ThreadLocal<SimpleDateFormat>>();

    public static String dateToString(Date date) {
        return dateToString(date,
                DEFAULT_DATETIME_PATTERN_WITHOUT_MILLISECONDS);
    }

    public static String dateToString(Date date, String pattern) {
        return getDateFormat(pattern).format(date);
    }

    public static SimpleDateFormat getDateFormat(String datePattern) {
        ThreadLocal<SimpleDateFormat> formatThreadLocal = threadLocalMap
                .get(datePattern);
        if (formatThreadLocal == null) {
            threadLocalMap
                    .put(datePattern,
                            formatThreadLocal = new ThreadLocal<SimpleDateFormat>());
        }
        SimpleDateFormat format = formatThreadLocal.get();
        if (format == null) {
            format = new SimpleDateFormat(datePattern);
            format.setTimeZone(TimeZone.getTimeZone("GMT"));
            formatThreadLocal.set(format);
        }
        return format;
    }
}

Related

  1. DateToSQLDate(Date pDate)
  2. dateToSQLDate(java.util.Date d)
  3. dateToStamp(String date)
  4. dateToSTR(Date data, String formato)
  5. dateToStr(java.util.Date date)
  6. dateToString(Date date, String format)
  7. dateToString(java.sql.Date d)
  8. dateToString(java.sql.Date data)
  9. dateToString(java.sql.Date datee)