[英]How to Iterate through a range of dates?
I am trying to iterate through strings of dates.我正在尝试遍历日期字符串。 The strings are in the format like this 2000.07.28.
字符串的格式类似于 2000.07.28。 I tried converting them to doubles but then it complains.
我尝试将它们转换为双打,但后来它抱怨了。 I also need to keep the same format for the dates.
我还需要为日期保持相同的格式。 Because I need to print certain dates.
因为我需要打印某些日期。 I tried this:
我试过这个:
String begDate = "1999.11.18";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd");
Date start = sdf.parse("2010.01.01");
Date end = sdf.parse("2010.04.01");
GregorianCalendar gcal = new GregorianCalendar();
gcal.setTime(start);
while (!gcal.getTime().after(end))
{
Date d = gcal.getTime();
System.out.println(d);
gcal.add(Calendar.MONTH, 1);
}
But while it does iterate through the dates correctly it changes the date format.但是,虽然它确实正确地遍历日期,但它会更改日期格式。
Date
/ Calendar
have no format Date
/ Calendar
没有格式You seem to be confusing the Date
/ Calendar
object with the textual representation of its value generated as a String by the toString
method.您似乎将
Date
/ Calendar
对象与其由toString
方法生成为 String 的值的文本表示相混淆。 Neither Date
nor Calendar
have a format. Date
和Calendar
都没有格式。 Each has a toString
method you are calling implicitly in your line System.out.println( d );
每个都有一个你在
System.out.println( d );
行中隐式调用的toString
方法System.out.println( d );
. . The format used by
toString
when generating a String is its own default format pattern, not the format pattern you specified while parsing. toString
在生成 String 时使用的格式是它自己的默认格式模式,而不是您在解析时指定的格式模式。
This line:这一行:
System.out.println( d );
…is a shortcut for this next line, with the implicit call to toString
made explicit: ...是下一行的快捷方式,隐式调用
toString
显式:
System.out.println( d.toString() );
That toString
method uses its own format (a clumsy, poorly designed format) of the form dow mon dd hh:mm:ss zzz yyyy
.该
toString
方法使用自己的格式(一种笨拙、设计不佳的格式),格式为dow mon dd hh:mm:ss zzz yyyy
。
You are using old legacy classes that have proven to be poorly design, confusing, and troublesome.您正在使用旧的遗留类,这些类已被证明设计不佳、令人困惑且麻烦。 They have been supplanted by the java.time framework built into Java 8 and later (and back-ported to Java 6 & 7 and to Android ).
它们已被内置于 Java 8 及更高版本中的java.time框架所取代(并且向后移植到 Java 6 & 7和Android )。
For a date-only value without time-of-day and without time zone, use the LocalDate
class.对于没有时间和时区的仅日期值,请使用
LocalDate
类。
To specify a formatting pattern for parsing, use the DateTimeFormatter
class.要指定用于解析的格式模式,请使用
DateTimeFormatter
类。
This example code spits out LocalDate
values represented textually in your desired format.此示例代码以您所需的格式输出以文本形式表示的
LocalDate
值。 At the same time this code collects each LocalDate
in the sequence in a List
.同时,这段代码收集
List
序列中的每个LocalDate
。
DateTimeFormatter formatter = DateTimeFormatter.ofPattern( "yyyy.MM.dd" );
LocalDate start = sdf.parse("2010.01.01");
LocalDate end = sdf.parse("2010.04.01");
LocalDate localDate = start;
List<LocalDate> dates = new ArrayList<>();
int i = 0;
while ( ! localDate.isAfter( end ) ) {
i = i + 1;
dates.add( localDate );
String output = localDate.format( formatter );
System.out.println( "LocalDate # " + i + " : " + output );
// Set up next loop.
localDate = localDate.plusDays( 1 );
}
System.out.println( "dates : " + dates.toString() );
When printing the List
of LocalDate
objects, notice each LocalDate
object's LocalDate::toString
method generates a String using sensible formats from the ISO 8601 standard.打印
LocalDate
对象List
时,请注意每个LocalDate
对象的LocalDate::toString
方法使用ISO 8601标准中的合理格式生成一个 String。
Tips:提示:
LocalDate
rather than Strings.LocalDate
而不是 Strings 这样的对象中完成大部分工作。 For business logic, use smart objects.yyyy.MM.dd
.yyyy.MM.dd
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.