Java Hour Format format(Date date)

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

Description

Formats a Date value into a ISO8601-compliant date/time string.

License

Apache License

Parameter

Parameter Description
date the time value to be formatted into a date/time string.

Return

the formatted date/time string.

Declaration

public static String format(Date date) 

Method Source Code

//package com.java2s;
/**/*from   w  ww.  j  a va  2 s  .c om*/
 * Copyright 2015 Marco Scavuzzo
 * Contact: Marco Scavuzzo <marco.scavuzzo@polimi.it>
 *
 * 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.DecimalFormat;

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

public class Main {
    /** used to format time zone offset */
    private static DecimalFormat xxFormat = new DecimalFormat("00");
    /**
     * specific ISO8601-compliant format string (without time zone information)
     */
    private static String ISO_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSS";

    /**
     * Formats a <code>Calendar</code> value into a ISO8601-compliant
     * date/time string.
     * @param cal the time value to be formatted into a date/time string.
     * @return the formatted date/time string.
     */
    public static String format(Calendar cal) {
        SimpleDateFormat format = new SimpleDateFormat(ISO_FORMAT);
        TimeZone tz = cal.getTimeZone();
        format.setTimeZone(tz);

        StringBuffer tzd = new StringBuffer("Z");
        int offset = tz.getRawOffset();
        if (offset != 0) {
            int hours = Math.abs((offset / (60 * 1000)) / 60);
            int minutes = Math.abs((offset / (60 * 1000)) % 60);
            tzd.append(offset < 0 ? "-" : "+");
            tzd.append(xxFormat.format(hours));
            tzd.append(":");
            tzd.append(xxFormat.format(minutes));
        }
        return format.format(cal.getTime()) + tzd.toString();
    }

    /**
     * Formats a <code>Date</code> value into a ISO8601-compliant
     * date/time string.
     * @param date the time value to be formatted into a date/time string.
     * @return the formatted date/time string.
     */
    public static String format(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return format(cal);
    }
}

Related

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