日期:2014-05-20 浏览次数:20689 次
Date date0 = new SimpleDateFormat("yyyy-MM-dd").parse("2008-10-01"); Date date1 = new SimpleDateFormat("yyyy-MM-dd").parse("2010-10-01"); Calendar cal = Calendar.getInstance(); cal.setTime(date0); while(cal.getTime().compareTo(date1)<=0){ //... cal.add(Calendar.DAY_OF_MONTH,1); }
------解决方案--------------------
SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd"); java.util.Date date= myFormatter.parse("2010-10-01"); java.util.Date mydate= myFormatter.parse("2008-10-01"); long day=(date.getTime()-mydate.getTime())/(24*60*60*1000); System.out.println("相差的日期:" + day);
------解决方案--------------------
import java.util.*; import java.text.*; public class DisplayDateEveryday { public static void main(String args[]){ display("2008-08-08", "2008-08-24"); } public static void display(String dateFirst, String dateSecond){ SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); try{ Date dateOne = dateFormat.parse(dateFirst); Date dateTwo = dateFormat.parse(dateSecond); Calendar calendar = Calendar.getInstance(); calendar.setTime(dateOne); while(calendar.getTime().before(dateTwo)){ System.out.println(dateFormat.format(calendar.getTime())); calendar.add(Calendar.DAY_OF_MONTH, 1); } } catch(Exception e){ e.printStackTrace(); } } }