关于LocalDate、LocalTime、LocalDateTime

0.快速上手

看源码:  https://www.matools.com/api/java8 在java.time中
//获取当前时间
LocalDateTime localDateTime = LocalDateTime.now();

//想要设置当前时间为12点0分0秒(要记住结果不是修改之前的,而是重新生成的)
LocalDateTime  localDateTime2  = LocalDateTime.now().withHour(12).withMinute(0).withSecond(0);

//打印结果(看样子还要设置毫秒,自己试试吧!)
2022-10-20T12:00:00.703


1.前言

我们通常使用Date ,Calendar ,GregoiranCalendar ,SimpleDateFormat 来分别处理
日期、日历、公历、或者将日期时间格式化;
但它们四个都是线程不安全的,而且处理很复杂;

在项目开发中,使用这JDK8新特性里提供的3个时间类:
LocalDate、LocalTime、LocalDateTime可以大大简化我们的处理过程。

2.快速了解LocalDate、LocalTime、LocalDateTime

2.1 LocalDate (获取年月日)

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.Month;
import java.time.temporal.ChronoField;

         //方式一:获取当前时间
         LocalDate localDate = LocalDate.now();
         //方式二:构造指定的年月日
         LocalDate localDate1 = LocalDate.of(2022, 8, 9);
         
         // 获取该时间的年/月/日
        int year1 = localDate.getYear();
        Month month1 = localDate.getMonth();//返回月份英文(大写),(Month是枚举类型)
        int day1 = localDate.getDayOfMonth();
        
         // 获取该时间年/月/日
        int year2 = localDate.get(ChronoField.YEAR);
        int month2 = localDate.get(ChronoField.MONTH_OF_YEAR);
        int day2 = localDate.get(ChronoField.DAY_OF_MONTH);
        
        // 获取该时间在一周中的所属位置
        DayOfWeek dayOfWeek1 = localDate.getDayOfWeek();//返回星期的的英文(大写)
        int dayOfWeek2 = localDate.get(ChronoField.DAY_OF_WEEK);

2.2 LocalTime (获取时分秒)

//创建LocalTime
 LocalTime localTime = LocalTime.of(13, 51, 10);
 LocalTime localTime1 = LocalTime.now();

//获取小时
int hour = localTime.getHour();
int hour1 = localTime.get(ChronoField.HOUR_OF_DAY);

//获取分
int minute = localTime.getMinute();
int minute1 = localTime.get(ChronoField.MINUTE_OF_HOUR);

//获取秒
int second = localTime.getSecond();
int second1 = localTime.get(ChronoField.SECOND_OF_MINUTE);

2.3 LocalDateTime(年月日时分秒)

//获取年月日时分秒,等于LocalDate+LocalTime

//创建LocalDateTime
LocalDateTime localDateTime = LocalDateTime.now();

LocalDateTime localDateTime1 = LocalDateTime.of(2019, Month.SEPTEMBER, 10, 14, 46, 56);

LocalDateTime localDateTime2 = LocalDateTime.of(localDate, localTime);

LocalDateTime localDateTime3 = localDate.atTime(localTime);

LocalDateTime localDateTime4 = localTime.atDate(localDate);

//获取LocalDate
LocalDate localDate2 = localDateTime.toLocalDate();


//获取LocalTime
LocalTime localTime2 = localDateTime.toLocalTime();

2.4 Instant(秒,毫秒)

//创建Instant对象
Instant instant = Instant.now();

//获取秒数
long currentSecond = instant.getEpochSecond();

//获取毫秒数(时间戳)
long currentMilli = instant.toEpochMilli();

3.增减年月日时分秒的方法(plus/minus系列的方法)

使用以下的增加/减少时间的方法,就无需考虑时间不够(加/减)的问题.
比如时间为1月31号,现在增加一天,会变为2月1号.

3.1 增加相关的方法

    plusYears(int offset)//:增加指定年份
    plusMonths(int offset)//:增加指定月份
    plusWeeks(int offset)//:增加指定周
    plusDates(int offset)//:增加指定日
    plusHours(int offset)//:增加指定时
    plusMinuets(int offset)//:增加指定分
    plusSeconds(int offset)//:增加指定秒
    plusNanos(int offset)//:增加指定纳秒

