TimeUtil

package com.mx.operationcenter.webframework.web.util;

import lombok.SneakyThrows;
import lombok.experimental.UtilityClass;
import org.apache.commons.lang3.StringUtils;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;

/**
 * 工程包名:   com.mx.operationcenter.webframework.web.util
 * 项目名称:   mx
 * 创建描述:   heliangming 补充
 * Creator:     heliangming
 * Create_Date: 10:57 2019-08-14
 * Updater:     heliangming
 * Update_Date:10:57 2019-08-14
 * 更新描述:    heliangming 补充
 * <if test="begin != null">
 *    AND RELEASE_TIME &gt; #{begin}
 * </if>
 * <if test="end != null">
 *    AND RELEASE_TIME &lt; #{end}
 * </if>
 **/
@UtilityClass
public class TimeUtil {

    public static final DateTimeFormatter DEFAULT_FORMATTER_DAY = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    public static final DateTimeFormatter DEFAULT_FORMATTER_SS = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    public LocalDate string2LocalDate(String day){
        LocalDate localDate = null;
        if (StringUtils.isNotBlank(day)) {
            localDate = LocalDate.parse(day, DEFAULT_FORMATTER_DAY);
        }
        return localDate;
    }

    public LocalDateTime string2LocalDateTime(String day){
        return LocalDateTime.parse(day,DEFAULT_FORMATTER_SS);
    }

    public String localDateTime2String(LocalDateTime day){
        return day.format(DEFAULT_FORMATTER_DAY);
    }

    public LocalDateTime string2LocalDateTimeDayStrat(String day){
        return LocalDate.parse(day, DEFAULT_FORMATTER_DAY).atStartOfDay();
    }

    public LocalDateTime string2LocalDateTimeDayEnd(String day){
        return LocalDate.parse(day, DEFAULT_FORMATTER_DAY).plusDays(1L).atStartOfDay();
    }

    @SneakyThrows
    public Date string2Date(String str){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.parse(str);
    }

    @SneakyThrows//返回date的num天前后日期
    public String inferenceDay(String date,int num){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date2 = sdf.parse(date);
        Calendar cal = Calendar.getInstance();
        cal.setTime(date2);
        cal.add(Calendar.DATE, num);
        return new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
    }

    public LocalDateTime start(String startTime){
        return LocalDate.parse(startTime, DEFAULT_FORMATTER_DAY).atStartOfDay();
    }

    public LocalDateTime end(String endTime){
        return LocalDate.parse(endTime, DEFAULT_FORMATTER_DAY).plusDays(1).atStartOfDay();
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值