Java Instant to Date toDate(Instant moment)

Here you can find the source of toDate(Instant moment)

Description

to Date

License

Open Source License

Declaration

public static Date toDate(Instant moment) 

Method Source Code


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

import java.time.Instant;

import java.time.LocalDateTime;

import java.time.ZoneId;

import java.util.Date;

public class Main {
    public static Date toDate(Instant moment) {
        if (moment == null) {
            return null;
        }//from ww  w .  j  av  a2s.  com
        Date date = Date.from(moment);
        return date;
    }

    public static Date toDate(LocalDateTime localDateTime) {
        if (localDateTime == null) {
            return null;
        }
        Instant moment = toInstant(localDateTime);
        Date date = toDate(moment);
        return date;
    }

    public static Instant toInstant(Date date) {
        if (date == null) {
            return null;
        }
        Instant moment = Instant.ofEpochMilli(date.getTime());
        return moment;
    }

    public static Instant toInstant(LocalDateTime localDateTime) {
        if (localDateTime == null) {
            return null;
        }
        Instant moment = localDateTime.atZone(ZoneId.systemDefault()).toInstant();
        return moment;
    }
}

Related

  1. toDate(Instant instant)