LocalDate和LocalTime和LocalDateTime常用工具类

文章介绍了Java8中的LocalDateUtil工具类,包含了日期和时间的处理方法,如日期格式转换、当前时间获取、字符串解析等。
package com.zdxz.alliance.util;

import org.apache.commons.lang3.StringUtils;

import java.text.ParseException;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/** java8  LocalDateUtil 工具类
 * @ClassName LocalDateUtil
 * @Description TODO
 * @Author liz
 * @Date 2023/11/14 10:25
 * @Version 2.0
 **/
public class LocalDateUtil {
    public static final String DATE_DIVISION = "-";
    /**
     * yyyy-MM-dd HH:mm:ss
     */
    public static final String TIME_PATTERN_DEFAULT = "yyyy-MM-dd HH:mm:ss";

    /**
     * yyyy-MM-dd HH:mm:
     */
    public static final String TIME_PATTERN_YYYYMMDDHHMM = "yyyy-MM-dd HH:mm";
    /**
     * yyyy-MM-dd
     */
    public static final String DATE_PATTERN_DEFAULT = "yyyy-MM-dd";
    /**
     * yyyyMMdd
     */
    public static final String DATA_PATTERN_YYYYMMDD = "yyyyMMdd";
    /**
     * yyyyMM
     */
    public static final String DATA_PATTERN_YYYYMM = "yyyyMM";
    /**
     * yyyyMM
     */
    public static final String DATA_PATTERN_MMDD = "MM-dd";
    /**
     * HH:mm:ss
     */
    public static final String TIME_PATTERN_HHMMSS = "HH:mm:ss";

    /** 获取当前的日期
     * @description 
     * @author liz
     * @date 2023/11/14 10:29
     * @param 
     * @return java.time.LocalDate
     **/
    public static LocalDate nowDate(){
        return LocalDate.now();
    }
    /** 获取当前的时间
     * @description 
     * @author liz
     * @date 2023/11/14 10:32
     * @param 
     * @return java.time.LocalTime
     **/
    public static LocalTime nowTime(){
        return LocalTime.now();
    }
    /** TODO 获取当前时间年月日十分秒
     * @description 
     * @author liz
     * @date 2023/11/14 10:35
     * @param 
     * @return java.time.LocalDateTime
     **/
    public static LocalDateTime now(){
        return LocalDateTime.now();
    }
    /** TODO 获取当前日期date类型
     * @description 
     * @author liz
     * @date 2023/11/14 13:36
     * @param 
     * @return java.util.Date
     **/
    public static Date date(){
        return localTimeToDate(LocalDateTime.now());
    }
    /** TODO 获取当前时间字符串
     * @description 
     * @author liz
     * @date 2023/11/14 11:26
     * @param 
     * @return java.lang.String
     **/
    public static String nowString(){
        return formatTime(LocalDateTime.now());
    }
    /** TODO 获得当前年月日的字符串
     * @description 
     * @author liz
     * @date 2023/11/14 11:36
     * @param 
     * @return java.lang.String
     **/
    public static String nowDateString(){
        return formatDate(LocalDateTime.now());
    }
    /**
     * 将Timestamp类型的日期转换为系统参数定义的格式的字符串。
     * @return 转换后符合给定格式的日期字符串
     */
    public static String formatDate(LocalDateTime date)
    {
        return format(date, DATE_PATTERN_DEFAULT);
    }

    /**
     * 将Timestamp类型的日期转换为系统参数定义的格式的字符串。
     * @return 转换后符合给定格式的日期字符串
     */
    public static String formatTime(LocalDateTime time)
    {
        return format(time, TIME_PATTERN_DEFAULT);
    }

    /**
     * 将LocalDateTime类型的日期转换为系统参数定义的格式的字符串。
     * @return
     */
    public static String format(LocalDateTime date, String pattern)
    {
        if (null == date || null==pattern) {
            return null;
        }
        DateTimeFormatter format = DateTimeFormatter.ofPattern(pattern);
        //日期转字符串
        return date.format(format);

    }
    /** 将字符串转成 LocalDate
     * @description 
     * @author liz
     * @date 2023/11/15 9:32 
     * @param dateValue 2023-11-15 09:34:50
     * @return java.time.LocalDateTime
     **/
    public static LocalDate parseLocalDate(String dateValue)
    {
        return parse(dateValue,TIME_PATTERN_DEFAULT).toLocalDate();
    }
    /** 将字符串转成 LocalDateTime
     * @description 
     * @author liz
     * @date 2023/11/15 9:32
     * @param dateValue 2023-11-15 09:34:50
     * @return java.time.LocalDateTime
     **/
    public static LocalDateTime parseLocalDateTime(String dateValue)
    {
        return parse(dateValue,TIME_PATTERN_DEFAULT);
    }


