Java 日期时间


有两种表示时间的方式:

  • 人的时间,使用年、月、日、时、分、秒表示。
  • 机器的时间,通过计算时间和1970-01-01T00:00:00Z(epoch)相差的纳秒数表示。

本地日期处理

本地日期处理不考虑时间和时区。

下面是本地日期处理的例子:

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.Month;
import java.time.MonthDay;
import java.time.Year;
import java.time.YearMonth;
import java.time.temporal.TemporalAdjuster;
import java.time.temporal.TemporalAdjusters;

public class Date {
    public static void main(String[] args) {
        LocalDate date = LocalDate.of(2018, Month.OCTOBER, 20);
        TemporalAdjuster adj = TemporalAdjusters.next(DayOfWeek.MONDAY);
        LocalDate nextMon = date.with(adj);
        System.out.printf("date : %s, the next Wednesday is %s.%n", date, nextMon);
            // date : 2018-10-20, the next Wednesday is 2018-10-23.

        YearMonth date2 = YearMonth.of(2018, Month.FEBRUARY);
        System.out.printf("%s %n", date2);    // 2018-02

        MonthDay date3 = MonthDay.of(Month.FEBRUARY, 28);
        System.out.printf("%s %n", date3);    // --02-28

        Year date4 = Year.of(2018);
        System.out.printf("%s %n", date4);    // 2018
    }
}

本地时间处理

本地时间处理不考虑日期和时区。

下面是本地时间处理的例子:

import java.time.LocalTime;

public class Time {
    public static void main(String[] args) {
        LocalTime time = LocalTime.of(10, 5, 30, 20000);

        System.out.printf("%s:%s:%s.%s %n", time.getHour(),
                time.getMinute(), time.getSecond(), time.getNano());
                // 10:5:30.20000
    }
}

本地日期时间处理

本地日期时间处理不考虑时区。

下面是本地日期时间处理的例子:

import java.time.LocalDateTime;

public class DateTime {
    public static void main(String[] args) {
        LocalDateTime dateTime = LocalDateTime.of(2017, 10, 2, 10, 5, 30, 20000);

        System.out.printf("%s %n", dateTime);    // 2017-10-02T10:05:30.000020
    }
}

时区和时区偏移

时区由时区标识符和相对于UTC的时区偏移表示。时区标识符的格式为Region/City,例如时区标识符为Asia/Shanghai,时区偏移为+8:00。

下面是时区和时区偏移的例子:

import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;

public class Zone {
    public static void main(String[] args) {
        LocalDateTime dateTime = LocalDateTime.of(2018, 10, 2, 10, 5, 30, 20000);
        System.out.printf("%s %n", dateTime);    // 2018-10-02T10:05:30.000020

        System.out.println("zone : "  + ZoneId.systemDefault().getId());    // Asia/Shanghai
        ZoneId zone = ZoneId.of("Asia/Shanghai");
        ZonedDateTime z = ZonedDateTime.of(dateTime, zone);
        System.out.printf("%s %n", z);    // 2018-10-02T10:05:30.000020+08:00[Asia/Shanghai]

        ZoneOffset offset = ZoneOffset.of("+08:00");
        OffsetDateTime o = OffsetDateTime.of(dateTime, offset);
        System.out.printf("%s %n", o);    // 2018-10-02T10:05:30.000020+08:00 
    }
}

机器时间

Instant类用于生成表示机器时间的时间戳。

下面是机器时间的例子:

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;

public class TimePoint {
    public static void main(String[] args) {
        Instant timestamp = Instant.now();
        System.out.printf("%s %n", timestamp);    // 2018-05-01T06:18:21.966Z

        LocalDateTime dateTime = LocalDateTime.ofInstant(timestamp, ZoneId.systemDefault());
        System.out.printf("%s %n", dateTime);        // 2018-05-01T14:18:21.966
    }
}

时间解析和格式化

下面是时间解析和格式化的例子:

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class DateParse {
    public static void main(String[] args) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        
        LocalDateTime dateTime = LocalDateTime.parse("2017-10-01 13:10:08", formatter);
        System.out.printf("%s%n", dateTime);    // 2017-10-01T13:10:08

        LocalDateTime dateTime2 = LocalDateTime.of(2017, 10, 2, 10, 5, 30, 20000);
        String str = dateTime2.format(formatter);
        System.out.printf("%s%n", str);        // 2017-10-02 10:05:30
    }
}

日期时间计算

下面是日期时间计算的例子:

import java.time.Duration;
import java.time.Instant;
import java.time.LocalDate;
import java.time.Month;
import java.time.Period;

public class DateCalculate {
    public static void main(String[] args) {
        Instant i = Instant.now();
        System.out.printf("%s%n", i);    // 2018-05-01T06:21:14.081Z
        Duration d = Duration.ofSeconds(10);
        Instant i2 = i.plus(d);
        System.out.printf("%s%n", i2);    // 2018-05-01T06:21:24.081Z

        LocalDate today = LocalDate.now();
        LocalDate date = LocalDate.of(2000, Month.JANUARY, 1);
        Period p = Period.between(date, today);
        System.out.printf("%s years, %s months, %s days %n",
                p.getYears(), p.getMonths(), p.getDays());
                // 18 years, 4 months, 0 days 
    }
}