细节
- JDK8新增的时间对象都是不可变的;
- 如果我们修改了,减少了,增加了时间,那么调用者是不会发生改变的,会产生一个新的时间;
- ZoneId,Instant,ZonedDateTime类对应JDK7的Date类;
- DateTimeFormatter类对应SimpleDateFormat类;
ZoneId类
概念: 时区,用洲名/城市名或者国家名/城市名;
常用方法:
static Set<String> getAvailableZoneIds() 获取Java中支持的所有时区
static ZoneId systemDefault() 获取系统默认时区
static ZoneId of(String zoneId) 获取一个指定时区
例子
package com.caihongjia2.a01jdk8datedmeo;
import java.time.ZoneId;
import java.util.Set;
public class A01ZoneIdDemo01 {
public static void main(String[] args) {
/*
static Set<String> getAvailableZoneIds() 获取Java中支持的所有时区
static ZoneId systemDefault() 获取系统默认时区
static ZoneId of(String zoneId) 获取一个指定时区
*/
//1.获取Java中支持的所有时区
Set<String> zoneIds = ZoneId.getAvailableZoneIds();
System.out.println(zoneIds);//所有时区
System.out.println(zoneIds.size());//该集合的长度
//2.获取系统默认时区
ZoneId zoneId = ZoneId.systemDefault();
System.out.println(zoneId);//Asia/Shanghai
//3.获取一个指定时区
ZoneId zoneIdOf = ZoneId.of("America/Cuiaba");
System.out.println(zoneIdOf);
}
}
Instant类
概念: 时间戳,表示某一个固定的时间对象;
常用方法:
static Instant now() 获取当前时间的Instant对象 (标准时间)
static Instant ofxxxx(long epochMilli) 根据 (秒/毫秒/纳秒)获取Instant对象
ZonedRateTime atZone(ZoneId zone) 指定时区
boolean isXxx(Instant otherInstant) 判断系列的方法
Instant minusXxx(long millisToSubtract) 减少时间系列的方法(不改变原来数据,返回一个新的数据)
Instant plusXxx(long millisToSubtract) 增加时间系列的方法(不改变原来数据,返回一个新的数据)
具体方法:
package com.caihongjia2.a01jdk8datedmeo;
import jdk.swing.interop.SwingInterOpUtils;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
public class A02InstatDemo01 {
public static void main(String[] args) {
// static Instant now() 获取当前时间的Instant对象 (标准时间)
// static Instant ofxxxx(long epochMilli) 根据 (秒/毫秒/纳秒)获取Instant对象
// ZonedRateTime atZone(ZoneId zone) 指定时区
// boolean isXxx(Instant otherInstant) 判断系列的方法
// Instant minusXxx(long millisToSubtract) 减少时间系列的方法(不改变原来数据,返回一个新的数据)
// Instant plusXxx(long millisToSubtract) 增加时间系列的方法(不改变原来数据,返回一个新的数据)
//1.获取当前时间的Instant对象 (标准时间)
Instant now = Instant.now();
System.out.println(now);//2023-05-20T16:43:25.612652700Z
//2.根据 (秒/毫秒/纳秒)获取Instant对象
//以毫秒获取Instant对象;
Instant instant1 = Instant.ofEpochMilli(0L);
System.out.println(instant1);//1970-01-01T00:00:00Z
//以秒获取Instant对象;
Instant instant2 = Instant.ofEpochSecond(1L);
System.out.println(instant2);//1970-01-01T00:00:01Z
//以秒和纳秒获取Instant对象;参数1为秒,参数2为纳秒;(1秒=1000000000毫秒)
Instant instant3 = Instant.ofEpochSecond(1L, 1000000000L);
System.out.println(instant3);//1970-01-01T00:00:02Z
//3.指定时区
ZonedDateTime time = Instant.now().atZone(ZoneId.of("Asia/Shanghai"));
System.out.println(time);
//4.isXxx 用于时间的判断
Instant instant4 = Instant.ofEpochMilli(0L);
Instant instant5 = Instant.ofEpochMilli(1000L);
//isBefore(在...之前):判断调用者代表的时间是否在参数表示时间的前面;
boolean result1 = instant4.isBefore(instant5);
System.out.println(result1);//true
//isAfter(在...之后):判断调用者代表的时间是否在参数表示时间的后面;
boolean result2 = instant4.isAfter(instant5);
System.out.println(result2);//false
//5.Instant minusXxx(long millisToSubtract) 减少时间系列的方法(不改变原来数据,返回一个新的数据)
Instant instant6 = Instant.ofEpochMilli(3000L);
System.out.println(instant6);
//minusSeconds(long millisToSubtract) 减少参数内的时间,参数内为秒
Instant instant7 = instant6.minusSeconds(1);
System.out.println(instant7);
//minusMillis(long millisToSubtract) 减少参数内的时间,参数内为豪秒
Instant instant8 = instant6.minusMillis(1000L);
System.out.println(instant8);
//6.Instant plusXxx(long millisToSubtract) 增加时间系列的方法(不改变原来数据,返回一个新的数据)
//plusMillis(long millisToSubtract) 增加参数内的时间,参数内为豪秒
//plusSeconds(long millisToSubtract) 增加参数内的时间,参数内为秒
}
}
ZonedDateTime类
概念: 带有时区的时间对象;
常用方法:
static ZonedDateTime now() 获取当前时间的ZonedDateTime对象
static ZonedDateTime ofxxx(。。。) 获取指定时间的ZonedDateTime对象
ZonedDateTime withxxx(时间) 修改时间系列的方法
ZonedDateTime minusxxx(时间) 减少时间系列的方法
ZonedDateTime plusxxx(时间) 增加时间系列的方法
具体方法:
package com.caihongjia2.a01jdk8datedmeo;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
public class A03ZonedDateTimeDemo01 {
public static void main(String[] args) {
/*
static ZonedDateTime now() 获取当前时间的ZonedDateTime对象
static ZonedDateTime ofxxx(。。。) 获取指定时间的ZonedDateTime对象
ZonedDateTime withxxx(时间) 修改时间系列的方法
ZonedDateTime minusxxx(时间) 减少时间系列的方法
ZonedDateTime plusxxx(时间) 增加时间系列的方法
*/
//1.获取当前时间对象(带时区)
ZonedDateTime now = ZonedDateTime.now();
System.out.println(now);
//2.获取指定的时间对象(带时区)
//年月日时分秒纳秒时区方式指定
ZonedDateTime time1 = ZonedDateTime.of(2023,10,1,11,12,12,0, ZoneId.of("Asia/Shanghai"));
System.out.println(time1);
//通过Instant + 时区的方式指定获取时间对象
Instant instant = Instant.ofEpochMilli(0L);
ZoneId zoneId = ZoneId.of("Asia/Shanghai");
ZonedDateTime time2 = ZonedDateTime.ofInstant(instant, zoneId);//获得带时区的时间原点
System.out.println(time2);
//3.withxxx 修改时间系列的方法(年月日时分秒)
ZonedDateTime time3 = time2.withYear(2000);
System.out.println(time3);
//4.减少时间(年月日时分秒)
ZonedDateTime time4 = time3.minusYears(1);
System.out.println(time4);
//5.增加时间
ZonedDateTime time5 = time4.plusYears(1);
System.out.println(time5);
}
}
概念: 用于时间的格式化和解析的对象;
常用方法:
static DateTimeFormatter ofPattern(格式) 获取格式对象
String format(时间对象) 按照指定方法格式化
具体方法:
package com.caihongjia2.a01jdk8datedmeo;
import java.time.DateTimeException;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
public class A04DateTimeFormatterDemo01 {
public static void main(String[] args) {
/*
static DateTimeFormatter ofPattern(格式) 获取格式对象
String format(时间对象) 按照指定方法格式化
*/
//获得时间对象
ZonedDateTime time = Instant.now().atZone(ZoneId.of("Asia/Shanghai"));
//解析/格式化器
DateTimeFormatter dtf1 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss EE a");
//格式化
String format = dtf1.format(time);
System.out.println(format);
}
}