Java Date Format formatDate(Date date)

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

Description

Format a Date to contain both date and time information with full millisecond precision.

License

Apache License

Parameter

Parameter Description
date The date to be formatted.

Return

The formatted date.

Declaration

public static String formatDate(Date date) 

Method Source Code


//package com.java2s;
/*// w  ww .j  a  v  a  2  s .  c o  m
 * Licensed to the Indoqa Software Design und Beratung GmbH (Indoqa) under
 * one or more contributor license agreements. See the NOTICE file distributed
 * with this work for additional information regarding copyright ownership.
 * Indoqa 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.text.DateFormat;

import java.text.SimpleDateFormat;

import java.util.Date;
import java.util.TimeZone;

public class Main {
    private static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss.SSS";
    private static final TimeZone TIME_ZONE = TimeZone.getTimeZone("UTC");

    /**
     * Format a {@link Date} to contain both date and time information with full millisecond precision. The time zone used is always
     * UTC.
     *
     * The result returned by this method is fully compatible with {@link #parseDate(String)}.
     *
     * @param date The date to be formatted.
     * @return The formatted date.
     */
    public static String formatDate(Date date) {
        return createDateFormat().format(date);
    }

    private static DateFormat createDateFormat() {
        DateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);
        dateFormat.setTimeZone(TIME_ZONE);
        return dateFormat;
    }
}

Related

  1. formatDate(Date date)
  2. formatDate(Date date)
  3. formatDate(Date date)
  4. formatDate(Date date)
  5. formatDate(Date date)
  6. formatDate(Date date)
  7. formatDate(Date date)
  8. formatDate(Date date)
  9. formatDate(Date date)