Java中LocalDateTime使用及LocalDate、LocalTime详解

1、JDK.8日期和时间API概述

新增的API严格区分了时刻、本地日期、本地时间,并且,对日期和时间进行运算更加方便。

其次,新API的类型几乎全部是不变类型(和String的使用类似),可以放心使用不必担心被修改。jdk1.8 之前的 SimpleDateFormat 是线程不安全的。DateTimeFormatter是线程安全的

LocalTime 用于时刻的计算(带有毫秒),LocalDate 用于日期的计算,LocalDateTime 用于日期+时刻 的计算。

【1、时间和日期类:LocalDateTime、LocalDate、LocalTime】

详解地址:https://blog.csdn.net/tangshiyilang/article/details/131832013

【2、带时区的日期和时间:ZonedDateTime】

【3、时刻:Instant是时间线上的一个瞬时点。】

Java中Date类的toInstant()方法用于将Date对象转换为Instant对象。在转换过程中会创建一个Instant,用于表示时间轴上与此日期相同的点。

【4、时区:ZoneId,ZoneOffSet】

java.time.ZoneOffset.ofHours(int hours)方法使用以小时为单位的偏移量获取 ZoneOffset 的实例

ZoneId.systemDefault():获取时区名称 输出:Asia/Shanghai

【5、时间间隔:Period,Duration】

6、Month月份

7、Week周

8、Clock:

2、LoalDateTime使用详解

2.1、LocalDateTime基本用法

public static void main(String[] args) {
		//测试时间点:2023-04-25T00:42:59.089
		LocalDateTime now = LocalDateTime.now();
           //1、获得年
		int year = now.getYear();//输出2023 
           //2、获得英文表达的月
		Month month = now.getMonth();//输出APRIL
           //3、获得数字的月份
		int monthValue = now.getMonthValue();//输出:4
           //4、获取当前是一年中的第多少天
		int dayOfYear = now.getDayOfYear();//输出:115
		//5、获取当前是当月的第多少天
		int dayOfMonth = now.getDayOfMonth();//输出:25
		//6、获取当前是星期几,返回英文
		DayOfWeek dayOfWeek = now.getDayOfWeek();//输出:TUESDAY
		//7、获取当前是星期几,返回数字
		int dayOfWeekValue = dayOfWeek.getValue();//输出:2
		//8、获取当前是几点钟
		int hour = now.getHour();//输出:0
		//9、获取当前的分钟数
		int minute = now.getMinute();//输出:42
		//10、获取当前的秒数
		int second = now.getSecond();//输出:59
		//11、获取毫秒单位
		long seconds = Instant.now().getEpochSecond(); //输出:1682354579
		//12、获取时间戳
		long milliSeconds = Instant.now().toEpochMilli();//输出:1682354579089
	}

2.2、LocalDateTime计算时间差

public class TestDate2 {
	public static void main(String[] args) {
		LocalDateTime dateTime = LocalDateTime.now();
		//当前时间减一秒
		dateTime.minusSeconds(1);// 2023-01-29T14:38:50
		//当前时间减一分钟
		dateTime.minusMinutes(1);// 2023-01-29T14:38:51
		//当前时间减一小时
		dateTime.minusHours(1);  // 2023-01-29T13:38:51
		//当前时间减一天
		dateTime.minusDays(1);   // 2023-01-28T14:38:51
		//当前时间减一周
		dateTime.minusWeeks(1);  // 2023-01-22T14:38:51
		//当前时间减一个月
		dateTime.minusMonths(1); // 2022-12-29T14:38:51
		//当前时间减一年
		dateTime.minusYears(1);  // 2022-01-29T14:38:51
		dateTime.plusSeconds(1);//当前时间增加1秒
		dateTime.plusMinutes(1);//当前时间增加1分钟
		dateTime.plusHours(1);//当前时间增加1小时
		dateTime.plusDays(1);//当前时间增加1天
		dateTime.plusMonths(1);//当前时间增加1月
		dateTime.plusYears(1);//当前时间增加1年
		dateTime.plusWeeks(1);//当前时间增加1周
	}
}

3、LocalDate使用详解

3.1、LocalDate基本使用

public class TestLocalDate {
	public static void main(String[] args) {
		 //1、获取当天
        LocalDate localDate1= LocalDate.now();
        //localDate1=2023-07-19
        System.out.println("localDate1=" + localDate1); 
        System.out.println("年=>"+localDate1.getYear());//年=>2023
        System.out.println("月=>"+localDate1.getMonthValue());//月=>7
        System.out.println("日=>"+localDate1.getDayOfMonth());//日=>19
        System.out.println("周几=>"+localDate1.getDayOfWeek());//周几=>WEDNESDAY
        //一年第多少天=>200
        System.out.println("一年第多少天=>"+localDate1.getDayOfYear());
        
//指定格式类型
        LocalDate formatDate = LocalDate.now();
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String format = formatDate.format(dateTimeFormatter);
        //format=>2023-07-19
        System.out.println("format=>"+format);

        //修改日期
        LocalDate newLocalDateByYear=localDate1.withYear(2024);//修改年
        //localDate1.withMonth(8);//修改月
        //localDate1.withDayOfMonth(20);//修改日
        //最新=2024-07-19
        System.out.println("最新="+newLocalDateByYear);
                
        //获取特定的日期
        LocalDate localDate2 = LocalDate.of(2023,7,15);
        //localDate2=2023-07-15
        System.out.println("localDate2="+localDate2);

        //判断是否是闰年
        LocalDate localDate9 = LocalDate.now();
        boolean leapYear = localDate9.isLeapYear();
        //leapYear=false
        System.out.println("leapYear="+leapYear);
	}
}

3.2、LocalDate中with使用

