问题描述
需要查看某个日期(例如:到期日期)是否大于或等于今天。目前已经使用JODA时间库来实现这种简单的比较。甚至一些帖子也推荐this。
但最近发现了一些时区问题。日期存在于 PST 中,当转换为 LocalDate 时,在太平洋标准时间下午 5:00 转换为 false,此时它应该为 true -
LocalDate Now = LocalDate.fromDateFields(new Date()); // Current date in PST
LocalDate expiryDate = LocalDate.fromDateFields(expiresOn); // expiresOn is java.util.Date
boolean notExpired = expiryDate.isEqual(Now) || expiryDate.isAfter(Now);
仔细观察,LocalDate expiryDate 使用的是 UTC 年表。所以在太平洋标准时间下午 5:00,当变量 expiryDate 包含 "2021-01-16" 时,变量 Now 变为 "2021- 01-17"
请推荐,有什么更好的方法来处理这个问题。 我试图了解使用 joda 时间可以实现哪些特殊优势,因为使用 SimpleDateFormatter 可以实现同样的同情。
解决方法
java.util
的日期时间 API 及其格式化 API SimpleDateFormat
已过时且容易出错。建议完全停止使用它们并切换到 modern date-time API。
- 出于任何原因,如果您必须坚持使用 Java 6 或 Java 7,您可以使用 ThreeTen-Backport,它将大部分 java.time 功能向后移植到 Java 6 和 7。
- 如果您正在为 Android 项目工作并且您的 Android API 级别仍然不符合 Java-8,请检查 Java 8+ APIs available through desugaring 和 How to use ThreeTenABP in Android Project。
从 Trail: Date Time 了解现代日期时间 API。
LocalDate
默认使用 JVM 的时区
无论何时涉及时区,请确保在创建 LocalDate
实例时指定相同的时区。默认情况下,LocalDate
使用 JVM 的时区,您不应该将一个时区的 LocalDate
与另一个时区的 UTC
进行比较,而不将它们转换为同一时区(推荐使用 LocalDateTime
)。 LocalDate
的情况也是如此。您应该使用具有日期和时间的对象(例如 LocalDateTime
)进行所有处理,而不是使用 LocalDate
,并且如果需要,您可以从中派生 java.util.Date
。
此外,January 1,1970,00:00:00 GMT
对象仅表示自称为“纪元”的标准基准时间(即 java.util.Date
(或 UTC))以来的毫秒数。当您打印 toString
的对象时,它的 expiryDate
方法返回 JVM 时区中的日期时间,根据该毫秒值计算得出。
因此,如果您从 java.util.Date
对象派生 UTC
,它本质上是 expiryDate
中的日期时间。
您可以将 now-in-PST 和 java.time.Instant
转换为 java.time.Instant
并进行比较。 import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Calendar;
import java.util.Date;
public class Main {
public static void main(String[] args) {
LocalDateTime nowInPST = LocalDateTime.now(ZoneId.of("America/Los_Angeles"));
System.out.println(nowInPST);
// Convert it to date in UTC
Instant nowInPSTConvertedToInstant = nowInPST.atZone(ZoneId.of("America/Los_Angeles"))
.withZoneSameInstant(ZoneId.of("Etc/UTC"))
.toInstant();
// Some java.util.Date
Calendar calendar = Calendar.getInstance();
calendar.set(2020,10,10);
Date date = calendar.getTime();
Instant expiry = date.toInstant();
System.out.println(nowInPSTConvertedToInstant.isBefore(expiry));
}
}
是 UTC 时间线上的瞬时点。
使用现代日期时间 API 的演示:
2021-01-17T10:58:38.490041
false
输出:
boolean notExpired = expiryDate.isEqual(now) || expiryDate.isAfter(now);
查看以下通知
Joda-Time 是 Java 的事实上的标准日期和时间库 在 Java SE 8 之前。现在要求用户迁移到 java.time (JSR-310)。
简化你的表达
以下声明
boolean notExpired = !expiryDate.isBefore(now);
可以简化为
using System;
using Renci.SshNet;
namespace mysftp
{
class Program
{
static void Main(string[] args)
{
string host = "myftps.hostname.com";
string username = "myusername";
string password = "securepassword";
int port = 990;
string remoteFolder = "/";
using (SftpClient sftp = new SftpClient(host,port,username,password))
{
try
{
Console.WriteLine("Attempting to connect to " + host + " ...");
sftp.Connect();
Console.WriteLine(sftp.ConnectionInfo.ServerVersion);
sftp.Disconnect();
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
}
}
}
}
,
您应该考虑两个 API:
- 您迄今为止一直在使用的 Joda-Time 是一个不错的库,但处于维护模式。
- Joda-Time 的首席开发人员 Stephen Colebourne 继续开发了 java.time,即现代 Java 日期和时间 API,从 Joda-Time 的好的和不太好的经验中汲取了教训。
你的问题不是很清楚。我假设到期时间已记录在 UTC 中并且似乎提前了一天,因为它是在太平洋时间查看的。因此,我将向您展示如何将所有内容保留在 UTC 中,以便进行比较有意义且准确。
乔达时间
System.setProperty("user.timezone","America/Vancouver");
Date expiresOn = new Date(1_610_841_600_000L); // Jan 17 UTC
System.out.println(expiresOn);
LocalDate now = LocalDate.now(DateTimeZone.UTC);
System.out.println(now);
LocalDate expiryDate = new DateTime(expiresOn,DateTimeZone.UTC).toLocalDate();
System.out.println(expiryDate);
boolean notExpired = expiryDate.isEqual(now) || expiryDate.isAfter(now);
System.out.println("Expired? " + (notExpired ? "No" : "Yes"));
现在运行时的输出:
Sat Jan 16 16:00:00 PST 2021
2021-01-17
2021-01-17
Expired? No
Joda-Time 主页说:
请注意,Joda-Time 被认为是一个基本上“已完成”的项目。
没有计划进行重大改进。如果使用 Java SE 8,请迁移
到 java.time
(JSR-310)。
java.time
LocalDate now = LocalDate.now(ZoneOffset.UTC);
System.out.println(now);
LocalDate expiryDate = expiresOn.toInstant()
.atOffset(ZoneOffset.UTC)
.toLocalDate();
System.out.println(expiryDate);
boolean notExpired = expiryDate.isEqual(now) || expiryDate.isAfter(now);
System.out.println("Expired? " + (notExpired ? "No" : "Yes"));
2021-01-17
2021-01-17
Expired? No
关于味道的说明
我的喜好是避免在变量名称(和其他地方)中出现不必要的否定。我觉得这样做更简单:
boolean expired = expiryDate.isBefore(now);
System.out.println("Expired? " + expired);
过期了?假
链接
- Joda-Time Home
- Oracle tutorial: Date Time 解释如何使用 java.time。