Java UTC Date formatUTC(Date time)

Here you can find the source of formatUTC(Date time)

Description

Date-Time formatter that corresponds to the standard UTC time as used in XML

License

Open Source License

Parameter

Parameter Description
time a parameter

Declaration

public static final String formatUTC(Date time) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.Calendar;
import java.util.Date;

public class Main {
    /**/*from w ww  .j  a va2 s . c  o m*/
     * Date-Time formatter that corresponds to the standard UTC time as used in XML
     * @param time
     * @return
     */
    public static final String formatUTC(Date time) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(time);
        StringBuffer buf = new StringBuffer(20);
        appendDate(buf, cal).append('T');
        appendInt2(buf, cal.get(Calendar.HOUR_OF_DAY)).append(':');
        appendInt2(buf, cal.get(Calendar.MINUTE)).append(':');
        appendInt2(buf, cal.get(Calendar.SECOND)).append('Z');
        return buf.toString();
    }

    /**
     * Append date (in YYYY-MM-DD format) to specified buffer.
     */
    private static StringBuffer appendDate(StringBuffer buf, Calendar cal) {
        buf.append(cal.get(Calendar.YEAR)).append('-');
        appendInt2(buf, cal.get(Calendar.MONTH) + 1).append('-');
        appendInt2(buf, cal.get(Calendar.DAY_OF_MONTH));
        return buf;
    }

    /**
     * Same as {@link #formatInt2(int)} but appends to specified buffer.
     */
    private static StringBuffer appendInt2(StringBuffer buf, int n) {
        if (n < 10) {
            buf.append('0');
        }
        buf.append(n);
        return buf;
    }
}

Related

  1. createUtcCalendar()
  2. formatUTC(Date time)
  3. getPlatypusConainerClass(Class aLayoutClass)
  4. getUTC()
  5. getUTC(String beforeFormart, String afterFormart, String dateStr)