1、为什么要用JDK8开始新增的时间表:
传统的时间类(Date,simpleDateFormat,Calender)存在如下问题:
①设计不合理,使用不方便,很多都被淘汰了
②都是可变对象,修改后会丢失最开始的时间信息
③线程不安全
④不能精确的纳秒,只能精确的毫秒
1秒=1000毫秒;1毫秒=1000微秒;1微秒=1000纳秒。
localDate、localTime、localDateTime(代替Calender)
localDate | 代表本地日期(年、月、日、星期) |
localTime | 代表本地时间(时、分、秒、纳秒) |
localDateTime | 代表本地的日期、时间(年、月、日、星期、时、分、秒、纳秒) |
他们获取对象的方案
方法名 | 示例 |
public static xxx now () ;获取系统当前时间对应的该对象 | localDate ld=localDate.now(); localTime lt=localTime.now(); localDateTime ldt=localDateTime.now(); |
import java.time.LocalDate;
public class Test1 {
public static void main(String[] args) {
//获取本地的日期对象
LocalDate ld= LocalDate.now();//年月日
System.out.println(ld);
//获取日期对象中的信息
int year =ld.getYear();//年
int month=ld.getMonthValue();//月
int day=ld.getDayOfMonth();//日
int dayofweek=ld.getDayOfWeek().getValue();//星期几
int dayofyear=ld.getDayOfYear();//一年中的第几天
System.out.println(year);
System.out.println(month);
System.out.println(day);
System.out.println(dayofweek);
System.out.println(dayofyear);
//2、直接修改某个信息:withyear,withmonth,withdayofmonth,withdayofyear
LocalDate ld1=ld.withYear(1998);
System.out.println(ld1);
System.out.println(ld);
//3、把某个信息加多少:pulsyears,pulsmonths,pulsdays,pulsweeks
LocalDate ld2=ld.plusDays(122);
System.out.println(ld);
System.out.println(ld2);
//4、把某个信息减多少:minusyears,minusmonths,minusdays,minusweeks
LocalDate ld3=ld.minusMonths(2);
System.out.println(ld3);
System.out.println(ld);
//5、获取指定日期的localDate对象:public static localDate of(int year,int month,int dayofmonth)
LocalDate ld4=LocalDate.of(2024,12,12);
LocalDate ld5=LocalDate.of(2025,01,01);
//6、判断两个日期对象是否相等,在前还是在后:equals isBefore isAfter
System.out.println(ld4.equals(ld5));
System.out.println(ld4.isAfter(ld5));
System.out.println(ld4.isBefore(ld5));
//7、可以把localDateTime转换成localDate,LocalTime
//public localDate toLocalDate();
//public localTime toLocalTime();
//public static localDateTime of(LocalDate date,localTime time);
}
}
ZoneId:时区
ZonedDateTime:带时区时间
import java.time.Clock;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Calendar;
import java.util.TimeZone;
public class ZoneTime {
public static void main(String[] args) {
//了解时区和带时区的时间
//1、Zoneid的常见方法
//public static Zoneid systemDefault():获取系统默认的时区
ZoneId zoneId=ZoneId.systemDefault();
System.out.println(zoneId.getId());
System.out.println(zoneId);
//public static set<String> getAvaiableZoneIds();获取Java支持的所有时区Ids
System.out.println(ZoneId.getAvailableZoneIds());
//public static ZoneId of(Strig ZoneId):把某个时区id封装成ZoneID对象
ZoneId zoneId1=ZoneId.of("America/Cuiaba");
//ZoneDateTime带时区时间
//public static ZoneDateTime now(ZoneId zone):获取某个时区的ZoneDateTime对象
ZonedDateTime now =ZonedDateTime.now(zoneId1);
//世界标准时间
ZonedDateTime now1=ZonedDateTime.now(Clock.systemUTC());
System.out.println(now1);
System.out.println(now);
//public static ZoneDateTime now():获取系统默认时区的ZoneDateTime对象
ZonedDateTime now2=ZonedDateTime.now();
System.out.println(now2);
Calendar instance=Calendar.getInstance(TimeZone.getTimeZone(zoneId1));
System.out.println(instance);//返回美国的时间
}
}
Instant:时间线上的某个时刻/时间戳(代替Date)
通过获取Instant对象可以获取此刻的时间,改时间是由两部分组成:从1970年01月01日 00:00:00 开始走到此刻的总秒数+不够1秒的纳秒数
方法名 | 说明 |
public static Instant now() | 获取当前时间的Instant对象(标准时间) |
public long getEpochsecod() | 获取从1970年1月1日00:00:00开始记录的秒数 |
public int getNano() | 从时间线开始,获取从第二个开始的纳秒数 |
pulsMillis pulsSeconds pulsNanos | 判断系列的方法 |
minusMillis minusSeconds minusNanos | 减少时间系列的方法 |
equals isBefore isAfter | 增加时间系列的方法 |
*作用:
①Instant对象的作用:做代码分析,或者记录用户操作的时间点。
②传统的Date类,只能精确的毫秒,并且是可变对象;
③新增的Instant类,可以精确到纳秒,并且是不可变对象,建议用Instant来代替Date
import java.time.Instant;
public class Instance {
public static void main(String[] args) {
//获取当前时间的Instant对象(标准时间)
//不可变对象
Instant now=Instant.now();
//获取从1970年1月1日00:00:00开始记录的秒数
//获取总秒数
long second=now.getEpochSecond();
System.out.println(second);
//从时间线开始,获取从第二个开始的纳秒数
//获取不够一秒的纳秒数
int nami=now.getNano();
System.out.println(nami);
Instant now1=now.plusMillis(111);
System.out.println(now1);
Instant now2=now.minusSeconds(12);
System.out.println(now2);
//Instant对象的作用:做代码分析,或者记录用户操作的时间点
Instant n1=Instant.now();
//代码执行....
Instant n2=Instant.now();
//传统的Date类,只能精确的毫秒,并且是可变对象;
// 新增的Instant类,可以精确到纳秒,并且是不可变对象,建议用Instant来代替Date
}
}
DateTimeFormatter:格式化器,用于时间的格式划,解析,线程安全(代替SimpleDateFormat:线程不安全)
方法名 | 说明 |
public static DateTimeFormatter ofpattern(时间格式) | 获取格式化器对象 |
public String Formate(时间对象) | 格式化时间 |
LocalDateTime 提供的格式化,解析时间的方法
方法名 | 说明 |
public String formate (DateTimeformatter formatter) | 格式化时间 |
public static LocalDateFormatter(parse charsquence text,DateTimeFormatter formatter) | 解析时间 |
rt java.time.LocalDate;
import java.time.LocalDateTime;
public class DateTimeFormatter {
public static void main(String[] args) {
//创建一个日期时间格式化对象出来
java.time.format.DateTimeFormatter formatter= java.time.format.DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
//对日期进行格式化
LocalDateTime now=LocalDateTime.now();
System.out.println(now);
String rs=formatter.format(now);//正向格式化
System.out.println(rs);
//格式化时间其实还有一种方法
String rs1=now.format(formatter);//反向格式化
System.out.println(rs1);
//解析时间:解析时间一般使用LocalDateTime提供的解析方法来解析
String rs2="2020年12月12日 12:12:12";
LocalDateTime lds=LocalDateTime.parse(rs2,formatter);
System.out.println(lds);
}
}
Period(一段时期)
可以用于计算两个LocalDate对象相差的年数,月数,天数
方法名 | 说明 |
public static period between(LocalDate start,LocalDate end) | 传入两个日期对象,得到Period对象 |
public int years() | 计算隔几年,返回 |
public int months() | 计算隔几月,返回 |
public int days() | 计算隔几天,返回 |
import java.time.LocalDate;
import java.time.Period;
public class Peroid {
public static void main(String[] args) {
//掌握period的作用:计算两个LocalDate对象相差的年数,月数,天数
//1、创建两个period 对象,封装两个日期对象
LocalDate start=LocalDate.of(2022,12,12);
LocalDate end=LocalDate.of(2025,1,1);
Period period=Period.between(start,end);
//通过两个period对象,获取两个日期对象相差的信息
int years= period.getYears();
int months= period.getDays();
int days= period.getMonths();
System.out.println(years);
System.out.println(days);
System.out.println(months);
}
}
Duration(持续时间)
可以用于计算两个日期对象相差的天数,小时数,分数,秒数,纳秒数,支持LocalTime,LocalDateTime,Instant等时间。
方法名 | 说明 |
public static Duration between(开始的时间对象1,截至的时间对象2) | 传入2个时间对象,得到Duration对象 |
public long toDays|() | 计算隔多少天,返回 |
public long toHours() | 计算隔多少小时,返回 |
public long toMinutes() | 计算隔多少分,返回 |
public long toSeconds() | 计算隔多少秒,返回 |
public long toMillis() | 计算隔多少毫秒,返回 |
public long toNanos() | 计算隔多少纳秒,返回 |
import java.time.LocalDateTime;
public class Duration {
public static void main(String[] args) {
//得到两个时间对象
LocalDateTime lds1 =LocalDateTime.of(2023,4,19,11,11,11);
LocalDateTime lds2 =LocalDateTime.of(2026,8,18,12,12,12);
//得到Duration对象
java.time.Duration duration=java.time.Duration.between(lds1,lds2);
//获取两个时间对象间隔的时期
long a=duration.toDays();
long b=duration.toHours();
long c=duration.getNano();
long d=duration.toMinutes();
long e=duration.getSeconds();
long f=duration.toMillis();
System.out.println(a);
System.out.println(b);
System.out.println(c);
System.out.println(d);
System.out.println(e);
System.out.println(f);
}
}