JodaTime中的最小/最大日期/日期时间 [英] Min/Max Date/DateTime in JodaTime

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

问题描述

在JodaTime中有什么方法可以构建一个Date / DateTime,它总是比任何其他Date / DateTime更小/更大?类似于

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

DateTime bigBang = DateTime.xxx();
DateTime endOfUniverse = DateTime.yyy();

约束:我不想使用标准的Java Date库。

Constraint: I don't want to use the standard Java Date libraries.

推荐答案

java.time



Joda-Time项目现在处于维护模式。团队建议迁移到java.time类。

java.time

The Joda-Time project is now in maintenance mode. The team advises migration to the java.time classes.

对于java.time中的min / max,请参阅我的回答类似问题。

For min/max in java.time, see my Answer on a similar Question.

Joda-Time 将时间记录为毫秒自1970年第一时刻以来 UTC 。此计数使用 64位 long 整数。因此,从技术上讲,最大值和最小值是 long 的+/-限制。

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.

… new DateTime( Long.MIN_VALUE )
… new DateTime( Long.MAX_VALUE )

Joda-Time没有这样的最小/最大值可以方便地作为常数。相比之下,请注意Joda-Time的继任者, java内置于Java 8及更高版本的.time 确实提供了常量 LocalDateTime.MIN LocalDateTime.MAX

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团队建议我们应该迁移到java.time。许多java.time功能都被反向移植到Java 6& 7在 ThreeTen-Backport 中,在 ThreeTen-ABP

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.

谨防这些极端情况。它们的使用并不实用。各种库,应用程序,数据库和其他日期时间值的汇/源可能有不同的限制,一些更大但通常更小。

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.

例如,许多系统使用UNIX和Linux的旧传统。追踪时间的POSIX为自1970-01-01T00:00:00Z以来的整数秒的32位整数计数。 +/- 20亿秒的自然限制导致迫在眉睫的 2038年问题

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.

另一个限制是表单和报表上字段的实际显示大小,一年中只有四位数字。

Another limit is the physical display size of fields on forms and reports that expect only four digits in a year number.

您可以定义自己的最小值/最大值。

You can define your own min/max.

您可能需要极端值,例如0000年和9999年.Joda-Time支持的时间超过9,999,但我会坚持使用4位数来适应常用的格式在屏幕上和报告中显示。从视觉上看,这四个9是一个虚假的日期。

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.

或者你可能想要一个适合你的业务逻辑的预期最小值。如果建立一个新的发票系统,那么你知道年份应该总是在今年或更晚。

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:

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 );

运行时。

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

这篇关于JodaTime中的最小/最大日期/日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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