莱芜环保网站,1+手机官网首页,长沙人才招聘网长沙58同城招聘网,ps切图做网站新增的日期和时间
为什么要学习新增的日期和时间 1、代替Calendar LocalDate#xff1a;年、月、日 LocalTime#xff1a;时、分、秒 LocalDateTime#xff1a;年、月、日、时、分、秒 ZoneId#xff1a;时区 ZoneldDatetime#xff1a;带时区的时间 2、代替Date Instan… 新增的日期和时间
为什么要学习新增的日期和时间 1、代替Calendar LocalDate年、月、日 LocalTime时、分、秒 LocalDateTime年、月、日、时、分、秒 ZoneId时区 ZoneldDatetime带时区的时间 2、代替Date Instant时间戳/时间线 3、代替SimpleDateForamt DateTimeFormatter用于时间的格式化和解析 4、其他补充 Period时间间隔年、月、日 Duration时间间隔时、分、秒纳秒 LocalDate、LocalTime、LocalDateTime
LocalDate代表本地日期年、月、日、星期
LocalTime代表本地时间时、分、秒、纳秒
LocalDateTime代表本地日期、时间年、月、日、星期、时、分、秒、纳秒 它们获取对象的方案
方法名示例public static Xxxx now获取系统当前时间对应的该对象 LocalDate ld LocaDate.now LocalTime ld LocalTime.now LocalDateTime ld LocalDateTime.now public static Xxxx of…获取指定时间的对象 LocalDate localDate1 LocalDate.of(2099,11,11) LocalTime localTime1 LocalTime.of(9,8,59) LocalDateTime localDateTime1 LocalDateTime.of(2025,11,16,14,30,01) LocalDate
LocalDate的常用API都是处理年、月、日、星期相关的
import java.time.LocalDate;public class Test {public static void main(String[] args){// 获取本地日期对象不可变对象LocalDate ld LocalDate.now(); // 年 月 日System.out.println(ld);// 获取日期对象中的信息int year ld.getYear(); // 年int month ld.getMonthValue(); // 月1-12int day ld.getDayOfMonth(); // 日int dayOfYear ld.getDayOfYear(); // 一年中的第几天int dayOfWeek ld.getDayOfWeek().getValue(); // 星期几// 直接修改某个信息withYear、withMonth、withDayOfMonth、withDayOfYearLocalDate ld2 ld.withYear(2099);LocalDate ld3 ld.withMonth(12);System.out.println(ld2);System.out.println(ld3);System.out.println(ld);// 把某个信息加多少plusYears、plusMonths、plusDays、plusWeeksLocalDate ld4 ld.plusYears(2);LocalDate ld5 ld.plusMonths(2);// 把某个信息减多少minusYears、minusMonths、minusDays、minusWeeksLocalDate ld6 ld.minusYears(2);LocalDate ld7 ld.minusMonths(2);// 获取指定日期的LocalDate对象LocalDate ld8 LocalDate.of(2099,12,12);LocalDate ld9 LocalDate.of(2099,12,12);// 判断2个日期对象是否相等再前还是再后equals isBefore isAfterSystem.out.println(ld8.equals(ld9));System.out.println(ld8.isAfter(ld));System.out.println(ld8.isBefore(ld));}
}LocalTime
LocalTime的常用API都是处理时、分、秒、纳秒相关的
import java.time.LocalTime;public class Test {public static void main(String[] args){// 获取本地时间对象LocalTime lt LocalTime.now(); // 时 分 秒System.out.println(lt);// 获取时间中的信息int hour lt.getHour(); // 时int minute lt.getMinute(); // 分int second lt.getSecond(); // 秒int nano lt.getNano(); // 纳秒// 修改时间LocalTime lt2 lt.withHour(10);LocalTime lt3 lt.withMinute(10);LocalTime lt4 lt.withSecond(10);LocalTime lt5 lt.withNano(10);// 加多少:plusHours、plusMinutes、plusSeconds、plusNanosLocalTime lt6 lt.plusHours(10);LocalTime lt7 lt.plusMinutes(10);// 减多少minusHours、minusMinutes、minusSeconds、minusNanosLocalTime lt8 lt.minusHours(10);LocalTime lt9 lt.minusMinutes(10);// 获取指定时间的LocalTime对象LocalTime lt10 LocalTime.of(12,12,12);LocalTime lt11 LocalTime.of(12,12,12);// 判断2个日期对象是否相等再前还是再后equals isBefore isAfterSystem.out.println(lt10.equals(lt11));System.out.println(lt10.isAfter(lt));System.out.println(lt10.isBefore(lt));}
} LocalDateTime
LocalTime的常用API都是处理年、月、日、时、分、秒、纳秒相关的
import java.time.LocalDateTime;public class Test {public static void main(String[] args){// 获取本地日期、时间对象LocalDateTime ldt LocalDateTime.now(); // 年 月 日 时 分 秒System.out.println(ldt);// 获取日期、时间中的信息int year ldt.getYear(); // 年int month ldt.getMonthValue(); // 月1-12int day ldt.getDayOfMonth(); // 日int dayOfYear ldt.getDayOfYear(); // 一年中的第几天int dayOfWeek ldt.getDayOfWeek().getValue(); // 星期几int hour ldt.getHour(); // 时int minute ldt.getMinute(); // 分int second ldt.getSecond(); // 秒int nano ldt.getNano(); // 纳秒// 修改时间LocalDateTime ldt2 ldt.withYear(2099);LocalDateTime ldt3 ldt.withMinute(59);// 加多少LocalDateTime ldt4 ldt.plusYears(2);LocalDateTime ldt5 ldt.plusMinutes(3);// 减多少LocalDateTime ldt6 ldt.minusYears(2);LocalDateTime ldt7 ldt.minusMinutes(3);// 获取指定日期、时间的LocalDateTime对象LocalDateTime ldt8 LocalDateTime.of(2099,12,12,12,12,12);LocalDateTime ldt9 LocalDateTime.of(2099,12,12,12,12,12);// 判断2个日期、时间对象是否相等再前还是再后equals isBefore isAfterSystem.out.println(ldt8.equals(ldt9));System.out.println(ldt8.isAfter(ldt));System.out.println(ldt8.isBefore(ldt));}
} Zoneld、ZonedDateTime
Zoneld代表时区id
Zoneld时区的常见方法 ZonedDateTime
ZonedDateTime带时区时间的常见方法
import java.time.Clock;
import java.time.ZoneId;
import java.time.ZonedDateTime;public class Test {public static void main(String[] args){// Zoneld的常见方法// 获取系统默认的时区ZoneId zoneId ZoneId.systemDefault();System.out.println(zoneId.getId());System.out.println(zoneId);// 获取Java支持的全部时区idSystem.out.println(ZoneId.getAvailableZoneIds());// 把某个时区id封装成Zoneid对象ZoneId zoneId1 ZoneId.of(America/New_York);// ZonedDateTime带时区的时间// 获取系统某个时区的ZondDateTime对象ZonedDateTime now ZonedDateTime.now(zoneId1);System.out.println(now);// 世界标准时间ZonedDateTime now1 ZonedDateTime.now(Clock.systemUTC());System.out.println(now1);// 获取系统默认时区的ZondDateTime对象ZonedDateTime now2 ZonedDateTime.now();System.out.println(now2);}
} Instant 时间线上的某个时刻/时间戳
通过获取Instant的对象可以拿到此刻的时间该时间由两部分组成从1970-01-01 00:00:00开始走到此刻的总秒数 不够一秒的纳秒数
import java.time.Instant;public class Test {public static void main(String[] args){// 创建Instant对象获取此刻时间信息Instant now Instant.now();// 获取总秒数long second now.getEpochSecond();System.out.println(second);// 不够一秒的纳秒数int nano now.getNano();System.out.println(nano);// 增加、减少时间Instant instant1 now.plusNanos(111);Instant instant2 now.minusMillis(2);System.out.println(now);// Instant对象的作用做代码的性能分析或者记录用户的操作时间点Instant now1 Instant.now();// 代码行……Instant now2 Instant.now();}
}
作用可以用来记录代码的执行时间或者记录用户的操作某个时间时间点。传统的Date类只能精确到毫秒并且是可变对象新增的Instant类可以精确到纳秒并且是不可变对象推荐用Instant代替Date。 DateTimeFormatter
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;public class Test {public static void main(String[] args) {// 创建一个日期时间格式化器对象DateTimeFormatter formatter 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 rs2 now.format(formatter);System.out.println(rs2);// 解析时间String dateStr 2029年12月12日 12:12:12;LocalDateTime ldt LocalDateTime.parse(dateStr,formatter);System.out.println(ldt);}
} Period、Duration
Period一段时期
可以用于计算两个LocalDate对象相差的年数、月数、天数。
import java.time.LocalDate;
import java.time.Period;public class Test {public static void main(String[] args) {LocalDate start LocalDate.of(2029,8,10);LocalDate end LocalDate.of(2029,12,15);// 创建Period对象封装两个日期对象Period period Period.between(start,end);// 通过Period对象获取两个日期对象相差的信息System.out.println(period.getYears());System.out.println(period.getMonths());System.out.println(period.getDays());}
} Duration持续时间
可以用于计算两个时间对象相差的天数、小数数、分数、秒数、纳秒数支持LocalTime、LocalDateTime、Instant等时间。
import java.time.Duration;
import java.time.LocalDateTime;public class Test {public static void main(String[] args) {LocalDateTime start LocalDateTime.of(2029,11,11,11,10,10);LocalDateTime end LocalDateTime.of(2029,11,11,11,11,11);// 创建Duration对象封装两个时间对象Duration duration Duration.between(start,end);// 通过Duration对象获取两个时间对象相差的信息System.out.println(duration.toDays());System.out.println(duration.toHours());System.out.println(duration.toMinutes());System.out.println(duration.toMillis());System.out.println(duration.toNanos());}
}