3.2 减少相关的方法

    minusYears(int offset)//:减少指定年
    minusMonths(int offset)//:减少指定月
    minusWeeks(int offset)//:减少指定周
    minusDates(int offset)//:减少指定日
    minusHours(int offset)//:减少指定时
    minusMinuets(int offset)//:减少指定分
    minusSeconds(int offset)//:减少指定秒
    minusNanos(int offset)//:减少指定纳秒

3.3 设置时间(返回这个时间的副本,新对象)

	withYear(int year)//设置年
	withMonth(int month)//设置月
	withDayOfMonth(int dayOfMonth)//设置日,取值范围(1-31)   --设置2月31号会返回异常的哦!
	withDayOfYear(int dayOfYear)//设置日,取值范围(1-365)
	
	withHour(int hour)//设置小时
	withMinute(int minute)//设置分钟
	withSecond(int second)//设置秒
	withNano(int nanoOfSecond)//设置纳秒

3.4代码演示:

public class TimeOfStudy {
    public static void main(String[] args) {
        //当前时间为2022-02-14
        LocalDate localDate = LocalDate.now();
        System.out.println(localDate);

        //年
        int year = localDate.getYear();
        int year02 = localDate.plusYears(1L).getYear();
        int year03 = localDate.minusYears(1L).getYear();
        //打印信息
        System.out.println(year + "--->year");
        System.out.println(year02 + "--->year2");
        System.out.println(year03 + "--->year2");

        //月
        Month month = localDate.getMonth();
        int monthValue = localDate.getMonthValue();
        int monthValue02 = localDate.plusMonths(1L).getMonthValue();
        int monthValue03 = localDate.minusMonths(1L).getMonthValue();
        //打印信息
        System.out.println(month + "--->");
        System.out.println(monthValue + "--->monthValue");
        System.out.println(monthValue02 + "--->monthValue02");
        System.out.println(monthValue03 + "--->monthValue03");

        //日
        DayOfWeek dayOfWeek = localDate.getDayOfWeek();
        int dayOfMonth = localDate.getDayOfMonth();
        int dayOfMonth02 = localDate.plusDays(1L).getDayOfMonth();
        int dayOfMonth03 = localDate.minusDays(1L).getDayOfMonth();
        int dayOfYear = localDate.getDayOfYear();

        //打印信息
        System.out.println(dayOfWeek + "--->dayOfWeek");
        System.out.println(dayOfMonth + "--->dayOfMonth");
        System.out.println(dayOfMonth02 + "--->dayOfMonth02");
        System.out.println(dayOfMonth03 + "--->dayOfMonth03");
        System.out.println(dayOfYear + "--->dayOfYear");

    }
}

4.与Data的相互转化

看源码:https://www.matools.com/api/java8

4.1 Date转LocalDateTime

    Date date = new Date();
    //获取系统时区
    ZoneId zoneId = ZoneId.systemDefault();
    LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), zoneId);

4.2 LocalDateTime转Date

    ZoneId zoneId = ZoneId.systemDefault();
    LocalDateTime localDateTime = LocalDateTime.now();
    Date date = Date.from(localDateTime.atZone(zoneId).toInstant());

5.与时间戳的相互转换

       ZoneId(时区), ZoneOffset(偏移数据)

5.1: 获取系统时间戳


       long l = System.currentTimeMillis();
       System.out.println("System::  "+l);

5.2: 使用Instant获取时间戳

  
       long l = Instant.now().toEpochMilli();
       System.out.println("Instant::  "+l);

5.3: 使用LocalDate转换为时间戳

        
        ZoneId zoneId = ZoneId.systemDefault();
        LocalDate localDate = LocalDate.now();
        long timestampDay = localDate.atStartOfDay(zoneId).toInstant().toEpochMilli();
        System.out.println("Day::  "+timestampDay);

5.4: 使用LocalDateTime获取时间戳

 
        LocalDateTime localDateTime = LocalDateTime.now();
        long timestamp = localDateTime.toInstant(ZoneOffset.ofHours(8)).toEpochMilli();
        System.out.println("DateTime::  " +timestamp);

5.5: 时间戳转Date

 
        System.out.println(new Date(放入时间戳));

6 LocalDateTime之间的比较

在这里插入图片描述
使用就是A.compareTo(B);时间A大于B返回个正值,反之亦然。

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值