Java Calendar Format formatDate(Calendar date, int precision)

Here you can find the source of formatDate(Calendar date, int precision)

Description

Format a Calendar date with a given precision.

License

Apache License

Parameter

Parameter Description
date Date as a Calendar to be formatted.
precision Calendar field value of desired precision.

Return

String formatted to the requested precision.

Declaration

public static String formatDate(Calendar date, int precision) 

Method Source Code

//package com.java2s;
/*//ww  w  .j  a v a 2s . co  m
 * Copyright (C) 2014 Dell, Inc.
 * 
 * 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.util.Calendar;

import java.util.GregorianCalendar;

public class Main {
    /**
     * Format a Calendar date with a given precision. 'precision' must be a Calendar
     * "field" value such as Calendar.MINUTE. The allowed precisions and the corresponding
     * string formats returned are:
     * <pre>
     *      Calendar.MILLISECOND:   YYYY-MM-DD hh:mm:ss.SSS 
     *      Calendar.SECOND:        YYYY-MM-DD hh:mm:ss 
     *      Calendar.MINUTE:        YYYY-MM-DD hh:mm 
     *      Calendar.HOUR:          YYYY-MM-DD hh 
     *      Calendar.DATE:          YYYY-MM-DD
     *      Calendar.MONTH:         YYYY-MM
     *      Calendar.YEAR:          YYYY
     * </pre>
     * Note that Calendar.DAY_OF_MONTH is a synonym for Calendar.DATE.
     *
     * @param date      Date as a Calendar to be formatted.
     * @param precision Calendar field value of desired precision.
     * @return          String formatted to the requested precision.
     */
    public static String formatDate(Calendar date, int precision) {
        assert date != null;

        // Remember that the bloody month field is zero-relative!
        switch (precision) {
        case Calendar.MILLISECOND:
            // YYYY-MM-DD hh:mm:ss.SSS
            return String.format("%04d-%02d-%02d %02d:%02d:%02d.%03d", date.get(Calendar.YEAR),
                    date.get(Calendar.MONTH) + 1, date.get(Calendar.DAY_OF_MONTH), date.get(Calendar.HOUR_OF_DAY),
                    date.get(Calendar.MINUTE), date.get(Calendar.SECOND), date.get(Calendar.MILLISECOND));
        case Calendar.SECOND:
            // YYYY-MM-DD hh:mm:ss
            return String.format("%04d-%02d-%02d %02d:%02d:%02d", date.get(Calendar.YEAR),
                    date.get(Calendar.MONTH) + 1, date.get(Calendar.DAY_OF_MONTH), date.get(Calendar.HOUR_OF_DAY),
                    date.get(Calendar.MINUTE), date.get(Calendar.SECOND));
        case Calendar.MINUTE:
            // YYYY-MM-DD hh:mm
            return String.format("%04d-%02d-%02d %02d:%02d", date.get(Calendar.YEAR), date.get(Calendar.MONTH) + 1,
                    date.get(Calendar.DAY_OF_MONTH), date.get(Calendar.HOUR_OF_DAY), date.get(Calendar.MINUTE));
        case Calendar.HOUR:
            // YYYY-MM-DD hh
            return String.format("%04d-%02d-%02d %02d", date.get(Calendar.YEAR), date.get(Calendar.MONTH) + 1,
                    date.get(Calendar.DAY_OF_MONTH), date.get(Calendar.HOUR_OF_DAY));
        case Calendar.DATE:
            // YYYY-MM-DD
            return String.format("%04d-%02d-%02d", date.get(Calendar.YEAR), date.get(Calendar.MONTH) + 1,
                    date.get(Calendar.DAY_OF_MONTH));
        case Calendar.MONTH:
            // YYYY-MM
            return String.format("%04d-%02d", date.get(Calendar.YEAR), date.get(Calendar.MONTH) + 1);
        case Calendar.YEAR:
            // YYYY
            return String.format("%04d", date.get(Calendar.YEAR));
        }
        throw new IllegalArgumentException("Unknown precision: " + precision);
    }

    /**
     * Format a Calendar date as "YYYY-MM-DD HH:mm:ss". This is a convenience method
     * that calles {@link #formatDate(Calendar, int)} with Calendar.SECOND for 'precision'.
     *
     * @param   date    Date as a Calendar to be formatted.
     * @return          "YYYY-MM-DD HH:mm:ss".
     */
    public static String formatDate(Calendar date) {
        return formatDate(date, Calendar.SECOND);
    }

    /**
     * Format a Date.getTime() value as "YYYY-MM-DD HH:mm:ss". This method creates a
     * GregorianCalendar object using the <i>local</i> time zone and then calls
     * {@link #formatDate(Calendar)}.
     *
     * @param   time    Date/time in Date.getTime() format (milliseconds since the epoch).
     * @return          "YYYY-MM-DD HH:mm:ss".
     */
    public static String formatDate(long time) {
        // Map date/time to a GregorianCalendar object (local time zone).
        GregorianCalendar date = new GregorianCalendar();
        date.setTimeInMillis(time);
        return formatDate(date, Calendar.SECOND);
    }
}

Related

  1. formatDate(Calendar cal)
  2. formatDate(Calendar calendar)
  3. formatDate(Calendar date)
  4. formatDate(Calendar date)
  5. formatDate(Calendar date)
  6. FormatDate(Calendar p_date, String p_separator)
  7. formatDate(String formats, Calendar date)
  8. formatDateTime(Calendar cal)
  9. formatDateTimeUTC(Calendar datetime)