Java Hour Format format(final Date date)

Here you can find the source of format(final Date date)

Description

Format date to utc date string.

License

Apache License

Parameter

Parameter Description
date Date to format

Return

Utc date string

Declaration

public static String format(final Date date) 

Method Source Code

//package com.java2s;
/**/*from  w ww. ja v  a  2 s.c o m*/
 * vertigo - simple java starter
 *
 * Copyright (C) 2013-2016, KleeGroup, direction.technique@kleegroup.com (http://www.kleegroup.com)
 * KleeGroup, Centre d'affaire la Boursidiere - BP 159 - 92357 Le Plessis Robinson Cedex - France
 *
 * 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.DateFormat;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

public class Main {
    private static final String[] INPUT_DATE_FORMATS = new String[] {
            "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", //ISO8601
            "EEE, dd MMM yyyy HH:mm:ss zzz", // RFC 822, updated by RFC 1123
            "EEEE, dd-MMM-yy HH:mm:ss zzz", // RFC 850, obsoleted by RFC 1036
    //not supported : "EEE MMM d HH:mm:ss yyyy", // ANSI C's asctime() format
    };

    /**
     * Format date to utc date string.
     * @param date Date to format
     * @return Utc date string
     */
    public static String format(final Date date) {
        //Use INPUT_DATE_FORMATS[0] => ISO8601 format
        return createDateFormat(INPUT_DATE_FORMATS[0],
                isTruncatedDate(date)).format(date);
    }

    private static DateFormat createDateFormat(final String dateFormat,
            final boolean isTruncatedDate) {
        final DateFormat simpleDateFormat = new SimpleDateFormat(
                dateFormat, Locale.US);
        if (!isTruncatedDate) {
            simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
        }
        return simpleDateFormat;
    }

    private static boolean isTruncatedDate(final Date date) {
        final Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.HOUR_OF_DAY) == 0
                && calendar.get(Calendar.MINUTE) == 0
                && calendar.get(Calendar.SECOND) == 0
                && calendar.get(Calendar.MILLISECOND) == 0;
    }

    private static boolean isTruncatedDate(final String dateStr) {
        return dateStr.endsWith("T00:00:00.000Z");
    }
}

Related

  1. format(Date self, String format, TimeZone tz)
  2. format(Date time, String fmt)
  3. format(double number, int decimalPlace)
  4. format(final Date date)
  5. format(final Date date)
  6. format(int level, String message, String source, boolean verbose)
  7. format(java.util.Date date, String format)
  8. format(List list, int scale)
  9. format(long time)