使用 Joda Time 库将日期字符串转换为 DateTime 对象

发布于 2024-11-14 00:45:57 字数 355 浏览 2 评论 0原文

我有一个日期作为字符串,格式如下:“04/02/2011 20:27:05”。我正在使用 Joda-Time 库,并希望将其转换为 DateTime 对象。我做到了:

DateTime dt = new DateTime("04/02/2011 20:27:05")

但我收到以下错误:

Invalid format: "04/02/2011 14:42:17" is malformed at "/02/2011 14:42:17"

如何将上述日期转换为 DateTime 对象?

I have a date as a string in the following format "04/02/2011 20:27:05". I am using Joda-Time library and would like to convert it to DateTime object. I did:

DateTime dt = new DateTime("04/02/2011 20:27:05")

But I'm getting the following error :

Invalid format: "04/02/2011 14:42:17" is malformed at "/02/2011 14:42:17"

How to convert the above date to a DateTime object?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

夜无邪 2024-11-21 00:45:58

一个简单的方法:

public static DateTime transfStringToDateTime(String dateParam, Session session) throws NotesException {
    DateTime dateRetour;
    dateRetour = session.createDateTime(dateParam);                 

    return dateRetour;
}

An simple method :

public static DateTime transfStringToDateTime(String dateParam, Session session) throws NotesException {
    DateTime dateRetour;
    dateRetour = session.createDateTime(dateParam);                 

    return dateRetour;
}
一江春梦 2024-11-21 00:45:58

两种方法可以实现

日期时间格式

DateTimeFormat.forPattern("dd/MM/yyyy HH:mm:ss").parseDateTime("04/02/2011 20:27:05");

简单日期格式

        String dateValue = "04/02/2011 20:27:05";
        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss"); //  04/02/2011 20:27:05

        Date date = sdf.parse(dateValue); // returns date object
        System.out.println(date); // outputs: Fri Feb 04 20:27:05 IST 2011

There are two ways this could be achieved.

DateTimeFormat

DateTimeFormat.forPattern("dd/MM/yyyy HH:mm:ss").parseDateTime("04/02/2011 20:27:05");

SimpleDateFormat

        String dateValue = "04/02/2011 20:27:05";
        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss"); //  04/02/2011 20:27:05

        Date date = sdf.parse(dateValue); // returns date object
        System.out.println(date); // outputs: Fri Feb 04 20:27:05 IST 2011
涫野音 2024-11-21 00:45:57

使用DateTimeFormat

DateTimeFormatter formatter = DateTimeFormat.forPattern("dd/MM/yyyy HH:mm:ss");
DateTime dt = formatter.parseDateTime(string);

Use DateTimeFormat:

DateTimeFormatter formatter = DateTimeFormat.forPattern("dd/MM/yyyy HH:mm:ss");
DateTime dt = formatter.parseDateTime(string);
尬尬 2024-11-21 00:45:57

我知道这是一个老问题,但我想补充一点,从 JodaTime 2.0 开始,你可以用一句话来做到这一点:

DateTime date = DateTime.parse("04/02/2011 20:27:05", 
                  DateTimeFormat.forPattern("dd/MM/yyyy HH:mm:ss"));

I know this is an old question, but I wanted to add that, as of JodaTime 2.0, you can do this with a one-liner:

DateTime date = DateTime.parse("04/02/2011 20:27:05", 
                  DateTimeFormat.forPattern("dd/MM/yyyy HH:mm:ss"));
青春如此纠结 2024-11-21 00:45:57
DateTimeFormat.forPattern("dd/MM/yyyy HH:mm:ss").parseDateTime("04/02/2011 20:27:05");
DateTimeFormat.forPattern("dd/MM/yyyy HH:mm:ss").parseDateTime("04/02/2011 20:27:05");
舟遥客 2024-11-21 00:45:57

从评论中我选择了一个答案,并添加了时区:

String dateTime = "2015-07-18T13:32:56.971-0400";

DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZZ")
        .withLocale(Locale.ROOT)
        .withChronology(ISOChronology.getInstanceUTC());

DateTime dt = formatter.parseDateTime(dateTime);

From comments I picked an answer like and also adding TimeZone:

String dateTime = "2015-07-18T13:32:56.971-0400";

DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZZ")
        .withLocale(Locale.ROOT)
        .withChronology(ISOChronology.getInstanceUTC());

DateTime dt = formatter.parseDateTime(dateTime);
柠北森屋 2024-11-21 00:45:57

