Java8 DateTime API

一、Java8时间处理

由于java.util.Date 设定为可变类型,以及 SimpleDateFormat 的非线程安全使其应用非常受限,优先推荐使用java.time 包下的所有类因为其实不可变类型而且线程安全的

类名称 描述
Instant 时间戳
Duration 持续时间,时间差
LocalData 只包含日期,例如:2011-03-12
LocalTime 只包含时间,例如:21:35:10
LocalDateTime 包含时间和日期,例如:2011-03-12 21:35:10
Period 时间段
ZoneOffset 时间偏移量,比如:+8:00
ZoneDateTime 带时区的时间
Clock 时钟,比如获取目前美国纽约的时间

1. 获取当前日期

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class Data {
public static void main(String[] args) {
//获取当日日期
System.out.println(LocalDate.now()); //输出:2022-08-03
//获取当前时间
System.out.println(LocalTime.now()); //输出:11:40:43.318
//获取时间戳
Clock clock = Clock.systemUTC();
Clock defaultClock = Clock.systemDefaultZone();
System.out.println("Clock : " + clock.millis());//输出当前系统时钟的时间戳,并设置为UTC:1659509339722
System.out.println("Clock : " + defaultClock.millis());//当前时区下的日期时间信息
Instant timestamp = Instant.now();
System.out.println(timestamp.toEpochMilli());//输出当前的时间戳:1659509339722
}
}

2. 获取年月日信息

1
2
3
4
5
6
7
public class Data {
public static void main(String[] args) {
System.out.println(today.getYear()); //输出:2022
System.out.println(today.getMonthValue()); //输出:08
System.out.println(today.getDayOfMonth()); //输出:03
}
}

3. 返回自定义日期对象

1
2
3
4
5
6
public class Data {
public static void main(String[] args) {
LocalDate customData = LocalDate.of(2021,10,9);//返回自定义的日期对象
System.out.println(customData); //输出:2021-10-9
}
}

4. 判断两日期是否相等

1
2
3
4
5
6
7
public class Data {
public static void main(String[] args) {
LocalDate customData = LocalDate.of(2021,10,9);//返回自定义的日期对象
LocalDate today = LocalDate.now();
System.out.println(today.equals(customData));
}
}

5. 判断像生日这种周期性事件(仅匹配同月同日不管年份)

1
2
3
4
5
6
7
8
public class Data {
public static void main(String[] args) {
LocalDate customData = LocalDate.of(2021,10,9);//返回自定义的日期对象
MonthDay birthday = MonthDay.of(customData.getMonth(),customData.getDayOfMonth());
MonthDay currentMonthDay = MonthDay.from(today);
System.out.println(currentMonthDay.equals(birthday));//输出:false
}
}

6. 判断日期是早于还是晚于另一个日期

1
2
3
4
5
6
7
8
public class Data {
public static void main(String[] args) {
LocalDate today = LocalDate.now();
LocalDate tomorrow = LocalDate.of(2018,2,6);
System.out.println(tomorrow.isAfter(today));//输出tomorrow是否在今天后:false
System.out.println(tomorrow.isBefore(today));//输出tomorrow是否在今天后:false
}
}

7. 判断平闰年

1
2
3
4
5
6
public class Data {
public static void main(String[] args) {
LocalDate today = LocalDate.now();
System.out.println(today.isLeapYear());//输出是否闰年
}
}

8. 获取两个日期之间相差的天数和月数

1
2
3
4
5
6
7
8
public class Data {
public static void main(String[] args) {
LocalDate today = LocalDate.now();
LocalDate releaseDate = LocalDate.of(2018, 12, 14);
Period monthsNum = Period.between(today, releaseDate);
System.out.println(periodToNextJavaRelease.getMonths());//输出today->releaseDate相差的月份
}
}

9. 时间处理

1
2
3
4
5
6
7
8
9
public class Data {
public static void main(String[] args) {
LocalDate today = LocalDate.now();
LocalTime newTime = LocalTime.now().plusHours(3);
System.out.println(newTime); //输出当前时间+3小时的时间:11:43:43.318
LocalTime newTime=LocalTime.now().minusHours(3);
System.out.println(newTime); //输出当前时间-3小时的时间:11:37:43.318
}
}

10. 日期处理

1
2
3
4
5
6
7
8
public class Data {
public static void main(String[] args) {
LocalDate nextWeek = LocalDate.now().plus(1, ChronoUnit.WEEKS);
System.out.println(newTime); //输出当前日期+1个星期:2022-08-10
LocalDate previousYear = LocalDate.now().minus(1, ChronoUnit.YEARS);
System.out.println(previousYear); //输出一年前的星期:2021-08-03
}
}

11. 时区处理

1
2
3
4
5
6
7
8
9
public class Data {
public static void main(String[] args) {
//本时区的时间转换成另一个时区的时间-时区处理
ZoneId america = ZoneId.of("America/New_York");
LocalDateTime localtDateAndTime = LocalDateTime.now();
ZonedDateTime dateAndTimeInNewYork = ZonedDateTime.of(localtDateAndTime, america);
System.out.println(dateAndTimeInNewYork);//输出转换之后的时区
}
}

12. 格式化字符串日期

1
2
3
4
5
6
7
public class Data {
public static void main(String[] args) {
//格式化字符串日期
LocalDate formatDate = LocalDate.parse("2022-01-03",DateTimeFormatter.ISO_LOCAL_DATE);
System.out.println(formatDate);//根据类型输出日期,只能格式化Str为ISO_LOCAL_DATE的日期:2022-01-03
}
}

13. 日期转字符串

1
2
3
4
5
6
7
public class Data {
public static void main(String[] args) {
LocalDateTime date = LocalDateTime.now();
String str = date.format(DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss"));
System.out.println(str);//输出日期转字符串:2022-08-03 15:02:16
}
}

14. 字符串转日期

1
2
3
4
5
6
7
8
9
10
11
public class Data {
public static void main(String[] args) {
String str="2022-08-03 15:02:16";
DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
//也可用自带的'ISO_LOCAL_DATE'枚举
LocalDate date1 = LocalDate.parse(str,DateTimeFormatter.ISO_LOCAL_DATE);
LocalDate date2 = LocalDate.parse(str,format);
System.out.println(date1);//输出字符串转日期:2022-08-03 15:02:16
System.out.println(date2);//输出字符串转日期:2022-08-03 15:02:16
}
}

15. 日期转时间戳

1
2
3
4
5
6
7
8
public class Data {
public static void main(String[] args) {
//日期转时间戳
LocalDateTime today = LocalDateTime.now();
Long timestamp=today.atStartOfDay(ZoneId.systemDefault()).toInstant().toEpochMilli();

}
}

16. 时间戳转日期

1
2
3
4
5
6
7
8
public class Data {
public static void main(String[] args) {
//日期转时间戳
long milli=1663830008227L;
LocalDate newDate = Instant.ofEpochMilli(milli).atZone(ZoneId.systemDefault()).toLocalDate();

}
}