DateUtils.java 2.55 KB
package cn.com.glsx.dj.taskcenter.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.temporal.TemporalAdjusters;
import java.util.Calendar;
import java.util.Date;

public class DateUtils extends cn.com.glsx.framework.core.util.DateUtils {
    private static DateFormat dateFormat= new SimpleDateFormat(DATE_FORMAT);

    public static Integer getMonthInt(Date date){
        Calendar now = Calendar.getInstance();
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            now.setTime(dateFormat.parse(formatDate(date)));
            int month = now.get(2) + 1;
            return month;
        } catch (ParseException var4) {
            var4.printStackTrace();
            return null;
        }
    }

    public static Integer getYearInt(Date date) {
        Calendar cl = Calendar.getInstance();
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            cl.setTime(dateFormat.parse(formatDate(date)));
            return cl.get(1);
        } catch (ParseException var3) {
            var3.printStackTrace();
            return 0;
        }
    }
    /**
     * 
     * @Description: 获取当月开始时间 yyyy-MM-01 00:00:00
     *  @param yearMonth yyyy-MM
     *  @return
     */
    public static String getMonthFirstTime(String yearMonth) {
    	return yearMonth.concat("-01 00:00:00");
    }
    
    /**
     * 
     * @Description: 获取当月开始时间 yyyy-MM-(28-31) 23:59:59
     *  @param yearMonth yyyy-MM
     *  @return
     */
    public static String getMonthEndtTime(String yearMonth) {
    	Date date = parseDate(getMonthFirstTime(yearMonth), "yyyy-MM-dd HH:mm:ss");
    	return getOffsetDate(getOffsetDate(date, 1, Calendar.MONTH), -1,Calendar.SECOND,"yyyy-MM-dd HH:mm:ss");
    }
    
    /**
     * 获取当前月的第一天
     */
    public static Date getFirstDayCurrentMonth() {
        LocalDateTime firstDayOfMonth = LocalDateTime.of(LocalDate.from(LocalDateTime.now().with(TemporalAdjusters.firstDayOfMonth())), LocalTime.MIN);
        return Date.from(firstDayOfMonth.atZone(ZoneId.systemDefault()).toInstant());
    }

    public static Date getLastDayCurrentMonth() {
        LocalDateTime lastDayOfMonth = LocalDateTime.of(LocalDate.from(LocalDateTime.now().with(TemporalAdjusters.lastDayOfMonth())), LocalTime.MAX);
        return Date.from(lastDayOfMonth.atZone(ZoneId.systemDefault()).toInstant());
    }
}