package weaver.mfbdt.util;

import weaver.common.DateUtil;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 类的详细说明
 *
 * @author wangj
 * @version 1.00版本
 * @Date 2023/9/14
 */

public class TimeUtil {
    /**
     * 获取一小时前 的 日期-时间
     * @param datestr
     * @return
     */
    public static String getBeforeHourDate(String datestr){
        Date datec = weaver.general.TimeUtil.getString2Date(datestr, "yyyy-MM-dd HH:mm:ss");
        long time = datec.getTime();

        time = time - 3600000;

        Date date1 = new Date(time);
        // 格式化日期对象为所需的时间字符串
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String formattedDate = dateFormat.format(date1);

        // 打印前一个小时的时间
        return formattedDate;
    }

    public static String getBeforeDate(String datestr) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

        Date specifiedDate = null;
        try {
            specifiedDate = dateFormat.parse(datestr);
        } catch (ParseException e) {
            e.getMessage();
        }

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(specifiedDate);
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        String previousDay = dateFormat.format(calendar.getTime());
        return previousDay;

    }

//    public static void main(String[] args) throws ParseException {
//        System.out.println(getBeforeDate("2023-09-01"));
//    }

}
