在Java中迭代日期 [英] Iterate over dates in java

查看:47
本文介绍了在Java中迭代日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要遍历一系列日期.不确定第二天如何进入for循环.我正在使用 java.util.Date .因此, plusDays(1)不能在for循环中用于获取下一个日期.

I need to iterate through a range of dates. Not sure on how to take next day in for loop. I am using java.util.Date. So plusDays(1) cannot be used in the for loop for taking next date.

在for循环中使用 date1 = new Date(date1.getTime()+(1000 * 60 * 60 * 24)).但是我认为通过每次在for循环中调用new来创建对象并不是一个好主意.

Used date1 = new Date(date1.getTime() + (1000 * 60 * 60 * 24)) in for loop. But I don't think its a good idea to create objects by calling new each time in for loop.

请提出一个更好的方法来参加第二天的循环比赛.

Please suggest a better way for taking next day in for loop.

推荐答案

tl; dr

LocalDate.now().plusDays( 1 )  // Get tomorrow's date.

避免使用旧式日期时间类.

请勿使用 java.util.Date .

该可怕的类在几年前被JSR 310中定义的现代 java.time 类所取代. Calendar GregorianCalendar SimpleDateFormat .

That terrible class was supplanted years ago by the modern java.time classes defined in JSR 310. Ditto for Calendar, GregorianCalendar, and SimpleDateFormat.

对于没有日期和时区的仅日期值,请使用 LocalDate 类.

For a date-only value, without time-of-day and without time zone, use LocalDate class.

调用 LocalDate.now 获取当前日期.

在任何特定时刻,日期都会随时区在全球范围内变化.因此,指定要用来查看日期的时区.如果省略,则会隐式应用JVM的当前默认时区.

For any particular moment, the date varies around the globe by time zone. So, specify the time zone by which you want to view the date. If omitted, you get the JVM’s current default time zone implicitly applied.

LocalDate today = LocalDate.now( ZoneId.of( "America/Montreal" ) ) ;  // Capture the current date as seen in a particular time zone.
for ( int i = 0 ; i < 7 ; i++ )
{
    LocalDate localDate = today.plusDays( i );
    System.out.println( "localDate = " + localDate );
}

运行时.

localDate = 2020-12-17
localDate = 2020-12-18
localDate = 2020-12-19
localDate = 2020-12-20
localDate = 2020-12-21
localDate = 2020-12-22
localDate = 2020-12-23

如果您熟悉Java流,请使用 LocalDate :: datesUntil .

If you are comfortable with Java streams, use LocalDate::datesUntil.

LocalDate today = LocalDate.now( ZoneId.of( "America/Montreal" ) );  // Capture the current date as seen in a particular time zone.
today.datesUntil( today.plusWeeks( 1 ) ).forEach( System.out :: println );


关于 java.time

旧版日期时间类,例如 SimpleDateFormat .


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

要了解更多信息,请参见 Oracle教程.并在Stack Overflow中搜索许多示例和说明.规范为 JSR 310 .

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Joda-Time 项目,现在位于<一个href ="https://en.wikipedia.org/wiki/Maintenance_mode" rel ="nofollow noreferrer">维护模式,建议迁移到您可以直接与数据库交换 java.time 对象.使用符合 JDBC驱动程序/jeps/170"rel =" nofollow noreferrer> JDBC 4.2 或更高版本.不需要字符串,不需要 java.sql.* 类.休眠5&JPA 2.2支持 java.time .

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes. Hibernate 5 & JPA 2.2 support java.time.

在哪里获取java.time类?

Where to obtain the java.time classes?

  • Most of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
  • 较新版本的Android(26+)捆绑了 java.time 类的实现.
  • 对于较早的Android(< 26),此过程称为 API废止 带来了子集 java.time 功能最初不是Android内置的.
    • Later versions of Android (26+) bundle implementations of the java.time classes.
    • For earlier Android (<26), a process known as API desugaring brings a subset of the java.time functionality not originally built into Android.
      • If the desugaring does not offer what you need, the ThreeTenABP project adapts ThreeTen-Backport (mentioned above) to Android. See How to use ThreeTenABP….

      这篇关于在Java中迭代日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