Java:给定日期的迭代 [英] Java: Iteration of given Dates
问题描述
Wed Jan 05 00:00:00 CET 2011
Sat Jan 15 23:59:59 CET 2011
现在我想迭代他们,所以每天我可以执行一个 System.out.println()
,其中我在控制台上放置这样的日期:
2011-01-05
2011-01-06
2011-01-07
...
2011-01-13
2011-01-14
2011-01-15
我该怎么办这个?
最好的问候,Tim。
更新:
日历calend = Calendar.getInstance();
calend.setTime(myObject.getBeginDate());
日历beginCalendar = new GregorianCalendar(calend.get(Calendar.YEAR),calend.get(Calendar.MONTH),calend.get(Calendar.DATE));
calend.setTime(myObject.getEndDate());
日历endCalendar = new GregorianCalendar(calend.get(Calendar.YEAR),calend.get(Calendar.MONTH),calend.get(Calendar.DATE));
while(beginCalendar.compareTo(endCalendar)< = 0){
// ...计算
beginCalendar.add(Calendar.DATE,1);
}
使用GregorianCalendar对象递增一天一天
使用 SimpleDateFormat 。
要将日期从字符串转换为Date对象,您必须执行以下
SimpleDateFormat format = new SimpleDateFormat(EEE MMM dd HH:mm:ss z yyyy);
Date date = format.parse(yourDateString);
然后,您需要转换为GregorianCalendar,以便您可以轻松地增加值,最后输出日期使用另一个SimplerDateFormat以您想要的方式。
更新:
更新,在您的代码更新之后,您可以简单地执行以下
日历beginCalendar = Calendar.getInstance();
beginCalendar.setTime(myObject.getBeginDate());
日历endCalendar = Calendar.getInstance();
beginCalendar.setTime(myObject.getEndDate());
while(beginCalendar.compareTo(endCalendar)< = 0){
// ...计算
beginCalendar.add(Calendar.DATE,1) ;
}
I have two dates in Java:
Wed Jan 05 00:00:00 CET 2011
Sat Jan 15 23:59:59 CET 2011
Now I want to iterate over them, so that every day I can do a System.out.println()
in which I put the date in this kind on the console:
2011-01-05
2011-01-06
2011-01-07
...
2011-01-13
2011-01-14
2011-01-15
How can I do this?
Best Regards, Tim.
Update:
Calendar calend = Calendar.getInstance();
calend.setTime(myObject.getBeginDate());
Calendar beginCalendar = new GregorianCalendar(calend.get(Calendar.YEAR), calend.get(Calendar.MONTH), calend.get(Calendar.DATE));
calend.setTime(myObject.getEndDate());
Calendar endCalendar = new GregorianCalendar(calend.get(Calendar.YEAR), calend.get(Calendar.MONTH), calend.get(Calendar.DATE));
while (beginCalendar.compareTo(endCalendar) <= 0) {
// ... calculations
beginCalendar.add(Calendar.DATE, 1);
}
Use the GregorianCalendar object to increment one day at a time
Output using SimpleDateFormat.
To get your date from a string, into a Date object, you have to do the following
SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
Date date = format.parse(yourDateString);
Then, you need to convert into a GregorianCalendar, so that you can easily increment the values and finally output the date using another SimplerDateFormat in the way you want to. See the documentation for the different codes.
Update: Update, following your code update, you can simply do the following
Calendar beginCalendar = Calendar.getInstance();
beginCalendar.setTime(myObject.getBeginDate());
Calendar endCalendar = Calendar.getInstance();
beginCalendar.setTime(myObject.getEndDate());
while (beginCalendar.compareTo(endCalendar) <= 0) {
// ... calculations
beginCalendar.add(Calendar.DATE, 1);
}
这篇关于Java:给定日期的迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!