LocalDate,LocalTime 和 LocalDateTime的基本使用


LocalDate、LocalTime 和 LocalDateTime 是 Java 8 引入的日期时间类,它们分别用于表示日期、时间和日期时间。这些类位于 java.time 包中,提供了更现代、更灵活的方式来处理日期和时间,至于选择使用哪个类取决于你的需求,如果你只关心日期,就使用 LocalDate;如果只关心时间,就使用 LocalTime;如果两者都需要,就使用 LocalDateTime。

1.LocalDate

LocalDate 表示日期,不包含具体的时间信息,也没有时区。以下是一些常见的操作:

 		//获取当前日期
 		LocalDate localDate = LocalDate.now();//2024-01-24

		//创建指定日期
        LocalDate date = LocalDate.of(2024, Month.JANUARY, 24);//2024-01-24

		//获取日期的各个部分
        int year1 = localDate.getYear();//2024
        Month month1 = localDate.getMonth();//JANUARY
        int monthValue1 = localDate.getMonthValue();//1
        int day1 = localDate.getDayOfMonth();//24
        DayOfWeek dayOfWeek = localDate.getDayOfWeek();//WEDNESDAY
        int dayOfYear = localDate.getDayOfYear();//24
        
        System.out.println("----------------------------------------");

        //日期的加减
        LocalDate currentDate = LocalDate.now();
        LocalDate plusDate = currentDate.plusYears(1).plusMonths(1).plusWeeks(1).plusDays(1);
        LocalDate minusDate = currentDate.minusYears(1).minusMonths(1).minusWeeks(1).minusDays(1);
        
        //加 年/月/周/日
        LocalDate plusYears = currentDate.plusYears(1);
        LocalDate plusMonths = currentDate.plusMonths(1);
        LocalDate plusWeeks = currentDate.plusWeeks(1);
        LocalDate plusDays = currentDate.plusDays(1);

        //减 年/月/周/日
        LocalDate minusYears = currentDate.minusYears(1);
        LocalDate minusMonths = currentDate.minusMonths(1);
        LocalDate minusWeeks = currentDate.minusWeeks(1);
        LocalDate minusDays = currentDate.minusDays(1);

2.LocalTime

LocalTime 表示时间,不包含日期和时区。以下是一些常见的操作:

		//获取当前时间
		LocalTime localTime = LocalTime.now();//14:02:23.832
		
		//创建指定时间
		LocalTime localTimeOf = LocalTime.of(13, 15, 56);//13:15:56

		//获取时间的各个部分
        int localTimeHour = localTime.getHour();//14
        int localTimeMinute = localTime.getMinute();//2
        int localTimeSecond = localTime.getSecond();//23
        int localTimeNano = localTime.getNano();//832000000

		System.out.println("----------------------------------------");

		//时间的加减
 		LocalTime currentTime = LocalTime.now();
 		LocalTime plusTime = currentTime.plusHours(1).plusMinutes(2).plusSeconds(3);
        LocalTime minusTime = currentTime.minusHours(1).minusMinutes(2).minusSeconds(3);
        
        // 加 时/分/秒
        LocalTime plusHours = currentTime.plusHours(1);
        LocalTime plusMinutes = currentTime.plusMinutes(2);
        LocalTime plusSeconds = currentTime.plusSeconds(3);

        //减 时/分/秒
        LocalTime minusHours = currentTime.minusHours(1);
        LocalTime minusMinutes = currentTime.minusMinutes(2);
        LocalTime minusSeconds = currentTime.minusSeconds(3);

3.LocalDateTime

		//获取当前日期和时间
        LocalDateTime dateTime = LocalDateTime.now();//2024-01-24T14:14:22.197

		//创建指定日期和时间
        LocalDateTime localDateTime = LocalDateTime.of(2024, Month.JANUARY, 24, 11, 8, 54);//2024-01-24T11:08:54
        
        //获取日期和时间的各个部分
        int year = dateTime.getYear();//2024
        Month month = dateTime.getMonth();//JANUARY
        int monthValue = dateTime.getMonthValue();//1
        int day = dateTime.getDayOfMonth();//24
        int hour = dateTime.getHour();//14
        int minute = dateTime.getMinute();//14
        int second = dateTime.getSecond();//22
        
        System.out.println("----------------------------------------");
        
        //日期和时间的加减
        LocalDateTime currentDateTime = LocalDateTime.now();
        LocalDateTime localDateTimePlus = currentDateTime.plusYears(1).plusMonths(1).plusDays(1).plusHours(2).plusMinutes(2).plusSeconds(2).plusWeeks(1);
        LocalDateTime localDateTimeMinus = currentDateTime.minusYears(1).minusMonths(1).minusDays(1).minusHours(2).minusMinutes(2).minusSeconds(2).minusWeeks(1);

