以下是您所要求的全部内容:
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Month;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;
public class Main {
public static void main(String[] args) {
// The current moment at UTC time-scale
Instant now = Instant.now();
System.out.println(now);
// The number of milliseconds from the epoch of 1970-01-01T00:00:00Z.
long millis = now.toEpochMilli();
System.out.println(millis);
// The number of seconds from the Java epoch of 1970-01-01T00:00:00Z.
long seconds = now.getEpochSecond();
System.out.println(seconds);
// Zone ID of Saudi Arabia
ZoneId zoneId = ZoneId.of("Asia/Riyadh");
// Get ZonedDateTime from Instant
ZonedDateTime zdt = now.atZone(zoneId);
System.out.println(zdt);
// Get the LocalDateTime part of this ZonedDateTime
LocalDateTime ldt = zdt.toLocalDateTime();
System.out.println(ldt);
// Get the LocalDate part of this ZonedDateTime
LocalDate today = zdt.toLocalDate();
System.out.println(today);
// An instant in the past
Instant past = LocalDateTime.of(LocalDate.of(2020, Month.APRIL, 10), LocalTime.MIDNIGHT)
.toInstant(ZoneOffset.ofHours(3));// Zone Offset of Saudi Arabia is UTC +3
System.out.println(past);
// No. of months
long months = ChronoUnit.MONTHS.between(past.atZone(zoneId), zdt);
System.out.println(months);
}
}
输出:
2020-08-27T08:45:31.927452Z
1598517931927
1598517931
2020-08-27T11:45:31.927452+03:00[Asia/Riyadh]
2020-08-27T11:45:31.927452
2020-08-27
2020-04-09T21:00:00Z
4