1.Java时间API的介绍

Java 8 吸收了 Joda-Time 的精华,以一个新的开始为 Java 创建优秀的 API。新的 java.time 中包含了所有关于

Java 8 中新日期时间API
本地日期(LocalTime)
本地时间(LocalDate)
本地日期时间(LocalDateTime)
时区(ZonedDateTime)
持续时间(Duration)

其中前三个是使用频率较高的几个

LocalDate、LocalTime、LocalDateTime 类是其中较重要的几个类,它们的实例,是不可变的对象,分别表示使用 ISO-8601日历系统的日期、时间、日期和时间。它们提供了简单的本地日期或时间,并不包含当前的时间信息,也不包含与时区相关的信息。

2.常用方法及应用案例一

方法 描述
now() 静态方法,根据当前时间创建对象
LocalDate 获取本地日期
LocalTime 获取本地时间
LocalDateTime 获取本地日期时间
 @Test
 public void Test1(){
 	//获取本地日期: xxx年-xx月-xx日
 	LocalDate localDate = LocalDate.now();
    //获取本地时间:  xxx时:xx分:xx秒
    LocalTime localTime = LocalTime.now();
    //获取本地日期时间: xxx年-xx月-xx日 xxx时:xx分:xx秒
    LocalDateTime localDateTime = LocalDateTime.now();

    System.out.println("本地今日日期:" + localDate);
    System.out.println("本地今日时间:" + localTime);
    System.out.println("本地今日日期时间:" + localDateTime);
    }

在这里插入图片描述

3.常用方法及应用案例二

方法 描述
of() 静态方法,根据指定日期/时间创建对象
getDayOfMonth() 获得月份天数(1-31)
getDayOfYear() 获得年份天数(1-366)
getDayOfWeek 获取星期几(DayOfWeek枚举值,英文)
getMonth 获得月份,返回一个Month枚举值
@Test
public void Test2(){
    //of():设置指定的年、月、日、时、分、秒、没有偏移量
    LocalDateTime ofTime = LocalDateTime.of(2021, 8, 11, 15, 45, 25);

    System.out.println("of设置的时间: " + ofTime);
    System.out.println("今天是这个月的第 " + ofTime.getDayOfMonth() + " 天");
    System.out.println("今天是今年的第 " + ofTime.getDayOfYear() + " 天");
    System.out.println("今天是(星期X): " + ofTime.getDayOfWeek());
    System.out.println("这个月是今年的第 " + ofTime.getMonth() + " 个月");
}

在这里插入图片描述

4.常用方法及应用案例三

方法 描述
getYear() 获得年份
getMonthValue() 获得月份(1-12)
getDayOfMonth() 获得月份天数(1-31)
getHour() 获得当前对象对应的小时
getMinute() 获得当前对象对应的分钟
getSecond() 获得当前对象对应的秒
@Test
public void Test3(){
   LocalDateTime nowTime = LocalDateTime.now();

   int year = nowTime.getYear();
   int month = nowTime.getMonthValue();
   int day = nowTime.getDayOfMonth();
   int hour = nowTime.getHour();
   int minute = nowTime.getMinute();
   int Seconds = nowTime.getSecond();

   System.out.println(nowTime);
   System.out.print("现在是 " + year + "年" + month + "月" + day + "日 ");
   System.out.println(hour + "时" + minute + "分" + Seconds + "秒");
}

在这里插入图片描述

5.常用方法及应用案例四

将月份天数、年份天数、月份、年份修改为指定的值并返回新的对象

方法 描述
withDayOfMonth 将月份天数修改为指定的值
withDayOfYear 将年份天数修改为指定的值
withMonth 将月份修改为指定的值
withYear 将年份修改为指定的值
@Test
public void Test4(){
    //获取现在的日期、时间
    LocalDateTime nowTime = LocalDateTime.now();
    System.out.println("获取的日期时间: " + nowTime + "\n");

    System.out.println("修改年份到1999年: " + nowTime.withYear(1999));

    System.out.println("修改月份到4月: " + nowTime.withMonth(4));

    System.out.println("修改月份天数到12日: " + nowTime.withDayOfMonth(12));

    System.out.println("今天是今年的第 " + nowTime.getDayOfYear() + " 天");
    System.out.println("修改年份天数到第10天: " + nowTime.withDayOfYear(10));

}

在这里插入图片描述

6.常用方法及应用案例五

向当前对象添加(plus)几天、几周、几个月、几年、几小时
向当前对象减去(minus)几天、几周、几个月、几年、几小时

方法 描述
plus/miuns + Seconds 加/减几秒钟
plus/miuns + Minutes 加/减几分钟
- -
plusHours() 添加几小时
plusDays() 添加几天
plusWeeks() 添加几周
plusMonths() 添加几个月
plusYears() 添加几年
- -
minusHours() 减去几小时
minusDays() 减去几天
minusWeeks() 减去几周
minusMonths() 减去几个月
minusYears() 减去几年
@Test
public void Test5(){
    //获取现在的日期、时间
    LocalDateTime nowTime = LocalDateTime.now();
    System.out.println("获取的日期时间: " + nowTime + "\n");

    //以下是分别在当前时间上加上500秒、10分钟、2小时
    System.out.println("加500秒:" + nowTime.plusSeconds(500));
    System.out.println("加10分钟:" + nowTime.plusMinutes(10));
    System.out.println("加2小时:" + nowTime.plusHours(2) + "\n");

    //以下是分别在当前时间上减去10天、10周、2个月、1年
    System.out.println("减去10天:" + nowTime.minusDays(10));
    System.out.println("减去10周:" + nowTime.minusWeeks(10));
    System.out.println("减去2个月:" + nowTime.minusMonths(2));
    System.out.println("减去1年:" + nowTime.minusYears(1));

}

在这里插入图片描述

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