Java 字符串到日期时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18823627/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 11:24:09  来源:igfitidea点击:

Java String to DateTime

javadatetime

提问by sn0ep

I have a string from a json response:

我有一个来自 json 响应的字符串:

start: "2013-09-18T20:40:00+0000",
end: "2013-09-18T21:39:00+0000",

How do i convert this string to a java DateTime Object?

我如何将此字符串转换为 java DateTime 对象?

i have tried using the following:

我尝试使用以下方法:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
start = sdf.parse("2013-09-18T20:40:00+0000");

but with this i can only create a Date Object. But the time binded in the String is kinda essential.

但有了这个我只能创建一个日期对象。但是绑定在字符串中的时间是必不可少的。

Any Help is greatly appreciated!

任何帮助是极大的赞赏!

采纳答案by SudoRahul

You can create Joda DateTimeobject from the Java Dateobject, since Java does not have a DateTimeclass.

您可以从 Java Date对象创建 Joda DateTime对象,因为 Java 没有类。DateTime

DateTime dt = new DateTime(start.getTime());


Though the Dateclass of Java holds the time information as well(that's what you need in the first place), I suggest you to use a Calendarinstead of the Dateclass of Java.

尽管DateJava 类也包含时间信息(这是您首先需要的),但我建议您使用 aCalendar代替DateJava 类。

Calendar myCal = new GregorianCalendar();
myCal.setTime(date);

Have a look at the Calendardocs for more info on how you can use it more effectively.

查看日历文档,了解有关如何更有效地使用它的更多信息。



Things have changed and now even Java (Java 8 to be precise), has a LocalDateTimeand ZonedDateTimeclass. For conversions, you can have a look at this SO answer(posting an excerpt from there).

事情发生了变化,现在甚至 Java(准确地说是 Java 8)也有一个LocalDateTimeZonedDateTime类。对于转换,您可以查看此SO 答案(从那里发布摘录)。

Given: Date date = [some date]

鉴于: Date date = [some date]

(1) LocalDateTime << Instant<< Date

(1) LocalDateTime << Instant<< 日期

Instant instant = Instant.ofEpochMilli(date.getTime());
LocalDateTime ldt = LocalDateTime.ofInstant(instant, ZoneOffset.UTC);

(2) Date << Instant << LocalDateTime

(2) Date << Instant << LocalDateTime

Instant instant = ldt.toInstant(ZoneOffset.UTC);
Date date = Date.from(instant);

回答by BackSlash

You don't need a DateTimeobject. java.util.Datestores the time too.

你不需要DateTime对象。java.util.Date也存储时间。

int hours = start.getHours(); //returns the hours
int minutes = start.getMinutes(); //returns the minutes
int seconds = start.getSeconds(); //returns the seconds


As R.J says, these methods are deprecated, so you can use the java.util.Calendarclass:

正如 RJ 所说,这些方法已被弃用,因此您可以使用java.util.Calendar该类:

Calendar calendar = Calendar.getInstance();
calendar.setTime(sdf.parse("2013-09-18T20:40:00+0000"));
int hour = calendar.get(Calendar.HOUR); //returns the hour
int minute = calendar.get(Calendar.MINUTE); //returns the minute
int second = calendar.get(Calendar.SECOND); //returns the second

Note:on my end, sdf.parse("2013-09-18T20:40:00+0000")fires a

注意:在我这边,sdf.parse("2013-09-18T20:40:00+0000")会触发一个

java.text.ParseException: Unparseable date: "2013-09-18T20:40:00+0000"
    at java.text.DateFormat.parse(DateFormat.java:357)
    at MainClass.main(MainClass.java:16)

回答by xyz

You can use DateTimeFormatter

您可以使用DateTimeFormatter

DateTimeFormatter format = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
DateTime time = format.parseDateTime("2013-09-18T20:40:00+0000");