//1、判断本周星期一的日期
        LocalDate localDate4= LocalDate.now();//测试日期2023-07-19
        LocalDate day1 = localDate4.with(DayOfWeek.MONDAY);
        //day1=2023-07-17
        System.out.println("day1="+day1);

//2、这个月的最后一个星期日
localDate.with(TemporalAdjusters.dayOfWeekInMonth(-1,DayOfWeek.SUNDAY));

//3、获取下个星期一
localDate.with(TemporalAdjusters.next(DayOfWeek.MONDAY));

        //4、获取本月第一天的日期
        LocalDate localDate5 = LocalDate.now();
        LocalDate firstDayOfMonth = localDate5.with(TemporalAdjusters.firstDayOfMonth());
        //dayOfMonth=2023-07-01
        System.out.println("dayOfMonth="+firstDayOfMonth);
        
        //5、获取本月最后一天日期
        LocalDate localDate6 = LocalDate.now();
        LocalDate lastDayOfMonth = localDate6.with(TemporalAdjusters.lastDayOfMonth());
        //lastDayOfMonth=2023-07-31
        System.out.println("lastDayOfMonth="+lastDayOfMonth);
        
        //6、获取本年第一天的日期
        LocalDate localDate7 = LocalDate.now();
        LocalDate firsDayOfYear = localDate7.with(TemporalAdjusters.firstDayOfYear());
        System.out.println("firsDayOfYear="+firsDayOfYear);
        
        //7、获取本月的总天数
        LocalDate localDate8 = LocalDate.now();
        int totalDayOfMonth = localDate8.withMonth(localDate8.getMonthValue()).lengthOfMonth();
        //totalDayOfMonth=31
        System.out.println("totalDayOfMonth="+totalDayOfMonth);

 

3.3、LocalDate中plus,minus计算时间差

plus:获取当前日期之后的日期

minus:获取当前日期之前的日期

//当前日期基础上推迟日期
        //localDate1.minusYears(2);两年前
        LocalDate newYear= localDate1.plusYears(2);
        System.out.println("两年后="+newYear);
        //localDate1.minusMonths(2);两月前
        LocalDate newMonth= localDate1.plusMonths(2);
        System.out.println("两月后="+newMonth);
        //localDate1.minusDays(2);两天前
        LocalDate newDay= localDate1.plusDays(2);
        System.out.println("两天后="+newDay);
        //localDate1.minusWeeks(2);两周前
        LocalDate newWeek= localDate1.plusWeeks(2);
        System.out.println("两周后="+newWeek);

3.4、LocalDate如何进行日期比较

isBefore:比较currentDate1是否在currentDate2之前

isAfter:比较currentDate1是否在currentDate2之后

isEqual:比较currentDate1是否在currentDate2是否相等

//日期比较
        LocalDate currentDate1 = LocalDate.parse("2023-07-17");
        LocalDate currentDate2=  LocalDate.parse("2023-07-18");
        //日期相减 currentDate1-currentDate2
        int betweenDays1 = currentDate1.compareTo(currentDate2);
        System.out.println("日期相差="+betweenDays1);//日期相差=-1
        //日期相差多少天
        long betweenDays2 = ChronoUnit.DAYS.between(currentDate1, currentDate2);
        System.out.println("日期相差="+betweenDays2);//日期相差=1
        
        boolean result = currentDate1.isBefore(currentDate2);
        //比较结果=true
        System.out.println("比较结果="+result);

4、LocalTime使用详解

public class TestLocalTime {
	public static void main(String[] args) {
		LocalTime localTime1=LocalTime.now();
		//localTime1=>17:27:45.830
		System.out.println("localTime1=>"+localTime1);
		//小时=>17
		System.out.println("小时=>"+localTime1.getHour());
		//分钟=>27
		System.out.println("分钟=>"+localTime1.getMinute());
		//秒=>45
		System.out.println("秒=>"+localTime1.getSecond());
		//毫秒=>1689758865831
		System.out.println("毫秒=>"+System.currentTimeMillis());
		//纳秒=>830000000
		System.out.println("纳秒=>"+localTime1.getNano());
		
		//获取指定时间
		LocalTime localTime2 = LocalTime.of(12, 0, 0);
		//localTime2=>12:00
		System.out.println("localTime2=>"+localTime2);
		
		//更改时刻
		LocalTime localTime3 = LocalTime.now().withHour(12);
		//localTime3=>12:44:00.374
		System.out.println("localTime3=>"+localTime3);
		
		//时间比较1
		LocalTime currentDataTime1 = LocalTime.of(10, 0, 0);
		LocalTime currentDataTime2 = LocalTime.of(11, 0, 0);
		boolean after = currentDataTime1.isAfter(currentDataTime2);
		//10点是否在11点之后=>false
		System.out.println("10点是否在11点之后=>"+after);

		boolean before = currentDataTime1.isBefore(currentDataTime2);
		//10点是否在11点之前=>true
		System.out.println("10点是否在11点之前=>"+before);
		
		//时间比较2
		LocalTime currentDataTime3 = LocalTime.of(10, 0, 0);
		LocalTime currentDataTime4 = LocalTime.of(11, 0, 0);
		int compare1 = currentDataTime3.compareTo(currentDataTime4);
		int compare2 = currentDataTime3.compareTo(currentDataTime4);
		int compare3 = currentDataTime3.compareTo(LocalTime.of(8, 0, 0));
		System.out.println("compare1=>"+compare1);//compare1=>-1
		System.out.println("compare2=>"+compare2);//compare2=>-1
		System.out.println("compare3=>"+compare3);//compare3=>1
	}
}

 

 

 

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

雾林小妖

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

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

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

打赏作者

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

抵扣说明:

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

余额充值