Java Date Format ISO formatIso8601(Date date)

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

Description

format Iso

License

Apache License

Declaration

public static String formatIso8601(Date date) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.text.SimpleDateFormat;

import java.util.Date;

public class Main {
    private static final String formatIso8601 = "yyyy-MM-dd'T'HH:mm:ssZ";

    public static String formatIso8601(Date date) {
        if (date == null)
            return "";

        // Add a colon 2 chars before the end of the string
        // to make it a valid ISO-8601 date.

        String str = format(date, getIso8601DateFormat());
        StringBuffer sb = new StringBuffer();
        sb.append(str.substring(0, str.length() - 2));
        sb.append(":");
        sb.append(str.substring(str.length() - 2));
        return sb.toString();
    }/*from www.  java  2s  . com*/

    /**
     * Returns a string the represents the passed-in date parsed
     * according to the passed-in format.  Returns an empty string
     * if the date or the format is null.
     **/
    public static String format(Date aDate, SimpleDateFormat aFormat) {
        if (aDate == null || aFormat == null) {
            return "";
        }
        synchronized (aFormat) {
            return aFormat.format(aDate);
        }
    }

    public static SimpleDateFormat getIso8601DateFormat() {
        return new SimpleDateFormat(formatIso8601);
    }
}

Related

  1. formatDateAsIsoString(Date date)
  2. formatDateISO(Date date)
  3. formatDateISO(Date date)
  4. formatISO(Integer integerValue)
  5. formatISO8601()
  6. formatIso8601(Date date)
  7. formatISO8601(Date date)
  8. formatISO8601(Date date)
  9. formatIso8601(Date date)