简体   繁体   English

如何在 Java 中将字符串解析为日期

[英]How to parse String to Date in Java

I want to convert a String 24 May 2020 07:40 AM to date format Mon May 24 07:40:55 IST 2020 .我想将字符串24 May 2020 07:40 AM转换为日期格式Mon May 24 07:40:55 IST 2020 I tried using Calendar and SimpleDateFormatter but didn't find a solution.我尝试使用 Calendar 和 SimpleDateFormatter 但没有找到解决方案。 Any help is appreciated.任何帮助表示赞赏。

I want the return type to be Date since I have to compare it with a couple of Date s.我希望返回类型为Date因为我必须将它与几个Date进行比较。

java.time java.time

When you've got some Date objects — likely from a legacy API that you cannot afford to upgrade to java.time just now — I still recommend that you use java.time, the modern Java date and time API, for your comparisons. When you've got some Date objects — likely from a legacy API that you cannot afford to upgrade to java.time just now — I still recommend that you use java.time, the modern Java date and time API, for your comparisons.

In the following example I am using Instant from java.time, but you may use ZonedDateTime or some other modern type too.在以下示例中,我使用来自 java.time 的Instant ,但您也可以使用ZonedDateTime或其他一些现代类型。

    DateTimeFormatter fromFormatter = DateTimeFormatter.ofPattern("d MMM uuuu hh:mm a", Locale.ENGLISH);

    Date anOldfashionedDate = new Date(1_590_286_000_000L);
    Date anotherOldfashionedDate = new Date(1_590_287_000_000L);
    System.out.println("The Date objects are " + anOldfashionedDate + " and " + anotherOldfashionedDate);

    String aString = "24 May 2020 07:40 AM";

    Instant instantFromDate = anOldfashionedDate.toInstant();
    Instant instantFromAnotherDate = anotherOldfashionedDate.toInstant();
    Instant instantFromString = LocalDateTime.parse(aString, fromFormatter)
            .atZone(ZoneId.of("Asia/Kolkata"))
            .toInstant();

    System.out.println("Comparing " + instantFromDate + " and " + instantFromString + ": "
            + instantFromDate.compareTo(instantFromString));
    System.out.println("Comparing " + instantFromAnotherDate + " and " + instantFromString + ": "
            + instantFromAnotherDate.compareTo(instantFromString));

Output is (when running in Asia/Kolkata time zone): Output 是(在亚洲/加尔各答时区运行时):

 The Date objects are Sun May 24 07:36:40 IST 2020 and Sun May 24 07:53:20 IST 2020 Comparing 2020-05-24T02:06:40Z and 2020-05-24T02:10:00Z: -1 Comparing 2020-05-24T02:23:20Z and 2020-05-24T02:10:00Z: 1

An Instant prints in UTC;以 UTC Instant打印; this is what its toString method generates.这是它的toString方法生成的。 The trailing Z means UTC.尾随Z表示 UTC。 Since India Standard Time is 5 hours 30 minutes ahead of UTC, 07:40 AM in India is the same time as 02:10 in UTC.由于印度标准时间比 UTC 时间早 5 小时 30 分钟,因此印度的 07:40 AM 与 UTC 的 02:10 时间相同。

Given that you embark on using java.time now, you are well prepared when one day your legacy API gets upgraded to using java.time too.鉴于您现在开始使用 java.time,当您的旧 API 也升级到使用 java.time 时,您已做好充分准备。

The opposite conversion反向转换

If you do insist on using Date , to answer your question as asked, the opposite conversion is easy too:如果您坚持使用Date来回答您的问题,则相反的转换也很容易:

    Date oldfashionedDateFromInstantFromString = Date.from(instantFromString);
    System.out.println("Converting to old-fashioned: " + oldfashionedDateFromInstantFromString);

Converting to old-fashioned: Sun May 24 07:40:00 IST 2020转换为老式:2020 年 5 月 24 日星期日 07:40:00 IST

Link关联

Oracle tutorial: Date Time explaining how to use java.time. Oracle 教程:日期时间解释如何使用 java.time。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM