版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。package demo;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
* 遍历两个日期之间天数的算法
public class MyTest {
public static void main(String[] args) throws ParseException {
String start = "2007-01-27";
String end = "2008-03-04";
//字符串转换成日期
SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
Date startDate=format.parse(start);
Calendar startTime=Calendar.getInstance();
startTime.clear();
startTime.setTime(startDate);
int startYear = startTime.get(Calendar.YEAR);
int startMonth = startTime.get(Calendar.MONTH);
int startDay = startTime.get(Calendar.DAY_OF_MONTH);
Date endDate=format.parse(end);
Calendar endTime=Calendar.getInstance();
endTime.clear();
endTime.setTime(endDate);
int endYear = endTime.get(Calendar.YEAR);
int endMonth = endTime.get(Calendar.MONTH);