Java UTC Date formatUTC(Date time)

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

Description

Date-Time formater 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   www .jav  a 2  s .  c  o  m*/
     * Date-Time formater that corresponds to the standard UTC time as used in XML
     * @param time
     * @return
     */
    public static final String formatUTC(Date time) {
        // This function needs optimising. It has a too high object churn.
        Calendar c = null;
        if (c == null)
            c = Calendar.getInstance();
        c.setTime(time);
        return c.get(Calendar.YEAR) + "-"
                + formatInt2(c.get(Calendar.MONTH) + 1) + "-"
                + formatInt2(c.get(Calendar.DAY_OF_MONTH)) + "T"
                + formatInt2(c.get(Calendar.HOUR_OF_DAY)) + ":"
                + formatInt2(c.get(Calendar.MINUTE)) + ":"
                + formatInt2(c.get(Calendar.SECOND)) + "Z";

    }

    /**
     * Formats an integer to 2 digits, as used for example in time.
     * I.e. a 0 gets printed as 00. 
     **/
    public static final String formatInt2(int n) {
        if (n < 10) {
            return "0" + n;
        } else {
            return Integer.toString(n);
        }
    }
}

Related

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