LocalDateTime 处理

package com.xx.xx.common;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoField;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * LocaDateTime 日期型处理
 *
 *
 * 2019年5月21日
 */
public class LocalDateTimeUtil {
    
    public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
    public static final String YYYY_MM_DD = "yyyy-MM-dd";
    
    private static Map<String, DateTimeFormatter> formatterMap = new HashMap<>();

    /**
     * 获得DateTimeFormatter
     * @param pattern
     * @return
     *
     * @date 2019年5月30日
     */
    private static DateTimeFormatter getDateTimeFormatter(String pattern) {
        
        if(StringUtil.isBlank(pattern)) {
            return null;
        }
        
        DateTimeFormatter tempFormatter = formatterMap.get(pattern);
        
        if(tempFormatter == null) {
            tempFormatter = DateTimeFormatter.ofPattern(pattern.trim());
        }
        
        return tempFormatter;
    }
    
    /**
     * 将字符串转化为日期,目前只支持yyyy-MM-dd 或者 yyyy-MM-dd HH:mm:ss两种格式
     * @param dateTimeStr
     * @param defaultDateTime
     * @return
 
     */
    public static LocalDateTime strToDateTime(String dateTimeStr,LocalDateTime defaultDateTime) {
        if(StringUtil.isBlank(dateTimeStr)) {
            return defaultDateTime;
        }
        
        dateTimeStr = dateTimeStr.trim();
        int strLength = dateTimeStr.length();
        
        if(strLength == YYYY_MM_DD.length()) {
            dateTimeStr = dateTimeStr + " 00:00:00";
        }else {
            if(strLength != YYYY_MM_DD_HH_MM_SS.length()) {
                throw new RuntimeException("日期字符串格式不正确,只支持yyyy-MM-dd 或者 yyyy-MM-dd HH:mm:ss");
            }
        }
        
        DateTimeFormatter formatter = getDateTimeFormatter(YYYY_MM_DD_HH_MM_SS);
        
        try {
            return LocalDateTime.parse(dateTimeStr,formatter);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    
    /**
     * 获取日期的对应的天开始时间值
     * @param dateTime
     * @return
     *
     */
    public static LocalDateTime getDayStartTime(LocalDateTime dateTime) {
        if(dateTime == null) {
            dateTime = LocalDateTime.now();
        }
        
        dateTime = LocalDateTime.of(dateTime.getYear(), dateTime.getMonth(), dateTime.getDayOfMonth(),0,0);
        
        return dateTime;
    }
    
    /**
     * 获取日期对应的天截止时间值
     * @param dateTime
     * @return
   
     */
    public static LocalDateTime getDayEndTime(LocalDateTime dateTime) {
        if(dateTime == null) {
            dateTime = LocalDateTime.now();
        }
        
        dateTime = LocalDateTime.of(dateTime.getYear(), dateTime.getMonth(), dateTime.getDayOfMonth(),23,59,59);
        
        return dateTime;
    }
    
    /**
     * 给定年月是否为当前日期所在的年月
     * @param year
     * @param month
     * @return
 
     * @date 2019年6月3日
     */
    public static boolean isCurrentMonth(int year, int month){
        LocalDateTime currentTime = LocalDateTime.now();
        
        int nowYear = currentTime.get(ChronoField.YEAR);
        int nowMonth = currentTime.get(ChronoField.MONTH_OF_YEAR);
        
        boolean isCurrentMonth = false;
        
        if(nowYear != year) {
            return isCurrentMonth;
        }
        
        if(nowMonth != month) {
            return isCurrentMonth;
        }
        
        return true;
    }
    
    /**
     * 获取指定年月的开始日期
     * @param year
     * @param month
     * @return
 
     * @date 2019年6月3日
     */
    public static LocalDateTime getYearMonthBegin(int year, int month) {
        return LocalDateTime.of(year, month, 1, 0, 0);
    }
    
    /**
     * 获取指定年月的截止日期
     * @param year
     * @param month
     * @return

     * @date 2019年6月3日
     */
    public static LocalDateTime getYearMonthEnd(int year, int month) {
        LocalDateTime nextMonthStart = getYearMonthBegin(year, month+1);
        
        LocalDateTime endTime = nextMonthStart.minus(1, ChronoUnit.DAYS);
        
        return getDayEndTime(endTime);
    }
    
    /**
     * 获取昨天的日期
     * @return

     * @date 2019年6月3日
     */
    public static LocalDateTime getYestarday() {
        
        LocalDateTime currentTime = LocalDateTime.now();
        currentTime = currentTime.minus(1, ChronoUnit.DAYS);
        
        return currentTime;
    }
    
    /**
     * 获取昨天的日期最大值或者最小值
     * @param startOrEnd true为start;false为end
     * @return
 
     * @date 2019年6月3日
     */
    public static LocalDateTime getYestarday(boolean startOrEnd) {
        LocalDateTime currentTime = getYestarday();
        
        if(startOrEnd) {
            return getDayStartTime(currentTime);
        }
        
        return getDayEndTime(currentTime);
    }
    
    /**
     * 获取指定日期的上周时间
     * @param targetDateTime
     * @return

     * @date 2019年6月4日
     */
    public static LocalDateTime getLastWeek(LocalDateTime targetDateTime) {
        if(targetDateTime == null) {
            targetDateTime = LocalDateTime.now();
        }
        
//        System.out.println(targetDateTime.getDayOfWeek()+"\t"+targetDateTime.getDayOfWeek().getValue());
        
        targetDateTime = targetDateTime.minus(targetDateTime.getDayOfWeek().getValue(), ChronoUnit.DAYS);
        
        return targetDateTime;
    }
    
    /**
     * 获取指定日期上周最后时间
     * @param targetDateTime
     * @return

     * @date 2019年6月4日
     */
    public static LocalDateTime getLastWeekByEndTime(LocalDateTime targetDateTime) {
        targetDateTime = getLastWeek(targetDateTime);
        
        return getDayEndTime(targetDateTime);
        
    }
    
    /**
     * 获取指定日期上周起始时间
     * @param targetDateTime
     * @return

     * @date 2019年6月4日
     */
    public static LocalDateTime getLastWeekByStartTime(LocalDateTime targetDateTime) {
        if(targetDateTime == null) {
            targetDateTime = LocalDateTime.now();
        }
        
        targetDateTime = targetDateTime.minus( 6 + targetDateTime.getDayOfWeek().getValue(), ChronoUnit.DAYS);
        
        return getDayStartTime(targetDateTime);
    }
    
    /**
     * LocalDateTime 转化为java.util.Date
     * @param targetDateTime
     * @return

     * @date 2019年6月5日
     */
    public static Date toDate(LocalDateTime  targetDateTime) {
        if(targetDateTime == null) {
            return null;
        }
        
        ZonedDateTime zoneDateTime = targetDateTime.atZone(ZoneId.systemDefault());
        Instant instant = zoneDateTime.toInstant();
        
        return Date.from(instant);
    }
    
    /**
     *  java.util.Date转化为LocalDateTime
     * @param targetDate
     * @return

     * @date 2019年6月5日
     */
    public static LocalDateTime fromDate(Date targetDate) {
        if(targetDate == null) {
            return null;
        }
        
        Instant instant = targetDate.toInstant();
        
        return LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
    }
    
    @SuppressWarnings("unused")
    private static class TClass{
        
        public static void main(String[] args) {
            
//            method1();
//            method2();
//            method3();
//            method4();
//            method5();
//            method6();
            method7();
            
        }
        
        private static void method1() {
            
            System.out.println(strToDateTime("2019-05-30", LocalDateTime.now()));
            
            System.out.println(LocalDateTime.now().format( DateTimeFormatter.ofPattern("yyyy-MM-dd")));// HH:mm:ss
            
//            System.out.println(LocalDateTime.parse("2019/05-32 00:00:00", DateTimeFormatter.ofPattern("yyyy/MM-dd HH:mm:ss")));
            
            LocalDateTime dateTime =  strToDateTime("2019-01-30", LocalDateTime.now());
            dateTime = dateTime.plus(1, ChronoUnit.MONTHS);
            System.out.println(dateTime);
        }
        
        private static void method2() {
            LocalDateTime dateTime = null ;
            
            System.out.println(getDayStartTime(dateTime));
            System.out.println(getDayEndTime(dateTime));
            
            dateTime = strToDateTime("2019-05-30", null) ;
            System.out.println(getDayStartTime(dateTime));
            System.out.println(getDayEndTime(dateTime));
        }
        
        private static void method3() {
            DateTimeFormatter dateTimeFormatter = getDateTimeFormatter("HH:mm:ss");//YYYY_MM_DD
//            
//            System.out.println(dateTimeFormatter.format(LocalDateTime.now()));
            System.out.println(dateTimeFormatter.format(LocalDate.now()));
//            System.out.println(dateTimeFormatter.format(LocalTime.now()));
            
        }
        
        private static void method4() {
            System.out.println(isCurrentMonth(2015, 2));
            System.out.println(isCurrentMonth(2019, 6));
        }
        
        private static void method5() {
            System.out.println(getYearMonthBegin(2019, 2));
            System.out.println(getYearMonthEnd(2019, 3));
            
            System.out.println(getYestarday());
            System.out.println(getYestarday(true));
            System.out.println(getYestarday(false));
        }
        
        private static void method6() {
            
            System.out.println(getLastWeek(null)+"\t"+getLastWeekByEndTime(null)+"\t"+getLastWeekByStartTime(null));
            
            LocalDateTime targetDateTime ;
            
            targetDateTime = strToDateTime("2019-05-19", null);
            System.out.println(getLastWeek(targetDateTime)+"\t"+getLastWeekByEndTime(targetDateTime)+"\t"+getLastWeekByStartTime(targetDateTime));
            
        }
        
        private static void method7() {
            System.out.println(toDate(LocalDateTime.now()));
            System.out.println(toDate(strToDateTime("2015-09-15 15:15:15", null)));
            System.out.println(fromDate(new Date()));
            
        }
        
    }
    
    
    

}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值