您的格式不是预期的 ISO 格式,您应该尝试

DateTimeFormatter format = DateTimeFormat.forPattern("dd/MM/yyyy HH:mm:ss");
DateTime time = format.parseDateTime("04/02/2011 20:27:05");

Your format is not the expected ISO format, you should try

DateTimeFormatter format = DateTimeFormat.forPattern("dd/MM/yyyy HH:mm:ss");
DateTime time = format.parseDateTime("04/02/2011 20:27:05");
手长情犹 2024-11-21 00:45:57

您还可以使用 SimpleDateFormat,如日期时间格式

Date startDate = null;
Date endDate = null;
try {
    if (validDateStart!= null) startDate = new SimpleDateFormat("MM/dd/yyyy HH:mm", Locale.ENGLISH).parse(validDateStart + " " + validDateStartTime);
    if (validDateEnd!= null) endDate = new SimpleDateFormat("MM/dd/yyyy HH:mm", Locale.ENGLISH).parse(validDateEnd + " " + validDateEndTime);
} catch (ParseException e) {
    e.printStackTrace();
}

You can also use SimpleDateFormat, as in DateTimeFormat

Date startDate = null;
Date endDate = null;
try {
    if (validDateStart!= null) startDate = new SimpleDateFormat("MM/dd/yyyy HH:mm", Locale.ENGLISH).parse(validDateStart + " " + validDateStartTime);
    if (validDateEnd!= null) endDate = new SimpleDateFormat("MM/dd/yyyy HH:mm", Locale.ENGLISH).parse(validDateEnd + " " + validDateEndTime);
} catch (ParseException e) {
    e.printStackTrace();
}
热情消退 2024-11-21 00:45:57

tl;dr

java.time.LocalDateTime.parse( 
    "04/02/2011 20:27:05" , 
    DateTimeFormatter.ofPattern( "dd/MM/uuuu HH:mm:ss" )
)

java.time

现代方法使用 java.time 类来取代古老的 Joda-Time 项目。

解析为 LocalDateTime< /a> 因为您的输入缺少任何时区指示符或与 UTC 的偏移量。

String input = "04/02/2011 20:27:05" ;
DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd/MM/uuuu HH:mm:ss" ) ;
LocalDateTime ldt = LocalDateTime.parse( input , f ) ;

ldt.toString(): 2011-02-04T20:27:05

提示:如果可能,请使用标准 ISO 8601 将日期时间值交换为文本时的格式,而不是此处看到的格式。方便的是,java.time 类在解析/生成字符串时使用标准格式。


关于 java.time

java.time 框架内置于 Java 8 及更高版本中。这些类取代了麻烦的旧遗留日期时间类,例如java.util.Date, 日历, & ; SimpleDateFormat

Joda-Time 项目,现已在 维护模式,建议迁移到 java.time 类。

要了解更多信息,请参阅 Oracle 教程。并在 Stack Overflow 上搜索许多示例和解释。规范为 JSR 310

从哪里获取 java.time 类?

ThreeTen-Extra 项目通过附加类扩展了 java.time 。该项目是 java.time 未来可能添加的内容的试验场。您可能会在这里找到一些有用的类,例如 间隔YearWeek<代码>YearQuarter,以及更多

tl;dr

java.time.LocalDateTime.parse( 
    "04/02/2011 20:27:05" , 
    DateTimeFormatter.ofPattern( "dd/MM/uuuu HH:mm:ss" )
)

java.time

The modern approach uses the java.time classes that supplant the venerable Joda-Time project.

Parse as a LocalDateTime as your input lacks any indicator of time zone or offset-from-UTC.

String input = "04/02/2011 20:27:05" ;
DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd/MM/uuuu HH:mm:ss" ) ;
LocalDateTime ldt = LocalDateTime.parse( input , f ) ;

ldt.toString(): 2011-02-04T20:27:05

Tip: Where possible, use the standard ISO 8601 formats when exchanging date-time values as text rather than format seen here. Conveniently, the java.time classes use the standard formats when parsing/generating strings.


About java.time

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.

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

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

白鸥掠海 2024-11-21 00:45:57

您需要一个适合于您正在使用的格式。查看文档以获取有关如何构建的说明。

即兴,我认为你需要 format = DateTimeFormat.forPattern("M/d/y H:m:s")

You need a DateTimeFormatter appropriate to the format you're using. Take a look at the docs for instructions on how to build one.

Off the cuff, I think you need format = DateTimeFormat.forPattern("M/d/y H:m:s")

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文