Android比较日期 [英] Android compare date
问题描述
我正在尝试比较这种格式的两个日期。
I am trying to compare two dates in this format.
date1 = Wed Jul 13 17:23:33 GMT+02:00 2016
date2 = Wed Jul 13 17:23:31 CEST 2016
if(date1.after(date2){
//my logic
}else if(date1.before(date2){
//my logic
}
比较似乎只发生了几次,这被认为是错误的?
The comparison seems to have happened only a few times, where it is thought to be wrong?
推荐答案
tl; dr
tl;dr
ZonedDateTime.parse(
"Wed Jul 13 17:23:33 GMT+02:00 2016" ,
DateTimeFormatter.ofPattern( "EEE MMM d HH:mm:ss z uuuu" )
.withLocale( Locale.ENGLISH )
)
.isAfter(
ZonedDateTime.parse(
"Wed Jul 13 17:23:31 CEST 2016" ,
DateTimeFormatter.ofPattern( "EEE MMM d HH:mm:ss z uuuu" )
.withLocale( Locale.ENGLISH )
)
)
java.time
现代方法使用 java.t ime 类。
String inputX = "Wed Jul 13 17:23:33 GMT+02:00 2016";
String inputY = "Wed Jul 13 17:23:31 CEST 2016";
定义格式模式以匹配您的输入字符串。
Define a formatting pattern to match your input strings.
DateTimeFormatter f = DateTimeFormatter.ofPattern( "EEE MMM d HH:mm:ss z uuuu" ).withLocale( Locale.ENGLISH );
ZonedDateTime
类表示一个时刻,时间轴上的时间点,以及特定地区(时区)的人们使用的挂钟时间。指定 Locale
确定(a)用于翻译日名,月名等的人类语言,以及(b)决定缩写,大写,标点,分隔符等问题的文化规范
The ZonedDateTime
class represents a moment, a point on the timeline, with a wall-clock time used by the people of a particular region (a time zone). Specify a Locale
to determine (a) the human language for translation of name of day, name of month, and such, and (b) the cultural norms deciding issues of abbreviation, capitalization, punctuation, separators, and such.
ZonedDateTime x = ZonedDateTime.parse( inputX , f );
ZonedDateTime y = ZonedDateTime.parse( inputY , f );
x.toString():2016-07-13T17:23: 33 + 02:00 [GMT + 02:00]
x.toString(): 2016-07-13T17:23:33+02:00[GMT+02:00]
y.toString():2016-07-13T17:23:31 + 02:00 [欧洲/巴黎]
y.toString(): 2016-07-13T17:23:31+02:00[Europe/Paris]
在实际工作中,捕获遇到错误输入时引发的异常的陷阱。
In real work, trap for the exception thrown when encountering faulty input.
使用 isBefore
, isAfter
进行比较, isEqual
或方法。请注意, Comparable :: equals
方法实现的行为有所不同(阅读文档)。
Compare using isBefore
, isAfter
, isEqual
, or methods. Be aware that the Comparable::equals
method implementation differs in behavior (read the doc).
if( x.isBefore( y ) ) { // BEFORE
…
} else if( x.isAfter( y ) ) { // AFTER
…
} else if( x.isEqual( y ) ) { // EQUAL
…
} else { // UNREACHABLE
… // Handle error condition. Should be unreachable.
}
区域
顺便说一句,您的两个输入使用的格式选择非常差。第一个只有从UTC偏移,而不是实际时区。第二个带有伪区域 CEST。这样的3-4个字母的标签在主流媒体中很常见,但不是真正的时区,不是标准化的,甚至不是唯一的(!)。
Zone
By the way, your two inputs use a very poor choice of formats. The first has only an offset-from-UTC rather than an actual time zone. The second carries a pseudo-zone of "CEST". Such 3-4 letter labels are common in mainstream media, but are not true time zones, are not standardized, and are not even unique(!).
指定正确的时区名称格式为大陆/地区
,例如 美国/蒙特利尔
, 非洲/卡萨布兰卡
,或太平洋/奥克兰
。请勿使用3-4个字母的缩写,例如 EST
或 IST
,因为它们不是 真实时区,不是标准化的,甚至不是唯一的(!)。
Specify a proper time zone name in the format of continent/region
, such as America/Montreal
, Africa/Casablanca
, or Pacific/Auckland
. Never use the 3-4 letter abbreviation such as EST
or IST
as they are not true time zones, not standardized, and not even unique(!).
ZoneId z = ZoneId.of( "America/Montreal" ) ;
尽可能使用标准的 ISO 8601 格式,将日期时间值转换为文本时。 java.time 类在解析/生成字符串时默认使用标准格式。 ZonedDateTime
类通过将时区的名称附加在方括号中来明智地扩展了标准格式,如上面的示例所示。
Whenever possible, use standard ISO 8601 formats when exchanging date-time values as text. The java.time classes use standard formats by default when parsing/generating strings. The ZonedDateTime
class wisely extends the standard format by appending the name of the time zone in square brackets, as seen above in our example.
java.time 框架已构建Java 8及更高版本。这些类取代了麻烦的旧旧版日期时间类,例如 java.util.Date
, Calendar
,& SimpleDateFormat
。
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
.
Joda-Time 项目,现在处于维护模式 ,建议迁移到 java.time 类。
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
要了解更多信息,请参见 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.
使用符合
Using a JDBC driver compliant with JDBC 4.2 or later, you may exchange java.time objects directly with your database. No need for strings nor java.sql.* classes.
从哪里获取java.time类?
Where to obtain the java.time classes?
- Java SE 8 , Java SE 9 ,然后是
- 内置。
- 具有捆绑实现的标准Java API的一部分。
- Java 9添加了一些次要功能和修复。
- Java SE 8, Java SE 9, and later
- Built-in.
- Part of the standard Java API with a bundled implementation.
- Java 9 adds some minor features and fixes.
- 许多java.time功能都反向移植到Java 6&
nofollow noreferrer> ThreeTen-Backport 。
- Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
- 较新版本的java.time类的Android捆绑实现。
- 对于早期的Android, ThreeTenABP 项目改编了 ThreeTen-Backport (如上所述)。请参阅 如何使用ThreeTenABP… 。
- Later versions of Android bundle implementations of the java.time classes.
- For earlier Android, the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….
ThreeTen-Extra 项目使用其他类扩展了java.time。该项目是将来可能向java.time添加内容的试验场。您可能会在这里找到一些有用的类,例如
时间间隔
,YearWeek
,YearQuarter
和更多。这篇关于Android比较日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!