简体   繁体   English

如何使用 Joda Time 将自定义日期时间字符串转换为毫秒?

[英]How to convert a custom Date Time string to milliseconds using Joda Time?

How can I create an instance of java.util.Date out of a custom date time string?如何从自定义日期时间字符串中创建java.util.Date的实例? This is what I tried so far:这是我到目前为止所尝试的:

Given I have the this custom date time string:鉴于我有这个自定义日期时间字符串:

Fri Jul 29 12:56:35 UTC 2022

Using Joda-Time in the following piece of code:在以下代码中使用 Joda-Time:

String time = "Fri Jul 29 12:56:35 UTC 2022";
DateTimeFormatter df = DateTimeFormat.forPattern("EEE MMM dd HH:mm:ss z yyyy");
long millis = df.parseMillis(time);

I get the following exception:我得到以下异常:

java.lang.IllegalArgumentException: Invalid format: "Fri Jul 29 12:56:35 UTC 2022" is malformed at " UTC 2022"
        at org.joda.time.format.DateTimeFormatter.parseMillis(DateTimeFormatter.java:644)

You can use a code like this:您可以使用这样的代码:

String time = "Fri Jul 29 12:56:35 UTC 2022";
Date date = new Date(time);
long millis = date.getTime();
System.out.println(millis);

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

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