Here you can find the source of getIso8601DateString(long timestampWithMilliseconds)
Parameter | Description |
---|---|
timestampWithMilliseconds | timestamp in milliseconds |
public static String getIso8601DateString(long timestampWithMilliseconds)
//package com.java2s; //License from project: Open Source License import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Locale; import java.util.TimeZone; public class Main { private final static TimeZone timeZone = TimeZone.getTimeZone("UTC"); /**// w ww.ja va 2 s.c o m * Format and rounds a given timestamp to a ISO8601-compliant date string * @param timestampWithMilliseconds timestamp in milliseconds * @return ISO8601 date string */ public static String getIso8601DateString(long timestampWithMilliseconds) { Calendar calendar = Calendar.getInstance(timeZone); calendar.setTimeInMillis(timestampWithMilliseconds); calendar.set(Calendar.HOUR, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MILLISECOND, 0); calendar.setTimeZone(timeZone); return getIso8601DateFormat().format(calendar.getTime()); } /** * Formats a date to an ISO8601 date string * @param date the date * @return ISO8601 date string */ public static String getIso8601DateString(Calendar date) { return getIso8601DateFormat().format(date.getTime()); } /** * Generates an ISO8601-compliant date formatter * @return the date formatter */ private static DateFormat getIso8601DateFormat() { DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mmZ", Locale.ENGLISH); df.setTimeZone(timeZone); return df; } }