Java UTC Date getUtcTimestampAsString(Date date)

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

Description

Returns an ISO 8601 timestamp for the given date.

License

Open Source License

Parameter

Parameter Description
date The date to get the string representation

Return

The ISO 8601 string representation

Declaration

public static String getUtcTimestampAsString(Date date) 

Method Source Code


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

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

public class Main {
    private static final String UTC_TIMEZONE = "UTC";
    private static final String DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss'Z'";

    /**/*from   w w w. j  a v  a 2  s  .c  o  m*/
     * Returns the current UTC timestamp as string (ISO 8601).
     * 
     * @return UTC time as string representation
     */
    public static String getUtcTimestampAsString() {
        Calendar cal = Calendar.getInstance();
        DateFormat dfm = new SimpleDateFormat(DATE_FORMAT);
        dfm.setTimeZone(TimeZone.getTimeZone(UTC_TIMEZONE));
        return dfm.format(cal.getTime());
    }

    /**
     * Returns an ISO 8601 timestamp for the given date.
     *
     * @param date The date to get the string representation
     * @return The ISO 8601 string representation
     */
    public static String getUtcTimestampAsString(Date date) {
        DateFormat dfm = new SimpleDateFormat(DATE_FORMAT);
        dfm.setTimeZone(TimeZone.getTimeZone(UTC_TIMEZONE));
        return dfm.format(date);
    }
}

Related

  1. getUTCTime()
  2. getUTCTime(final Date inDate)
  3. getUtcTimestamp()
  4. getUtcTimestamp()
  5. getUTCTimestamp(Date date)
  6. getUTCTimeString4Digits(Date date)
  7. nowInUTC()
  8. nowUtc(String pattern)
  9. nowUTCString()