一、简介
- JAVA 8通过发布新的Date-Time API (JSR 310)来进一步加强对日期与时间的处理。
在旧版的日期时间 API 存在诸多问题,其中有:
- 非线程安全 − java.util.Date 是非线程安全的,所有的日期类都是可变的,这是Java日期类最大的问题之一。
- 设计很差 − Java的日期/时间类的定义并不一致,在java.util和java.sql的包中都有日期类,此外用于格式化和解析的类在java.text包中定义。java.util.Date同时包含日期和时间,而java.sql.Date仅包含日期,将其纳入java.sql包并不合理。另外这两个类都有相同的名字,这本身就是一个非常糟糕的设计。
- 时区处理麻烦 − 日期类并不提供国际化,没有时区支持,因此Java引入了java.util.Calendar和java.util.TimeZone类,但他们同样存在上述所有的问题。
Java 8 在 java.time 包下提供了很多新的 API。以下为两个比较重要的 API:
- Local(本地) − 简化了日期时间的处理,没有时区的问题。
- Zoned(时区) − 通过制定的时区处理日期时间。
新的java.time包涵盖了所有处理日期,时间,日期/时间,时区,时刻(instants),过程(during)与时钟(clock)的操作。
二、本地化日期时间 API(Local)
LocalDate/LocalTime 和 LocalDateTime 类可以在处理时区不是必须的情况
实例代码:
package org.jeecg;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
public class Java8Test {
public static void main(String args[]) {
System.out.println("======================================LocalDateTime 获取年月日时分秒");
LocalDateTime currentTime = LocalDateTime.now();
System.out.println("当前时间: " + currentTime);
int year = currentTime.getYear();
int monthValue = currentTime.getMonthValue();
int dayOfMonth = currentTime.getDayOfMonth();
int hour = currentTime.getHour();
int minute = currentTime.getMinute();
int seconds = currentTime.getSecond();
System.out.println("年:" + year + "月: " + monthValue + ", 日: " + dayOfMonth);
System.out.println("时:" + hour + "分: " + minute + ", 秒: " + seconds);
System.out.println("======================================LocalDate 获取年月日");
LocalDate localDate = currentTime.toLocalDate();
System.out.println("LocalDate: " + localDate);
int year1 = localDate.getYear();
int monthValue1 = localDate.getMonthValue();
int dayOfMonth1 = localDate.getDayOfMonth();
System.out.println("年:" + year1 + "月: " + monthValue1 + ", 日: " + dayOfMonth1);
System.out.println("======================================LocalTime 获取时分秒");
LocalTime localTime = currentTime.toLocalTime();
System.out.println("LocalDateTime: " + localTime);
int hour1 = localTime.getHour();
int minute1 = localTime.getMinute();
int second = localTime.getSecond();
System.out.println("时:" + hour1 + "分: " + minute1 + ", 秒: " + second);
System.out.println("======================================OF方法");
LocalDateTime of = LocalDateTime.of(2021, 11, 03, 21, 21, 21);
System.out.println("of: " + of);
LocalDate of1 = LocalDate.of(2021, 11, 03);
System.out.println("of1: " + of1);
LocalTime of2 = LocalTime.of(21, 21, 21);
System.out.println("of2: " + of2);
System.out.println("======================================parse方法");
//也可以用俩参数,传递 DateTimeFormatter
//DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime parse = LocalDateTime.parse("2021-11-01T21:21:21");
System.out.println("parse: " + parse);
LocalDate parse1 = LocalDate.parse("2021-11-01");
System.out.println("parse1: " + parse1);
LocalTime parse2 = LocalTime.parse("21:21:21");
System.out.println("parse2: " + parse2);
}
}
执行结果:
======================================LocalDateTime 获取年月日时分秒
当前时间: 2021-11-03T17:31:12.628296
年:2021月: 11, 日: 3
时:17分: 31, 秒: 12
======================================LocalDate 获取年月日
LocalDate: 2021-11-03
年:2021月: 11, 日: 3
======================================LocalTime 获取时分秒
LocalDateTime: 17:31:12.628296
时:17分: 31, 秒: 12
======================================OF方法
of: 2021-11-03T21:21:21
of1: 2021-11-03
of2: 21:21:21
======================================parse方法
parse: 2021-11-01T21:21:21
parse1: 2021-11-01
parse2: 21:21:21
三、使用时区的日期时间API(Zoned)
实例代码:
package org.jeecg;
import java.time.ZoneId;
import java.time.ZonedDateTime;
public class Java8Test {
public static void main(String args[]) {
ZoneId currentZone = ZoneId.systemDefault();
System.out.println("当期时区: " + currentZone);
ZonedDateTime now = ZonedDateTime.now();
System.out.println("当前时间: " + now);
String id = now.getZone().getId();
System.out.println("ZoneId: " + id);
}
}
执行结果:
当期时区: Asia/Shanghai
当前时间: 2021-11-03T17:36:42.836397+08:00[Asia/Shanghai]
ZoneId: Asia/Shanghai
注:以上内容仅提供参考和交流,请勿用于商业用途,如有侵权联系本人删除!