Java8中LocalDateTime相关类和方法

本文详细介绍Java8中LocalDateTime类及其相关类的使用方法,包括LocalDate、LocalTime的实例化、日期时间的加减操作、日期格式化及日期比较等功能,并演示如何将Java8日期时间与传统Date类型进行转换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Java8中LocalDateTime相关类和方法都在这了【破涕为笑】

import org.junit.Test;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters;
import java.util.Date;

/**
 * @author Tuzi
 * @since 1.0.0
 */
public class LocalDateTimeDemo {

    @Test
    public void testLocalDate() {
        // 获取LocalDate
        LocalDate now = LocalDate.now();
        LocalDate localDate = LocalDate.of(1995, 10, 19);
        LocalDate localDate2 = LocalDate.parse("2017-02-22");

        // 昨天
        LocalDate yesterday = now.minusDays(1);

        // 上个月的今天
        LocalDate localDate1 = now.minus(1, ChronoUnit.MONTHS);

        DayOfWeek dayOfWeek = localDate2.getDayOfWeek();
        int dayOfMonth = localDate2.getDayOfMonth();

        // 判断今年是不是闰年
        boolean leapYear = now.isLeapYear();

        // 获取这个月的第一天
        LocalDate firstDayOfMonth = now.withDayOfMonth(1);
        LocalDate firstDayOfMonth2 = now.with(TemporalAdjusters.firstDayOfMonth());

        // 判断今天是不是生日
        LocalDate birthday = LocalDate.parse("1995-10-19");
        MonthDay birthdayInMonthDay = MonthDay.of(birthday.getMonth(), birthday.getDayOfMonth());
        MonthDay today = MonthDay.from(now);
        boolean todayIsMyBrithday = birthdayInMonthDay.equals(today);
    }

    @Test
    public void testLocalTime() {
        // 获取LocalTime
        LocalTime now = LocalTime.now();    // 16:39:45.252
        LocalTime localTime = LocalTime.of(16, 40);
        LocalTime localTime1 = LocalTime.parse("16:40");

        LocalTime nextHour = now.plus(1, ChronoUnit.HOURS);

        int hour = now.getHour();
        int minute = now.getMinute();

        boolean after = localTime.isAfter(now);

        // 常量
        System.out.println(LocalTime.MAX);      // 23:59:59.999999999
        System.out.println(LocalTime.MIN);      // 00:00
    }

    @Test
    public void testLocalDateTime() {
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime localDateTime = LocalDateTime.parse("2017-11-06T16:48:15.801");
        LocalDateTime localDateTime1 = LocalDateTime.of(2017, 11, 06, 16, 48, 15, 801);

        // 加减
        LocalDateTime plusHours = now.plusHours(1);
        LocalDateTime minus = now.minus(2, ChronoUnit.DAYS);

        System.out.println(now.getDayOfMonth());

        LocalDate localDate = localDateTime.toLocalDate();
        LocalTime localTime = localDateTime.toLocalTime();
        LocalDateTime localDateTime2 = LocalDateTime.from(localDate.atStartOfDay());
    }

    @Test
    public void testDateTimeFormatter() {
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        System.out.println("默认格式: " + now);
        System.out.println("自定义格式:  " + now.format(dateTimeFormatter));

        LocalDateTime.parse("2017-11-06 17:02:05", dateTimeFormatter);

        dateTimeFormatter.format(LocalDateTime.now());
    }

    @Test
    public void testPeriod() {
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime plusTwoDay = now.plus(Period.ofDays(2));
        long between = ChronoUnit.DAYS.between(now, plusTwoDay);
    }

    @Test
    public void transfer() {
        Date date = Date.from(Instant.now());
        date.toInstant();

        LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());

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

        LocalDate localDate = LocalDate.now();
        Date.from(localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant());
    }

