Java Date Format ISO formatISODate(Date date)

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

Description

Returns a date formatted according to ISO 8601 rules.

License

BSD License

Parameter

Parameter Description
date the date to format.

Return

the formmatted date.

Declaration

public static String formatISODate(Date date) 

Method Source Code

//package com.java2s;
/*/*from   ww w . j ava  2  s .c o m*/
 * Copyright (c) 2012. betterFORM Project - http://www.betterform.de
 * Licensed under the terms of BSD License
 */

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

public class Main {
    /**
     * Returns a date formatted according to ISO 8601 rules.
     *
     * @param date the date to format.
     * @return the formmatted date.
     */
    public static String formatISODate(Date date) {
        // always set time zone on formatter
        TimeZone timeZone = TimeZone.getDefault();
        boolean utc = TimeZone.getTimeZone("UTC").equals(timeZone) || TimeZone.getTimeZone("GMT").equals(timeZone);

        String pattern = utc ? "yyyy-MM-dd'T'HH:mm:ss'Z'" : "yyyy-MM-dd'T'HH:mm:ssZ";
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        format.setTimeZone(timeZone);

        StringBuffer buffer = new StringBuffer(format.format(date));
        if (!utc) {
            buffer.insert(buffer.length() - 2, ':');
        }

        return buffer.toString();
    }
}

Related

  1. formatISO8601Date(final Calendar date)
  2. formatISO8601Date(final java.util.Date date)
  3. formatISO8601DateWOTime(final java.util.Date date)
  4. formatIso8601ForCCTray(Date date)
  5. formatIsoDate(Date date)
  6. formatISODateTime(Date date)
  7. formatOneDecimal(int iNumber, int iDivisor)
  8. formattedIso8601DateString(Calendar pCal)
  9. formatTimestampIso8601(long millis, TimeZone zone)