如何根据一个日期得到下天的算法

2016-10-23  本文已影响15人  waj6701

心血来潮,写了一个算法,娱乐一下自己

```java

import java.util.Arrays;import java.util.Date;import java.util.List;public class NextDate {public static void main(String[] args) {Date date = new Date();date.setYear(116);date.setMonth(11);date.setDate(31);System.out.println(nextDate(date).toLocaleString());}public static Date nextDate(Date date) {//long l = date.getTime();//return new Date(l+24*60*60*1000);if (date==null) {return null;}if (isYearEnd(date)) {return new Date(date.getYear()+1,0,1);}else if (isSecondMonthEnd(date) || isOhterMonthEnd(date)) {return new Date(date.getYear(),date.getMonth()+1,1);}else {return new Date(date.getYear(),date.getMonth(),date.getDate()+1);}}private static boolean isYearEnd(Date date){if(date.getMonth()==11 && date.getDate()==31){return true;}else{return false;}}private static boolean isSecondMonthEnd(Date date){if(date.getMonth()!=1){return false;}else if (isLeapYear(date)) {return date.getDate()==29;}else{return false;}}//private static boolean isLeapYear(Date date){int year = date.getYear();if(year%4==0 && !(year%100==0) || year%400==0){return true;}else {return false;}}private static boolean isOhterMonthEnd(Date date){ListbigMonthList = Arrays.asList(new Integer[]{0,2,4,6,7,9,11});

if(bigMonthList.contains(date.getMonth())){

return date.getDate() == 31;

}

else{

return date.getDate() == 30;

}

}

}

```

上一篇下一篇

猜你喜欢

热点阅读