java8 日期和时间

新的日期API:LocalDate

//of
LocalDate date = LocalDate.of(2022, 3, 18); 
//parse
LocalDate date1 = LocalDate.parse("2022-03-18"); 

int year = date.getYear(); 
Month month = date.getMonth(); 
int day = date.getDayOfMonth(); 
DayOfWeek dow = date.getDayOfWeek(); 
int len = date.lengthOfMonth(); 
boolean leap = date.isLeapYear(); 

新的时间API:LocalTime

//of
LocalTime time = LocalTime.of(13, 45, 20); 
//parse
LocalTime time1 = LocalTime.parse("13:45:20");

int hour = time.getHour(); 
int minute = time.getMinute(); 
int second = time.getSecond(); 

合并日期和时间API:LocalDateTime

LocalDate date = LocalDate.of(2022, 3, 18); 

LocalTime time = LocalTime.of(13, 45, 20); 

LocalDateTime dt1 = LocalDateTime.of(2022, Month.MARCH, 18, 13, 45, 20); 

LocalDateTime dt2 = LocalDateTime.of(date, time); 

LocalDateTime dt3 = date.atTime(13, 45, 20); 

LocalDateTime dt4 = date.atTime(time); 

LocalDateTime dt5 = time.atDate(date); 

时间段API:Duration

  LocalTime time1 = LocalTime.of(12, 30, 30);
  LocalTime time2 = LocalTime.of(12, 40, 30);
  Duration d1 = Duration.between(time1, time2);
  System.out.println(d1.toMillis());

  LocalDateTime dateTime1 = LocalDateTime.of(2022, 2, 12, 12, 30, 30);
  LocalDateTime dateTime2 = LocalDateTime.of(2022, 2, 12, 12, 40, 30);
  Duration d2 = Duration.between(dateTime1, dateTime2);
  System.out.println(d2.getSeconds());

  //十分钟后
  Duration tenMinutes = Duration.ofMinutes(10);
  Duration tenMinutes1 = Duration.of(10, ChronoUnit.MINUTES);

时间段API:Period


  //计算两个日期间隔天数
  Period period = Period.between(LocalDate.of(2022, 3, 12), LocalDate.of(2022, 3, 22));
  System.out.println(period.getDays());

  //十天之后
  Period tenDays = Period.ofDays(10);
  //三周之后
  Period threeWeeks = Period.ofWeeks(3);
  //两年六个月零1天后
  Period twoYearsSixMonthsOneDay = Period.of(2, 6, 1);
        
     

操纵日期

//以比较直观的方式操纵LocalDate的属性 
LocalDate date1 = LocalDate.of(2022, 3, 18); 
//修改年份
LocalDate date2 = date1.withYear(2021); 
//修改天
LocalDate date3 = date2.withDayOfMonth(25); 
//修改月份
LocalDate date4 = date3.with(ChronoField.MONTH_OF_YEAR, 9);

//以相对方式修改LocalDate对象的属性 
LocalDate d1 = LocalDate.of(2022, 3, 18); 
//增加一周
LocalDate d2 = date1.plusWeeks(1); 
//减去3年
LocalDate d3 = date2.minusYears(3); 
//增加三个月
LocalDate d4 = date3.plus(6, ChronoUnit.MONTHS); 

使用 TemporalAdjuster修改日期

  LocalDate date1 = LocalDate.of(2022, 3, 18);
  //获取下一个星期天
  LocalDate date2 = date1.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY));
  //获取这个月的最后一天
  LocalDate date3 = date2.with(TemporalAdjusters.lastDayOfMonth());

格式化


LocalDate date = LocalDate.of(2022, 3, 18);
String s1 = date.format(DateTimeFormatter.BASIC_ISO_DATE);
String s2 = date.format(DateTimeFormatter.ISO_LOCAL_DATE);
System.out.println(s1);
System.out.println(s2);

//按照某个模式创建DateTimeFormatter
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
LocalDate date1 = LocalDate.of(2022, 3, 18);
String formattedDate = date1.format(formatter);
LocalDate date2 = LocalDate.parse(formattedDate, formatter);
System.out.println(formattedDate);

//创建一个本地化的DateTimeFormatter
DateTimeFormatter italianFormatter =
     DateTimeFormatter.ofPattern("d. MMMM yyyy", Locale.ITALIAN);
LocalDate date3 = LocalDate.of(2022, 3, 18);
String formattedDate1 = date3.format(italianFormatter);
System.out.println(formattedDate1);

// 构造一个DateTimeFormatter
DateTimeFormatter dateTimeFormatter = new DateTimeFormatterBuilder()
       .appendText(ChronoField.DAY_OF_MONTH)
       .appendLiteral(". ")
       .appendText(ChronoField.MONTH_OF_YEAR)
       .appendLiteral(" ")
       .appendText(ChronoField.YEAR)
       .parseCaseInsensitive()
       .toFormatter(Locale.ITALIAN);

时区

 ZoneId romeZone = ZoneId.of("Europe/Rome");
 ZoneId zoneId = TimeZone.getDefault().toZoneId();
        
 //为时间点添加时区信息
 LocalDate date = LocalDate.of(2022, Month.MARCH, 18);
 ZonedDateTime zdt1 = date.atStartOfDay(romeZone);
        
 LocalDateTime dateTime = LocalDateTime.of(2022, Month.MARCH, 18, 13, 45);
 ZonedDateTime zdt2 = dateTime.atZone(romeZone);
        
 Instant instant = Instant.now();
 ZonedDateTime zdt3 = instant.atZone(romeZone);

《人体使用手册》

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

古智云开

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值