get Current ISO Date as OffsetDateTime - Java java.util

Java examples for java.util:Time

Description

get Current ISO Date as OffsetDateTime

Demo Code


//package com.java2s;
import java.time.Instant;

import java.time.OffsetDateTime;
import java.time.ZoneId;

import java.time.format.DateTimeFormatter;

public class Main {
    public static void main(String[] argv) throws Exception {
        System.out.println(getCurrentISODate());
    }/*  ww  w . j ava 2s . c  o  m*/

    /** ISO DATE format. It's used in ePub generator. */
    private static final DateTimeFormatter ISO_DATE = DateTimeFormatter
            .ofPattern("yyyy-MM-dd'T'HH:mm:ssZ");

    public static String getCurrentISODate() {
        return ms2OffsetDateTime(System.currentTimeMillis()).format(
                ISO_DATE);
    }

    /**
     * ms -> OffsetDateTime object.
     * @param ms millisecond
     * @return LocalDateTime
     */
    public static OffsetDateTime ms2OffsetDateTime(final long ms) {
        return OffsetDateTime.ofInstant(Instant.ofEpochMilli(ms),
                ZoneId.systemDefault());
    }
}

Related Tutorials