DateUtil

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

public class DateUtil {

    public static final SimpleDateFormat DAY_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
    public static final SimpleDateFormat PRECISE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    public static final SimpleDateFormat WX_PAY_FORMAT = new SimpleDateFormat("yyyyMMddHHmmss");
    
    /**
     * 根据字符串格式的时间返回"yyyy-MM"格式的Calendar
     * @param dateTime
     * @return
     */
    public static Calendar getCalendarByStringTime(String dateTime) {
        Date date = null;
        try {
            date = new SimpleDateFormat("yyyy-MM").parse(dateTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        return calendar;
    }
    
    /**
     * 如2018-03-31到2017-12-01之间有4个月,注意,我们的月份开始是31号,结束时是01号,所以间距要加1
     * @param earlyDate 放较大的那个日期
     * @param lateDate 放较小的那个日期
     * @return
     */
    public static int calculateMonthIn(Date earlyDate, Date lateDate) {
          Calendar earlyCal = new GregorianCalendar();
          earlyCal.setTime(earlyDate);
          Calendar lateCal = new GregorianCalendar();
          lateCal.setTime(lateDate);
          int monthCount =
              (lateCal.get(Calendar.YEAR) - earlyCal.get(Calendar.YEAR)) * 12 + lateCal.get(Calendar.MONTH)
              - earlyCal.get(Calendar.MONTH) + 1;
          return monthCount;
    }
    
    /**
     * 上面方法的重载,可以放入String类型
     * @param earlyDate
     * @param lateDate
     * @return
     */
    public static int calculateMonthIn(String earlyDate,String  lateDate) {
        Date beginDate = null;
        Date endDate = null;
        try {
            beginDate = new SimpleDateFormat("yyyy-MM").parse(earlyDate);
            endDate = new SimpleDateFormat("yyyy-MM").parse(lateDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
          Calendar earlyCal = new GregorianCalendar();
          earlyCal.setTime(beginDate);
          Calendar lateCal = new GregorianCalendar();
          lateCal.setTime(endDate);
          int monthCount =
              (lateCal.get(Calendar.YEAR) - earlyCal.get(Calendar.YEAR)) * 12 + lateCal.get(Calendar.MONTH)
              - earlyCal.get(Calendar.MONTH) + 1;
          return monthCount;
    }
    
    public static String getFirstDayOfMonth() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, 0);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        String firstday = DAY_FORMAT.format(calendar.getTime());
        return firstday;
        
    }
    
    public static String getLastDayOfMonth() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, 1);
        calendar.set(Calendar.DAY_OF_MONTH, 0);
        String lastday = DAY_FORMAT.format(calendar.getTime());
        return lastday;
    }
    
    /**
     * 得到本月第一天,精确到时分秒
     */
    public static String getFirstDayTimeOfMonth()  {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        String firstDay = DAY_FORMAT.format(calendar.getTime());
        Date date = null;
        try {
            date = DAY_FORMAT.parse(firstDay);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        firstDay = PRECISE_FORMAT.format(date);
        return firstDay;
    }
    
    /**
     * 将日期精度的字符串转为时分秒精度的
     */
    public static String transferDayToTime(String dayStr)  {
        Date date = null;
        try {
            date = DAY_FORMAT.parse(dayStr);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
        return PRECISE_FORMAT.format(date);
    }
    
    /**
     * 将日期变为字符串
     */
    public static String transferDateToStr(Date date)  {
        return PRECISE_FORMAT.format(date);
    }

    /**
     * 将日期变为字符串
     */
    public static String wxPayDateFormat(Date date)  {
        return WX_PAY_FORMAT.format(date);
    }

    /**
     *将yyyy-MM-dd 转date
     * @param timeStr
     * @return
     */
    public static Date parseStringFormat (String timeStr) {
        Date parse = null;
        try {
            parse = DAY_FORMAT.parse(timeStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return parse;
    }

    /**
     * 将字符串变为日期
     */
    public static Date parseStrToDay(String strDate) {
        Date startDate = null;
        try {
            startDate = DAY_FORMAT.parse(strDate);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return startDate;
    }

    /**
     * 判断某个日期是否在某个时间区间
     */
    public static boolean isInDateInterval(Date current,Date beginDate,Date endDate) {
        long currentTime = current.getTime();
        long beginTime = beginDate.getTime();
        long endTime = endDate.getTime();
        return (currentTime >= beginTime) && (currentTime <= endTime);
    }

}

你可能感兴趣的:(DateUtil)