[英]Java String to DateTime
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!任何帮助是极大的赞赏!
You don't need a DateTime
object.您不需要DateTime
对象。java.util.Date
stores the time too.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 RJ says, these methods are deprecated, so you can use the java.util.Calendar
class:正如 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")
, 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)
You can create Joda DateTime object from the Java Date object, since Java does not have a DateTime
class.您可以从 Java Date对象创建 Joda DateTime对象,因为 Java 没有DateTime
类。
DateTime dt = new DateTime(start.getTime());
Though the Date
class of Java holds the time information as well(that's what you need in the first place), I suggest you to use a Calendar
instead of the Date
class of Java.尽管 Java 的Date
类也包含时间信息(这是您首先需要的),但我建议您使用Calendar
而不是 Java 的Date
类。
Calendar myCal = new GregorianCalendar();
myCal.setTime(date);
Have a look at the Calendar docs 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 LocalDateTime and ZonedDateTime class.事情发生了变化,现在甚至 Java(准确地说是 Java 8)也有一个LocalDateTime和ZonedDateTime类。 For conversions, you can have a look at this SO answer (posting an excerpt from there).对于转换,您可以查看此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);
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");
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.但是,绑定在String中的时间是必不可少的。
Any Help is greatly appreciated!任何帮助是极大的赞赏!
I recommend that you use java.time, the modern Java date and time API, for your date and time work.我建议您使用 java.time,现代 Java 日期和时间 API,用于您的日期和时间工作。
String startStr = "2013-09-18T20:40:00+0000";
OffsetDateTime start = OffsetDateTime.parse(startStr, isoFormatter);
System.out.println(start);
Output is:输出是:
2013-09-18T20:40Z 2013-09-18T20:40Z
I was using this formatter:我正在使用这个格式化程序:
private static final DateTimeFormatter isoFormatter = new DateTimeFormatterBuilder()
.append(DateTimeFormatter.ISO_LOCAL_DATE_TIME)
.appendOffset("+HHMM", "+0000")
.toFormatter();
If by a DateTime
object you meant org.joda.time.Datetime
, please read this quote from the Joda-Time homepage (boldface is original):如果DateTime
对象指的是org.joda.time.Datetime
,请阅读 Joda-Time 主页上的引用(粗体为原始内容):
Note that from Java SE 8 onwards, users are asked to migrate to
java.time
(JSR-310) - a core part of the JDK which replaces this project.请注意,从 Java SE 8 开始,要求用户迁移到java.time
(JSR-310) - 替代该项目的 JDK 的核心部分。
The SimpleDateFormat
class that you tried to use in the question is a notorious troublemaker of a class and long outdated.您尝试在问题中使用的SimpleDateFormat
类是类的臭名昭著的麻烦制造者,并且已经过时了。 Under no circumstances use it.在任何情况下都不要使用它。 Ever.曾经。
你试过这个吗?
Date date1=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2013-09-18T20:40:00+0000".replace("T"," ").substring(0,19));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.