java 計算兩個時間相差幾天,獲取某天當月的第一天,最後一天

通過jdk8可以使用:

// 取本月第1天:
LocalDate firstDayOfThisMonth = today.with(TemporalAdjusters.firstDayOfMonth()); // 2017-03-01
// 取本月第2天:
LocalDate secondDayOfThisMonth = today.withDayOfMonth(2); // 2017-03-02
// 取本月最後一天,再也不用計算是28,29,30還是31:
LocalDate lastDayOfThisMonth = today.with(TemporalAdjusters.lastDayOfMonth()); // 2017-12-31
// 取下一天:
LocalDate firstDayOf2015 = lastDayOfThisMonth.plusDays(1); // 變成了2018-01-01
// 取2017年1月第一個週一,用Calendar要死掉很多腦細胞:
LocalDate firstMondayOf2015 = LocalDate.parse("2017-01-01").with(TemporalAdjusters.firstInMonth(DayOfWeek.MONDAY)); // 2017-01-02

/**
     * 獲取兩個時間之間的沒有最後一天
     * 
     * @param startDate
     * @param endDate
     * @return
     */
    public static List<String> getDayList(Date startDate, Date endDate) {
        if (startDate == null) {
            startDate = new Date();
        }
        if (endDate == null) {
            endDate = new Date();
        }

        List<String> dataList = new ArrayList<>();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate start = LocalDate.parse(format.format(startDate), df);
        LocalDate end = LocalDate.parse(format.format(endDate), df);
        while (true) {
            if (start.isAfter(end)) {
                break;
            }
            dataList.add(df.format(start.with(TemporalAdjusters.lastDayOfMonth())));
            int tmpYear = start.getYear();
            int tmpMonth = start.getMonthValue() + 1;
            if (end.getYear() == tmpYear && tmpMonth == end.getMonthValue()) {

            }
            int tmpDay = 1;
            start = LocalDate.of(tmpYear, tmpMonth, tmpDay);
        }
        return dataList;
    }

 

/**

* @Deprecated 計算兩天相差幾天
* @param day1較後
* @param day2較早
* @return
*/
private int numDays(String day1, String day2) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
try {
Date date1 = format.parse(day1);
Date date2 = format.parse(day2);
return (int) ((date1.getTime() - date2.getTime()) / (1000 * 3600 * 24));
} catch (ParseException e) {
this.logger.error("numDays計算相隔幾天錯誤" + day1 + "\t" + day2);
return 0;
}

}

 

// 獲取某天的前幾天

public String getSpecifiedDayBefore(String specifiedDay, int days) {

Calendar c = Calendar.getInstance();
Date date = null;
try {
date = new SimpleDateFormat("yy-MM-dd").parse(specifiedDay);
} catch (ParseException e) {
e.printStackTrace();
}
c.setTime(date);
int day = c.get(Calendar.DATE);
c.set(Calendar.DATE, day - days);


String dayBefore = new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
return dayBefore;
}

// 獲取某天的後幾天
public String getSpecifiedDayAfter(String specifiedDay, int days) {
Calendar c = Calendar.getInstance();
Date date = null;
try {
date = new SimpleDateFormat("yy-MM-dd").parse(specifiedDay);
} catch (ParseException e) {
e.printStackTrace();
}
c.setTime(date);
int day = c.get(Calendar.DATE);
c.set(Calendar.DATE, day + days);


String dayAfter = new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
return dayAfter;
}

// 獲取當月第一天,最後一天
public String[] getFirstLastDay(String time) {
if (StringUtil.isNullOrEmpty(time)) {
time = DateUtil.getCurDate();
}
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar = Calendar.getInstance();
try {
calendar.setTime(format.parse(time));
} catch (ParseException e) {
}
calendar.set(Calendar.DAY_OF_MONTH, 1);
Date firstDayOfMonth = calendar.getTime();
calendar.add(Calendar.MONTH, 1);
calendar.add(Calendar.DAY_OF_MONTH, -1);
Date lastDayOfMonth = calendar.getTime();
String[] fristLast = new String[] { format.format(firstDayOfMonth), format.format(lastDayOfMonth) };
return fristLast;
}

 

 

// 某天的前幾天

public static String getBeforDays(Date date, int days) {
long beforeTime = (date.getTime() / 1000) - 60 * 60 * 24 * days;
Date nextYear = new Date();
nextYear.setTime(beforeTime * 1000);
return DateUtil.formatDate(nextYear);
}
 

// 某天的後幾天
public static String getAfterDays(Date date, int days) {
long beforeTime = (date.getTime() / 1000) + 60 * 60 * 24 * days;
Date nextYear = new Date();
nextYear.setTime(beforeTime * 1000);
return DateUtil.formatDate(nextYear);
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章