4.不同类型之间的转换关系

4.1 LocalDateTime 转化 --> LocalDate/LocalTime/Date/String
        LocalDateTime localDateTime = LocalDateTime.now();

        //(1)LocalDateTime 转 LocalDate
        LocalDate toLocalDate = localDateTime.toLocalDate();

        //(2)LocalDateTime 转 LocalTime
        LocalTime toLocalTime = localDateTime.toLocalTime();

        //(3)LocalDateTime 转 Date
        Date dateTimeAsDate = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());

        //(4)LocalDateTime 转 String
        String toString = localDateTime.toString();
        
        //使用特定格式化形式将 LocalDateTime 转为 String
        String dateStr = localDateTime.format(DateTimeFormatter.ofPattern("yyyy/MM/dd"));
4.2 LocalDate 转化 --> LocalDateTime/Date/String
        //(1)LocalDate 和 LocalTime 转化为 LocalDateTime
        LocalDate date = LocalDate.now();
        LocalTime time = LocalTime.now();
        LocalDateTime dateTime = date.atTime(time);

        //以下三种也可以的 LocalDate --> LocalDateTime(第三种跟上面的是一样的)
        LocalDateTime localDateTime2 = date.atStartOfDay();
        LocalDateTime localDateTime3 = date.atTime(8, 20, 33);
        LocalDateTime localDateTime4 = date.atTime(LocalTime.now());

        //(2)LocalDate 转化 Date
        LocalDate localDate = LocalDate.now();
        Date localDateToDate = Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());

        //(3)LocalDate 转化 String
        String dateString = localDate.toString();

        //使用特定格式化形式将 LocalDate 转为 String
        String dateStr2 = localDate.format(DateTimeFormatter.ofPattern("yyyy/MM/dd"));
4.3 LocalTime 转化 --> LocalDateTime/Date/String
		//(1)LocalDate 和 LocalTime 转化为 LocalDateTime
        LocalDate date = LocalDate.now();
        LocalTime time = LocalTime.now();
        LocalDateTime dateTime = date.atTime(time);

        LocalTime localTime = LocalTime.now();
        
        //(2)LocalTime 转为 Date
        Date timeAsDate = Date.from(localTime.atDate(LocalDate.of(1970, 1, 1)).atZone(ZoneId.systemDefault()).toInstant());
        
        //(3)LocalTime 转为 String
        String timeString = localTime.toString();
        
        //使用特定格式化形式将 LocalTime 转为 String
        String dateStr1 = localTime.format(DateTimeFormatter.ofPattern("HH:mm:ss"));
4.4 Date 转化 --> LocalDateTime/LocalDate/LocalTime/String
		Date date1 = new Date();
        Instant instant = date1.toInstant();
        
        //(1)Date 转化 --> LocalDateTime
        LocalDateTime localDateTime1 = instant.atZone(ZoneId.systemDefault()).toLocalDateTime();
        
        //(2)Date 转化 --> LocalDate
        LocalDate localDate1 = instant.atZone(ZoneId.systemDefault()).toLocalDate();
        
        //(3)Date 转化 --> LocalTime
        LocalTime localTime1 = instant.atZone(ZoneId.systemDefault()).toLocalTime();

        //(4)Date 转化 --> String
        String pattern = "yyyy-MM-dd HH:mm:ss";// 定义日期格式
        SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
        String dateToSting = dateFormat.format(date1);// 将 Date 转换为 String
