Is there any way in JodaTime to construct a Date/DateTime which will always be smaller/larger than any other Date/DateTime? Something along the lines of
JodaTime中是否有方法构造一个日期/日期时间,它总是比任何其他日期/日期时间都要小/大?类似的东西
DateTime bigBang = DateTime.xxx();
DateTime endOfUniverse = DateTime.yyy();
Constraint: I don't want to use the standard Java Date libraries.
约束条件:我不想使用标准的Java日期库。
3 个解决方案
#1
10
java.time
The Joda-Time project is now in maintenance mode. The team advises migration to the java.time classes.
Joda-Time项目现在处于维护模式。团队建议迁移到java。时间类。
For min/max in java.time, see my Answer on a similar Question.
最小/最大的java。时间,看看我对类似问题的回答。
Joda-Time
Joda-Time tracks time as a count of milliseconds since the epoch of first moment of 1970 in UTC. This count is kept using a 64-bit long
integer. So, technically, the maximum and minimums are the +/- limits of a long
.
Joda-Time跟踪时间是自1970年第一个时刻UTC开始以来的毫秒数。这个计数使用一个64位长整数。因此,从技术上讲,最大值和最小值是一个long的+/-极限。
… new DateTime( Long.MIN_VALUE )
… new DateTime( Long.MAX_VALUE )
Joda-Time has no such minimum/maximum values available conveniently as constants. In contrast, note that Joda-Time’s successor, java.time built into Java 8 and later, does indeed offer the constants LocalDateTime.MIN
and LocalDateTime.MAX
.
Joda-Time没有像常量那样的最小/最大值。相反,请注意Joda-Time的继任者java。Java 8和以后构建的时间确实提供了LocalDateTime常量。分钟,LocalDateTime.MAX。
By the way, the Joda-Time team has advised we should migrate to java.time. Much of the java.time functionality is back-ported to Java 6 & 7 in the ThreeTen-Backport, further adapted to Android in ThreeTen-ABP.
顺便说一下,Joda-Time团队建议我们应该迁移到java.time。大部分java。在ThreeTen-Backport中,时间功能被反向移植到Java 6 & 7,并在3 - abp中进一步适应Android。
Too big, too small
Beware of these extremes. Their use is not practical. Various libraries, apps, databases, and other sinks/sources of date-time values may have much different limits, some much larger but typically much smaller.
当心这些极端。它们的使用是不实用的。不同的库、应用程序、数据库和日期时间值的其他接收器/来源可能有很多不同的限制,有些更大,但通常更小。
For example, many systems use the old tradition from UNIX & POSIX of tracking time as a 32-bit integer count of whole seconds since 1970-01-01T00:00:00Z. The natural limit of +/- two billion seconds results in the looming Year 2038 Problem.
例如,许多系统使用UNIX和POSIX的跟踪时间的旧传统作为自1970-01-01 - 01t00:00:00 z以来的32位整秒计数。20亿秒的自然极限导致了2038年的问题。
Another limit is the physical display size of fields on forms and reports that expect only four digits in a year number.
另一个限制是表单和报表上的字段的物理显示大小,这些字段在一年内只能显示4位数。
Workaround
You can define your own min/max.
你可以定义你自己的最小值/最大值。
You may want extreme values such as year 0000 and year 9999. Joda-Time supports years later than 9,999 but I would stick with 4 digits to fit the commonly used formats for display on-screen and in reports. Visually, the four nines stand out as a bogus date.
您可能需要极端值,比如年份0000和年份9999。Joda-Time支持的时间比9999晚了好几年,但我会坚持使用4位数字,以适应屏幕和报表中常用的显示格式。从视觉上看,这四个9代表着一个虚假的约会。
Or you may want an expected minimum value appropriate to your business logic. If building a new invoicing system, then you know the year should always be this year or later.
或者您可能想要一个适合您的业务逻辑的期望最小值。如果构建一个新的发票系统,那么您就知道年份应该是今年或更晚。
I suggest defining constants on a helper class. Something like this:
我建议在helper类上定义常量。是这样的:
package com.example;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
public class JodaTimeHelper {
static final public DateTime START_OF_TIME = new DateTime( 0000, 1, 1, 0, 0, 0, DateTimeZone.UTC );
static final public DateTime END_OF_TIME = new DateTime( 9999, 1, 1, 0, 0, 0, DateTimeZone.UTC );
static final public DateTime MINIMUM_INVOICE_DATETIME = new DateTime( 2015, 1, 1, 0, 0, 0, DateTimeZone.UTC );
}
Here is the syntax for calling those constants.
下面是调用这些常量的语法。
System.out.println( "START_OF_TIME: " + JodaTimeHelper.START_OF_TIME );
System.out.println( "END_OF_TIME: " + JodaTimeHelper.END_OF_TIME );
System.out.println( "END_OF_TIME: " + JodaTimeHelper. MINIMUM_INVOICE_DATETIME );
When run.
运行时。
START_OF_TIME: 0000-01-01T00:00:00.000Z
END_OF_TIME: 9999-01-01T00:00:00.000Z
MINIMUM_INVOICE_DATETIME: 2015-01-01T00:00:00.000Z
#2
2
When all dates are within the same TimeZone, you can create DateTime
object with fields assigned to minimum or max value.
However when using this constructor
当所有的日期都在同一个时区时,您可以创建DateTime对象,该对象的字段分配为最小值或最大值。但是在使用这个构造函数时
DateTime(int year, int monthOfYear, int dayOfMonth, int hourOfDay, int minuteOfHour, int secondOfMinute)
with Years.MAX_VALUE.getYears()
I have below exception:
max_value.getyears()我有以下例外:
So using the max number of years from exception, I have came up with the following end-of-universe dateTime:
因此,利用exception的最大年数,我提出了以下端点时间:
DateTime dtMax = new DateTime(292278993, 12, 31, 23, 59, 59);
System.out.println(dtMax);
// prints 292278993-12-31T23:59:59
See documentation for more details.
Also an interesting discussion can be read here.
有关更多细节,请参阅文档。这里还有一个有趣的讨论。
#3
0
After reading through lots of different tangents on this, I finally decided to figure out a minimal case that works for both the minimum and maximum for the UTC time zone. And it turns out that attempting to use Long
's MIN_VALUE
and MAX_VALUE
send me down rabbit trails.
在阅读了许多不同的切线之后,我最终决定找出一个适用于UTC时区的最小和最大值的最小情况。结果是,尝试使用Long的MIN_VALUE和MAX_VALUE将我发送到兔子路径。
So, given the following code snippet:
因此,鉴于以下代码片段:
new DateTime(milliseconds, DateTimeZone.UTC)
here are the minimum/maximum values that work for milliseconds
(tested in Java 1.7):
以下是在毫秒内工作的最小/最大值(在Java 1.7中测试):
UTC_DATE_TIME_MIN_VALUE_IN_MILLIS = 9223372017129599999L
UTC_DATE_TIME_MAX_VALUE_IN_MILLIS = -9223372017043200000L
Both of these values are approximately 20 billion away from Long.MIN_VALUE
and Long.MAX_Value
.
这两个值离Long大约有200亿。MIN_VALUE Long.MAX_Value。
#1
10
java.time
The Joda-Time project is now in maintenance mode. The team advises migration to the java.time classes.
Joda-Time项目现在处于维护模式。团队建议迁移到java。时间类。
For min/max in java.time, see my Answer on a similar Question.
最小/最大的java。时间,看看我对类似问题的回答。
Joda-Time
Joda-Time tracks time as a count of milliseconds since the epoch of first moment of 1970 in UTC. This count is kept using a 64-bit long
integer. So, technically, the maximum and minimums are the +/- limits of a long
.
Joda-Time跟踪时间是自1970年第一个时刻UTC开始以来的毫秒数。这个计数使用一个64位长整数。因此,从技术上讲,最大值和最小值是一个long的+/-极限。
… new DateTime( Long.MIN_VALUE )
… new DateTime( Long.MAX_VALUE )
Joda-Time has no such minimum/maximum values available conveniently as constants. In contrast, note that Joda-Time’s successor, java.time built into Java 8 and later, does indeed offer the constants LocalDateTime.MIN
and LocalDateTime.MAX
.
Joda-Time没有像常量那样的最小/最大值。相反,请注意Joda-Time的继任者java。Java 8和以后构建的时间确实提供了LocalDateTime常量。分钟,LocalDateTime.MAX。
By the way, the Joda-Time team has advised we should migrate to java.time. Much of the java.time functionality is back-ported to Java 6 & 7 in the ThreeTen-Backport, further adapted to Android in ThreeTen-ABP.
顺便说一下,Joda-Time团队建议我们应该迁移到java.time。大部分java。在ThreeTen-Backport中,时间功能被反向移植到Java 6 & 7,并在3 - abp中进一步适应Android。
Too big, too small
Beware of these extremes. Their use is not practical. Various libraries, apps, databases, and other sinks/sources of date-time values may have much different limits, some much larger but typically much smaller.
当心这些极端。它们的使用是不实用的。不同的库、应用程序、数据库和日期时间值的其他接收器/来源可能有很多不同的限制,有些更大,但通常更小。
For example, many systems use the old tradition from UNIX & POSIX of tracking time as a 32-bit integer count of whole seconds since 1970-01-01T00:00:00Z. The natural limit of +/- two billion seconds results in the looming Year 2038 Problem.
例如,许多系统使用UNIX和POSIX的跟踪时间的旧传统作为自1970-01-01 - 01t00:00:00 z以来的32位整秒计数。20亿秒的自然极限导致了2038年的问题。
Another limit is the physical display size of fields on forms and reports that expect only four digits in a year number.
另一个限制是表单和报表上的字段的物理显示大小,这些字段在一年内只能显示4位数。
Workaround
You can define your own min/max.
你可以定义你自己的最小值/最大值。
You may want extreme values such as year 0000 and year 9999. Joda-Time supports years later than 9,999 but I would stick with 4 digits to fit the commonly used formats for display on-screen and in reports. Visually, the four nines stand out as a bogus date.
您可能需要极端值,比如年份0000和年份9999。Joda-Time支持的时间比9999晚了好几年,但我会坚持使用4位数字,以适应屏幕和报表中常用的显示格式。从视觉上看,这四个9代表着一个虚假的约会。
Or you may want an expected minimum value appropriate to your business logic. If building a new invoicing system, then you know the year should always be this year or later.
或者您可能想要一个适合您的业务逻辑的期望最小值。如果构建一个新的发票系统,那么您就知道年份应该是今年或更晚。
I suggest defining constants on a helper class. Something like this:
我建议在helper类上定义常量。是这样的:
package com.example;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
public class JodaTimeHelper {
static final public DateTime START_OF_TIME = new DateTime( 0000, 1, 1, 0, 0, 0, DateTimeZone.UTC );
static final public DateTime END_OF_TIME = new DateTime( 9999, 1, 1, 0, 0, 0, DateTimeZone.UTC );
static final public DateTime MINIMUM_INVOICE_DATETIME = new DateTime( 2015, 1, 1, 0, 0, 0, DateTimeZone.UTC );
}
Here is the syntax for calling those constants.
下面是调用这些常量的语法。
System.out.println( "START_OF_TIME: " + JodaTimeHelper.START_OF_TIME );
System.out.println( "END_OF_TIME: " + JodaTimeHelper.END_OF_TIME );
System.out.println( "END_OF_TIME: " + JodaTimeHelper. MINIMUM_INVOICE_DATETIME );
When run.
运行时。
START_OF_TIME: 0000-01-01T00:00:00.000Z
END_OF_TIME: 9999-01-01T00:00:00.000Z
MINIMUM_INVOICE_DATETIME: 2015-01-01T00:00:00.000Z
#2
2
When all dates are within the same TimeZone, you can create DateTime
object with fields assigned to minimum or max value.
However when using this constructor
当所有的日期都在同一个时区时,您可以创建DateTime对象,该对象的字段分配为最小值或最大值。但是在使用这个构造函数时
DateTime(int year, int monthOfYear, int dayOfMonth, int hourOfDay, int minuteOfHour, int secondOfMinute)
with Years.MAX_VALUE.getYears()
I have below exception:
max_value.getyears()我有以下例外:
So using the max number of years from exception, I have came up with the following end-of-universe dateTime:
因此,利用exception的最大年数,我提出了以下端点时间:
DateTime dtMax = new DateTime(292278993, 12, 31, 23, 59, 59);
System.out.println(dtMax);
// prints 292278993-12-31T23:59:59
See documentation for more details.
Also an interesting discussion can be read here.
有关更多细节,请参阅文档。这里还有一个有趣的讨论。
#3
0
After reading through lots of different tangents on this, I finally decided to figure out a minimal case that works for both the minimum and maximum for the UTC time zone. And it turns out that attempting to use Long
's MIN_VALUE
and MAX_VALUE
send me down rabbit trails.
在阅读了许多不同的切线之后,我最终决定找出一个适用于UTC时区的最小和最大值的最小情况。结果是,尝试使用Long的MIN_VALUE和MAX_VALUE将我发送到兔子路径。
So, given the following code snippet:
因此,鉴于以下代码片段:
new DateTime(milliseconds, DateTimeZone.UTC)
here are the minimum/maximum values that work for milliseconds
(tested in Java 1.7):
以下是在毫秒内工作的最小/最大值(在Java 1.7中测试):
UTC_DATE_TIME_MIN_VALUE_IN_MILLIS = 9223372017129599999L
UTC_DATE_TIME_MAX_VALUE_IN_MILLIS = -9223372017043200000L
Both of these values are approximately 20 billion away from Long.MIN_VALUE
and Long.MAX_Value
.
这两个值离Long大约有200亿。MIN_VALUE Long.MAX_Value。