    /** 将字符串转换成为LocalDateTime
     * @description
     * @author liz
     * @date 2023/11/15 9:30
     * @param dateValue
     * @param pattern
     * @return java.time.LocalDateTime
     **/
    public static LocalDateTime parse(String dateValue, String pattern){
        if (StringUtils.isEmpty(dateValue) || StringUtils.isEmpty(pattern) ) {
            return null;
        }
        return LocalDateTime.parse(dateValue, DateTimeFormatter.ofPattern(pattern));
    }
    /** LocalDateTime 转 LocalDate
     * @description
     * @author liz
     * @date 2023/11/14 11:45
     * @param localDateTime
     * @return java.time.LocalDate
     **/
    public static LocalDate toLocalDate(LocalDateTime localDateTime){
       return localDateTime.toLocalDate();
    }
    /** Date 转 LocalDateTime
     * @description
     * @author liz
     * @date 2023/11/14 11:53
     * @param date
     * @return java.time.LocalDateTime
     **/
    public static LocalDateTime dateToLocalTime(Date date){
       return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
    }
    /**  LocalDateTime 转 Date
     * @description 
     * @author liz
     * @date 2023/11/14 11:58
     * @param localDateTime
     * @return java.util.Date
     **/
    public static Date localTimeToDate(LocalDateTime localDateTime){
       return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
    }
    /** LocalDate 转 LocalDateTime 00小时00分 eg:2023-11-15T00:00
     * @description 
     * @author liz
     * @date 2023/11/15 10:31
     * @param localDate
     * @return java.time.LocalDateTime
     **/
    public static LocalDateTime localDateToDateTime(LocalDate localDate){
       return localDate.atStartOfDay();
    }
    /** LocalDate拼接时间
     * @description 
     * @author liz
     * @date 2023/11/15 10:38
     * @param localDate
     * @param localTime
     * @return java.time.LocalDateTime
     **/
    public static LocalDateTime localDateAddTime(LocalDate localDate,LocalTime localTime){
       return localDate.atTime(localTime);
    }


