Java Date Format ISO getIso8601DateString(long timestampWithMilliseconds)

Here you can find the source of getIso8601DateString(long timestampWithMilliseconds)

Description

Format and rounds a given timestamp to a ISO8601-compliant date string

License

Open Source License

Parameter

Parameter Description
timestampWithMilliseconds timestamp in milliseconds

Return

ISO8601 date string

Declaration

public static String getIso8601DateString(long timestampWithMilliseconds) 

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.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;
    }
}

Related

  1. getIso8601DateFormat()
  2. getISO8601DateFormat()
  3. getIso8601DateFormat()
  4. getISO8601DateFormat(TimeZone tz, String mask)
  5. getISO8601DateString(Date d)
  6. getIso8601DayDateFormat()
  7. getISO8601FormatDate(String in)
  8. getIso8601String(Date d)
  9. getISO8601String(Date date)