java

时间:2014-12-27 02:16:02   收藏:0   阅读:186

今天有时间写一个计算两个日期之间有多少个日期,在这儿献丑了。呵呵,不说废话了,请看源码:

//获取某段日期之间的日期集合
??? public static List<String> getDateStringListByBetweenTwoDates(String beginDate,String endDate){
??? ??? List<String> dateList = new ArrayList<String>();
??? ??? int maxRound = 366;//hardcode
??????? int round = 0;
??????? String nextDate = beginDate;
??????? dateList.add(nextDate);
??????? while ((nextDate != null && !nextDate.equals(endDate)) && round++ < maxRound){
??????????? nextDate = DateStringAdd(nextDate, 1);
??????????? dateList.add(nextDate);
??????? }
??? ??? return dateList;
??? }

public static String DateStringAdd(String dateString, int add){
??????? String sret = "";
??????? SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
??????? if (dateString == null)
??????? ??? return null;
??????? if (add == 0)
??????? ??? return dateString;
??????? if (!dateString.startsWith("1") && !dateString.startsWith("2"))
??????? ??? return null;
??????? if(add <0)
??????? ??? return null;
??????? try {
??? ??? ??? Date date = sf.parse(dateString);
??? ??? ??? int year = date.getYear()+1900;
??? ??? ??? int month = date.getMonth()+1;
??? ??? ??? int day = date.getDate();
??? ??? ??? switch(month){
??? ??? ??? ??? case 1:
??? ??? ??? ??? case 3:
??? ??? ??? ??? case 5:
??? ??? ??? ??? case 7:
??? ??? ??? ??? case 8:
??? ??? ??? ??? case 10:
??? ??? ??? ??? ??? if(day+add>31){
??? ??? ??? ??? ??? ??? month++;
??? ??? ??? ??? ??? ??? day=1;
??? ??? ??? ??? ??? }else{
??? ??? ??? ??? ??? ??? day=day+add;
??? ??? ??? ??? ??? }
??? ??? ??? ??? ??? break;
??? ??? ??? ??? case 12:
??? ??? ??? ??? ??? if(month==12 && day+add>31){
??? ??? ??? ??? ??? ??? year++;
??? ??? ??? ??? ??? ??? month=1;
??? ??? ??? ??? ??? ??? day=1;
??? ??? ??? ??? ??? }else{
??? ??? ??? ??? ??? ??? day=day+add;
??? ??? ??? ??? ??? }
??? ??? ??? ??? ??? break;
??? ??? ??? ??? case 4:
??? ??? ??? ??? case 6:
??? ??? ??? ??? case 9:
??? ??? ??? ??? case 11:
??? ??? ??? ??? ??? if(day+add>30){
??? ??? ??? ??? ??? ??? month++;
??? ??? ??? ??? ??? ??? day=1;
??? ??? ??? ??? ??? }else{
??? ??? ??? ??? ??? ??? day=day+add;
??? ??? ??? ??? ??? }
??? ??? ??? ??? ??? break;
??? ??? ??? ??? case 2:
??? ??? ??? ??? ??? if(year%400==0||(year%4==0 && year%100!=0)){
??? ??? ??? ??? ??? ??? if(day+add>29){
??? ??? ??? ??? ??? ??? ??? month++;
??? ??? ??? ??? ??? ??? ??? day=1;
??? ??? ??? ??? ??? ??? }else{
??? ??? ??? ??? ??? ??? ??? day=day+add;
??? ??? ??? ??? ??? ??? }
??? ??? ??? ??? ??? }else{
??? ??? ??? ??? ??? ??? if(day+add>28){
??? ??? ??? ??? ??? ??? ??? month++;
??? ??? ??? ??? ??? ??? ??? day=1;
??? ??? ??? ??? ??? ??? }else{
??? ??? ??? ??? ??? ??? ??? day=day+add;
??? ??? ??? ??? ??? ??? }
??? ??? ??? ??? ??? }
??? ??? ??? ??? ??? break;
??? ??? ??? }
??? ??? ??? if(month<10){
??? ??? ??? ??? sret = year+"-0"+month;
??? ??? ??? }else{
??? ??? ??? ??? sret = year+"-"+month;
??? ??? ??? }
??? ??? ??? if(day<10){
??? ??? ??? ??? sret = sret+"-0"+day;
??? ??? ??? }else{
??? ??? ??? ??? sret = sret+"-"+day;
??? ??? ??? }
??? ??? } catch (ParseException e) {
??? ??? ??? e.printStackTrace();
??? ??? }
???????
??????? return sret;
??? }

原文:http://201108161636.iteye.com/blog/2170358

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!