    /**
     * 判断两个LocalDateTime时间大小
     * @param date1
     * @param date2
     * @return date1 > date2 = 1 ;date1 < date2 = -1 ;date1 = date2 = 0;
     * @throws ParseException
     */
    public static Integer between(LocalDateTime date1,LocalDateTime date2) {
        //date1 = date2 = 0;
        if (date1.isEqual(date2)){
            return 0;
        }
        //date1 > date2 = 1
        if (date1.isAfter(date2)){
            return 1;
        }
        //date1 < date2 = -1
        if (date1.isBefore(date2)){
            return -1;
        }
        return 0;

    }
    /**
     * 判断两个LocalDate时间大小
     * @param date1
     * @param date2
     * @return date1 > date2 = 1 ;date1 < date2 = -1 ;date1 = date2 = 0;
     * @throws ParseException
     */
    public static Integer between(LocalDate date1,LocalDate date2) {
        //date1 = date2 = 0;
        if (date1.isEqual(date2)){
            return 0;
        }
        //date1 > date2 = 1
        if (date1.isAfter(date2)){
            return 1;
        }
        //date1 < date2 = -1
        if (date1.isBefore(date2)){
            return -1;
        }
        return 0;

    }
    /** 常用方法演示
     * @description
     * @author liz
     * @date 2023/11/15 10:12
     * @param
     * @return void
     **/
    public static void yearAndMonthAndDayAndHourDemo(){
        System.out.println("今天的日期时间字符串:"+nowString());
        LocalDateTime now = LocalDateTime.now();
        int year = now.getYear();
        System.out.println("获取年份:"+year);
        Month month = now.getMonth();
        System.out.println("使用月份枚举值:"+month);
        int dayOfMonth = now.getDayOfMonth();
        System.out.println("获取日期在该月是第几天:"+dayOfMonth);
        DayOfWeek dayOfWeek = now.getDayOfWeek();
        System.out.println("获取日期是星期几:"+dayOfWeek);
        int dayOfYear = now.getDayOfYear();
        System.out.println("获取日期在该年是第几天:"+dayOfYear);
        int hour = now.getHour();
        System.out.println("获取小时:"+hour);
        int minute = now.getMinute();
        System.out.println("获取分钟:"+minute);
        int second = now.getSecond();
        System.out.println("获取秒:"+second);
        int nano = now.getNano();
        System.out.println("获取纳秒:"+second);

        System.out.println("--------------------------增减时间--------------------------");
        LocalDateTime localDateTime1 = now.plusDays(1);
        System.out.println(" 给当前时间加一天:"+localDateTime1);
        LocalDateTime localDateTime2 = now.plusDays(1);
        System.out.println(" 给当前时间加一周:"+localDateTime2);
        LocalDateTime localDateTime3 = now.plusMonths(1);
        System.out.println(" 给当前时间加一月:"+localDateTime3);
        LocalDateTime localDateTime4 = now.plusYears(1);
        System.out.println(" 给当前时间加一年:"+localDateTime4);
        LocalDateTime localTime1 = now.plusHours(1);
        System.out.println(" 给当前时间加一小时:"+localTime1);
        LocalDateTime localTime2 = now.plusMinutes(1);
        System.out.println(" 给当前时间加一分钟:"+localTime2);
        LocalDateTime localTime3 = now.plusSeconds(1);
        System.out.println(" 给当前时间加一秒:"+localTime3);
        LocalDateTime localTime4 = now.plusNanos(1);
        System.out.println(" 给当前时间加一纳秒:"+localTime4);
        LocalDateTime localTime5 = now.minusHours(1);
        System.out.println(" 给当前时间减一小时:"+localTime5);
        LocalDateTime localTime6 = now.minusMinutes(1);
        System.out.println(" 给当前时间减一分钟:"+localTime6);
        LocalDateTime localTime7 = now.minusSeconds(1);
        System.out.println(" 给当前时间减一秒:"+localTime7);
        LocalDateTime localTime8 = now.minusNanos(1);
        System.out.println(" 给当前时间减一纳秒:"+localTime8);

        System.out.println("--------------------------修改时间--------------------------");
        LocalDateTime localDate1 = now.withYear(2020);
        System.out.println(" 修改日期对象年份为2020:"+localDate1);
        LocalDateTime localDate2 = now.withMonth(1);
        System.out.println(" 修改日期对象月份为1:"+localDate2);
        LocalDateTime localDate3 = now.withDayOfMonth(1);
        System.out.println(" 修改日期对象的日期(一月中的第几天):"+localDate3);
        LocalDateTime localDate4 = now.withDayOfYear(1);
        System.out.println(" 修改日期对象的日期(一年中的第几天):"+localDate4);
        LocalDateTime localTime9 = now.withHour(1);
        System.out.println(" 修改时间对象小时为1:"+localTime9);
        LocalDateTime localTime10 = now.withMinute(1);
        System.out.println(" 修改时间对象分钟为1:"+localTime10);
        LocalDateTime localTime11= now.withSecond(1);
        System.out.println(" 修改时间对象秒钟为1:"+localTime11);
        LocalDateTime localTime12 = now.withNano(1);
        System.out.println(" 修改时间对象纳秒为1:"+localTime12);


    }

    public static void main(String[] args) {
        System.out.println("今天的日期:"+nowDate());
        System.out.println("今天的时间:"+nowTime());
        System.out.println("今天的日期时间:"+now());
        System.out.println("今天的日期年月日字符串:"+nowDateString());
        System.out.println("今天的日期时间字符串:"+nowString());
        System.out.println("LocalDateTime 转 LocalDate:"+toLocalDate(now()));
        System.out.println("Date 转 LocalDateTime:"+dateToLocalTime(new Date()));
        System.out.println("LocalDateTime 转 Date:"+localTimeToDate(now()));
        System.out.println("获取当前时间:"+date());
        System.out.println("字符串转localDate:"+parseLocalDate("2023-11-15 09:34:50"));
        System.out.println("字符串转localDateTime:"+parseLocalDateTime("2023-11-15 09:34:50"));
        System.out.println("判断两个时间大小1:"+between(now(),now()));
        System.out.println("判断两个时间大小2:"+between(parseLocalDateTime("2023-11-15 09:34:50"),now()));
        System.out.println("判断两个时间大小3:"+between(parseLocalDateTime("2023-11-16 09:34:50"),now()));
        System.out.println("判断两个时间大小4:"+between(now().toLocalDate(),now().toLocalDate()));
        System.out.println("判断两个时间大小5:"+between(parseLocalDateTime("2023-11-14 09:34:50").toLocalDate(),now().toLocalDate()));
        System.out.println("判断两个时间大小6:"+between(parseLocalDateTime("2023-11-15 09:34:50").toLocalDate(),now().toLocalDate()));
        System.out.println("判断两个时间大小7:"+between(parseLocalDateTime("2023-11-16 09:34:50").toLocalDate(),now().toLocalDate()));
        System.out.println(localDateToDateTime(parseLocalDate("2023-11-15 09:34:50")));
        System.out.println(localDateAddTime(parseLocalDate("2023-11-15 09:34:50"),nowTime()));
        yearAndMonthAndDayAndHourDemo();
    }
}

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);
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值