package com.aapoint.util; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.time.temporal.TemporalAdjusters; public class LocalDateTimeUtil { /** * 比较 localDateTime2 是否在localDateTime1之前(比较大小) * @param localDateTime1 * @param localDateTime2 * @return */ public static Boolean compare(LocalDateTime localDateTime1,LocalDateTime localDateTime2){ return localDateTime1.isBefore(localDateTime2); } /** * 获取当前月份前/后的月份的第一天 * @param i 指定距离当前月份的时间 * @param state 状态 0.当月 1.前 2.后 * @return */ public static String firstDay(Integer state,Integer i){ LocalDateTime date = null; //type 型 0.1.天 2.小时 3.分钟 4.秒 date = getLocalDateTime(state,0,i); //获取该月份的第一天 String firstDay = date.with(TemporalAdjusters.firstDayOfMonth()).format(DateTimeFormatter.ofPattern("yyyy-MM-dd")); // System.out.println("第一天为:"+firstDay); return firstDay; } /** * 获取当前月份前/后的月份的最后一天 * @param i 指定距离当前月份的时间 * @param state 状态 0.当月 1.前 2.后 * @return */ public static String lastDay(Integer state,Integer i){ LocalDateTime date = null; //type 型 0.1.天 2.小时 3.分钟 4.秒 date = getLocalDateTime(state,0,i); //获取该月份的最后一天 String lastDay = date.with(TemporalAdjusters.lastDayOfMonth()).format(DateTimeFormatter.ofPattern("yyyy-MM-dd")); // System.out.println("最后一天为:"+lastDay); return lastDay; } /** * 获取当时间前/后的时间(天) * @param i 指定距离当前月份的时间 * @param state 状态 0.当月 1.前 2.后 * @return */ public static String obtainDay(Integer state,Integer i){ LocalDateTime date = null; //type 型 0.1.天 2.小时 3.分钟 4.秒 date = getLocalDateTime(state,1,i); //获取天 String day = date.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")); // System.out.println("获取的时间为(天):"+day); return day; } /** * 获取当时间前/后的时间(小时) * @param i 指定距离当前月份的时间 * @param state 状态 0.当月 1.前 2.后 * @return */ public static String obtainHours(Integer state,Integer i){ LocalDateTime date = null; //type 型 0.1.天 2.小时 3.分钟 4.秒 date = getLocalDateTime(state,2,i); //获取该月份的最后一天 String hours = date.format(DateTimeFormatter.ofPattern("HH:mm:ss")); // System.out.println("获取的时间为(小时):"+hours); return hours; } /** * 获取当时间前/后的时间(小时) * @param i 指定距离当前月份的时间 * @param state 状态 0.当月 1.前 2.后 * @return */ public static String obtainMinutes(Integer state,Integer i){ LocalDateTime date = null; //type 型 0.1.天 2.小时 3.分钟 4.秒 date = getLocalDateTime(state,3,i); //获取该月份的最后一天 String minutes = date.format(DateTimeFormatter.ofPattern("HH:mm:ss")); // System.out.println("获取的时间为(分钟):"+minutes); return minutes; } /** * 获取当时间前/后的时间(小时) * @param i 指定距离当前月份的时间 * @param state 状态 0.当月 1.前 2.后 * @return */ public static String obtainSeconds(Integer state,Integer i){ LocalDateTime date = null; //type 型 0.1.天 2.小时 3.分钟 4.秒 date = getLocalDateTime(state,4,i); //获取该月份的最后一天 String seconds = date.format(DateTimeFormatter.ofPattern("HH:mm:ss")); // System.out.println("获取的时间为(秒):"+seconds); return seconds; } public static void main(String[] args) { System.out.println("当前时间为:"+LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))); System.out.println("前一个月份的第一天为:"+LocalDateTimeUtil.firstDay(1,1)); System.out.println("前一个月份的最后一天为:"+LocalDateTimeUtil.lastDay(1,1)); System.out.println("当前时间的前一天为:"+LocalDateTimeUtil.obtainDay(1,1)); System.out.println("当前时间的后一天为:"+LocalDateTimeUtil.obtainDay(2,1)); System.out.println("当前时间的前一小时为:"+LocalDateTimeUtil.obtainHours(1,1)); System.out.println("当前时间的后一小时为:"+LocalDateTimeUtil.obtainHours(2,1)); System.out.println("当前时间的前一分钟为:"+LocalDateTimeUtil.obtainMinutes(1,1)); System.out.println("当前时间的后一分钟为:"+LocalDateTimeUtil.obtainMinutes(2,1)); System.out.println("当前时间的前一秒为:"+LocalDateTimeUtil.obtainSeconds(1,1)); System.out.println("当前时间的后一秒为:"+LocalDateTimeUtil.obtainSeconds(2,1)); } private static LocalDateTime getLocalDateTime(Integer state,Integer type,Integer i) { LocalDateTime date; if(state == 0){ date = LocalDateTime.now(); }else if(state == 1){ if(type == 0) { //获取月 date = LocalDateTime.now().minusMonths(i); }else if(type == 1){ //获取天 date = LocalDateTime.now().minusDays(i); }else if(type == 2){ //获取小时 date = LocalDateTime.now().minusHours(i); }else if(type == 3){ //获取分钟 date = LocalDateTime.now().minusMinutes(i);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值