4.5 String 转化 --> LocalDateTime/LocalDate/LocalTime/Date
		//(1)String 转化 --> LocalDateTime
        String dateTimeString = "2024-01-24T14:30:00";
        LocalDateTime dateTime1 = LocalDateTime.parse(dateTimeString);

        //使用特定格式化形式从String 转化 LocalDateTime
        LocalDateTime ldt3 = LocalDateTime.parse("2019-12-07 21:20:06", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));

        //(2)String 转化 --> LocalDate
        String dateString1 = "2024-01-24";
        LocalDate toDate = LocalDate.parse(dateString1);

        //使用特定格式化形式从String 转化 LocalDate
        LocalDate date2 = LocalDate.parse("2019-03-03", DateTimeFormatter.ofPattern("yyyy-MM-dd"));

        //(3)String 转化 --> LocalTime
        String timeString1 = "14:30:00";
        LocalTime time1 = LocalTime.parse(timeString1);

        //使用特定格式化形式从String 转化 LocalTime
        LocalTime date3 = LocalTime.parse("12:01:02", DateTimeFormatter.ofPattern("HH:mm:ss"));

        //(4)String 转化 --> Date
        String dateString2 = "2024-01-24 15:30:00";// 要转换的字符串
        String pattern1 = "yyyy-MM-dd HH:mm:ss";// 定义日期格式
        SimpleDateFormat dateFormat1 = new SimpleDateFormat(pattern1);
        try {
            // 将字符串解析为 Date
            Date parse = dateFormat1.parse(dateString2);
        } catch (ParseException e) {
            e.printStackTrace();
        }
  • 10
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是关于LocalDateLocalTimeLocalDateTime基本用法代码示例: 1. LocalDate基本用法: ```java // 获取当前日期 LocalDate currentDate = LocalDate.now(); System.out.println("当前日期: " + currentDate); // 创建指定日期 LocalDate specificDate = LocalDate.of(2022, 12, 31); System.out.println("指定日期: " + specificDate); // 获取年、月、日 int year = specificDate.getYear(); int month = specificDate.getMonthValue(); int day = specificDate.getDayOfMonth(); System.out.println("年: " + year + ", 月: " + month + ", 日: " + day); // 日期比较 LocalDate date1 = LocalDate.of(2022, 1, 1); LocalDate date2 = LocalDate.of(2022, 1, 2); boolean isBefore = date1.isBefore(date2); boolean isAfter = date1.isAfter(date2); boolean isEqual = date1.isEqual(date2); System.out.println("date1是否在date2之前: " + isBefore); System.out.println("date1是否在date2之后: " + isAfter); System.out.println("date1是否等于date2: " + isEqual); ``` 2. LocalTime基本用法: ```java // 获取当前时间 LocalTime currentTime = LocalTime.now(); System.out.println("当前时间: " + currentTime); // 创建指定时间 LocalTime specificTime = LocalTime.of(12, 30, 45); System.out.println("指定时间: " + specificTime); // 获取时、分、秒 int hour = specificTime.getHour(); int minute = specificTime.getMinute(); int second = specificTime.getSecond(); System.out.println("时: " + hour + ", 分: " + minute + ", 秒: " + second); // 时间比较 LocalTime time1 = LocalTime.of(10, 30); LocalTime time2 = LocalTime.of(12, 30); boolean isBefore = time1.isBefore(time2); boolean isAfter = time1.isAfter(time2); boolean isEqual = time1.equals(time2); System.out.println("time1是否在time2之前: " + isBefore); System.out.println("time1是否在time2之后: " + isAfter); System.out.println("time1是否等于time2: " + isEqual); ``` 3. LocalDateTime基本用法: ```java // 获取当前日期时间 LocalDateTime currentDateTime = LocalDateTime.now(); System.out.println("当前日期时间: " + currentDateTime); // 创建指定日期时间 LocalDateTime specificDateTime = LocalDateTime.of(2022, 12, 31, 12, 30, 45); System.out.println("指定日期时间: " + specificDateTime); // 获取年、月、日、时、分、秒 int year = specificDateTime.getYear(); int month = specificDateTime.getMonthValue(); int day = specificDateTime.getDayOfMonth(); int hour = specificDateTime.getHour(); int minute = specificDateTime.getMinute(); int second = specificDateTime.getSecond(); System.out.println("年: " + year + ", 月: " + month + ", 日: " + day); System.out.println("时: " + hour + ", 分: " + minute + ", 秒: " + second); // 日期时间比较 LocalDateTime dateTime1 = LocalDateTime.of(2022, 1, 1, 10, 30); LocalDateTime dateTime2 = LocalDateTime.of(2022, 1, 2, 12, 30); boolean isBefore = dateTime1.isBefore(dateTime2); boolean isAfter = dateTime1.isAfter(dateTime2); boolean isEqual = dateTime1.equals(dateTime2); System.out.println("dateTime1是否在dateTime2之前: " + isBefore); System.out.println("dateTime1是否在dateTime2之后: " + isAfter); System.out.println("dateTime1是否等于dateTime2: " + isEqual); } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值