如何取得某年某月某日到某年某月某日的日期,要求返回类型List<Date>
2个回答
展开全部
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
public class Du2 {
public static void main(String[] args) {
int date1 = 20111001;
int date2 = 20111101;
List<Date> list = getJulianDate(date1, date2);
for(Date date: list){
System.out.println(date.toLocaleString());
}
}
private static List<Date> getJulianDate(int date1, int date2) {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, date1 / 10000);
cal.set(Calendar.MONTH, date1 /100 % 100 - 1);
cal.set(Calendar.DATE, date1 % 100);
final SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
List<Date> list = new ArrayList<Date>();
while(Integer.parseInt(sdf.format(cal.getTime())) <= date2){
list.add(cal.getTime());
cal.add(Calendar.DATE, 1);
}
return list;
}
}
--------------testing
Oct 1, 2011 5:57:54 PM
Oct 2, 2011 5:57:54 PM
Oct 3, 2011 5:57:54 PM
Oct 4, 2011 5:57:54 PM
Oct 5, 2011 5:57:54 PM
Oct 6, 2011 5:57:54 PM
Oct 7, 2011 5:57:54 PM
Oct 8, 2011 5:57:54 PM
Oct 9, 2011 5:57:54 PM
Oct 10, 2011 5:57:54 PM
Oct 11, 2011 5:57:54 PM
Oct 12, 2011 5:57:54 PM
Oct 13, 2011 5:57:54 PM
Oct 14, 2011 5:57:54 PM
Oct 15, 2011 5:57:54 PM
Oct 16, 2011 5:57:54 PM
Oct 17, 2011 5:57:54 PM
Oct 18, 2011 5:57:54 PM
Oct 19, 2011 5:57:54 PM
Oct 20, 2011 5:57:54 PM
Oct 21, 2011 5:57:54 PM
Oct 22, 2011 5:57:54 PM
Oct 23, 2011 5:57:54 PM
Oct 24, 2011 5:57:54 PM
Oct 25, 2011 5:57:54 PM
Oct 26, 2011 5:57:54 PM
Oct 27, 2011 5:57:54 PM
Oct 28, 2011 5:57:54 PM
Oct 29, 2011 5:57:54 PM
Oct 30, 2011 5:57:54 PM
Oct 31, 2011 5:57:54 PM
Nov 1, 2011 5:57:54 PM
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
public class Du2 {
public static void main(String[] args) {
int date1 = 20111001;
int date2 = 20111101;
List<Date> list = getJulianDate(date1, date2);
for(Date date: list){
System.out.println(date.toLocaleString());
}
}
private static List<Date> getJulianDate(int date1, int date2) {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, date1 / 10000);
cal.set(Calendar.MONTH, date1 /100 % 100 - 1);
cal.set(Calendar.DATE, date1 % 100);
final SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
List<Date> list = new ArrayList<Date>();
while(Integer.parseInt(sdf.format(cal.getTime())) <= date2){
list.add(cal.getTime());
cal.add(Calendar.DATE, 1);
}
return list;
}
}
--------------testing
Oct 1, 2011 5:57:54 PM
Oct 2, 2011 5:57:54 PM
Oct 3, 2011 5:57:54 PM
Oct 4, 2011 5:57:54 PM
Oct 5, 2011 5:57:54 PM
Oct 6, 2011 5:57:54 PM
Oct 7, 2011 5:57:54 PM
Oct 8, 2011 5:57:54 PM
Oct 9, 2011 5:57:54 PM
Oct 10, 2011 5:57:54 PM
Oct 11, 2011 5:57:54 PM
Oct 12, 2011 5:57:54 PM
Oct 13, 2011 5:57:54 PM
Oct 14, 2011 5:57:54 PM
Oct 15, 2011 5:57:54 PM
Oct 16, 2011 5:57:54 PM
Oct 17, 2011 5:57:54 PM
Oct 18, 2011 5:57:54 PM
Oct 19, 2011 5:57:54 PM
Oct 20, 2011 5:57:54 PM
Oct 21, 2011 5:57:54 PM
Oct 22, 2011 5:57:54 PM
Oct 23, 2011 5:57:54 PM
Oct 24, 2011 5:57:54 PM
Oct 25, 2011 5:57:54 PM
Oct 26, 2011 5:57:54 PM
Oct 27, 2011 5:57:54 PM
Oct 28, 2011 5:57:54 PM
Oct 29, 2011 5:57:54 PM
Oct 30, 2011 5:57:54 PM
Oct 31, 2011 5:57:54 PM
Nov 1, 2011 5:57:54 PM
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询