long To Date String via LocalDateTime - Java java.util

Java examples for java.util:Time

Description

long To Date String via LocalDateTime

Demo Code


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

import java.time.LocalDateTime;

import java.time.ZoneId;

import java.time.format.DateTimeFormatter;

public class Main {
    public static void main(String[] argv) throws Exception {
        long unixMilliSec = 2;
        System.out.println(longToStr(unixMilliSec));
    }//w w  w  .j  a va  2  s.  c om

    public static String longToStr(final long unixMilliSec) {
        return longToStr(unixMilliSec, "yyyy-MM-dd(E)");
    }

    public static String longToStr(final long unixMilliSec,
            final String format) {
        return ms2LocalDateTime(unixMilliSec).format(
                DateTimeFormatter.ofPattern(format));
    }

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

Related Tutorials