简体   繁体   English

如何遍历一系列日期?

[英]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. DateCalendar都没有格式。 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

java.time时间

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 & 7Android )。

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:提示:

  • Do most of your work in objects like LocalDate rather than Strings.在像LocalDate而不是 Strings 这样的对象中完成大部分工作。 For business logic, use smart objects.对于业务逻辑,请使用智能对象。 When you need to present user, then generate Strings in a certain format.当您需要呈现用户时,则生成某种格式的字符串。
  • When exchanging data or serializing values, if at all possible, use standard ISO 8601 formats rather than inventing your own such as yyyy.MM.dd .在交换数据或序列化值时,如果可能,请使用标准 ISO 8601 格式,而不是自己发明,例如yyyy.MM.dd

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM