时间的工具类

/**
 * 获取某天的开始时刻
 * @param date
 * @return
 */
public static Date getStartOfDay(Date date) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    int year = calendar.get(Calendar.YEAR);
    int month = calendar.get(Calendar.MONTH);
    int day = calendar.get(Calendar.DATE);
    calendar.set(year, month, day, 0, 0, 0);
    return calendar.getTime();
}


/**
 * 获取某天的结束时刻
 * @param date
 * @return
 */
public static Date getEndOfDay(Date date) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    int year = calendar.get(Calendar.YEAR);
    int month = calendar.get(Calendar.MONTH);
    int day = calendar.get(Calendar.DATE);
    calendar.set(year, month, day, 23, 59, 59);
    return calendar.getTime();

}

/**
 * 获取当前天的前第N天
 * @param n
 * @return
 */
public static Date minusDay(Integer n) {
    Calendar calendar = Calendar.getInstance();
    int day = calendar.get(Calendar.DATE);
    calendar.set(Calendar.DATE, day - n);
    return calendar.getTime();

}

/**
 * 获取周四的时间
 */
public static List<Date> thursday(){
    List<Date> date=new ArrayList<Date>();
    Calendar calendar = new GregorianCalendar();
    Calendar calendarEnd = Calendar.getInstance();
    int year = calendarEnd.get(Calendar.YEAR);
    int month = calendarEnd.get(Calendar.MONTH);
    int day = calendarEnd.get(Calendar.DATE);
    calendar.set(Calendar.YEAR, year-1);
    calendar.set(Calendar.MONTH,month);
    calendar.set(Calendar.DAY_OF_MONTH, day);
    while(calendar.getTime().getTime()<=calendarEnd.getTime().getTime()){
        if(calendar.get(Calendar.DAY_OF_WEEK)==4){
            date.add(calendar.getTime());
        }
        calendar.add(Calendar.DAY_OF_MONTH, 1);
    }
    return date;
}

阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