英文:
Extract Date from Zulu Time using Joda Time
问题
我有一个 Zulu 时间 "2011-08-12T20:17:46.384Z"。如何使用 Java Joda Time 提取日期部分?
我已阅读多个关于 Zulu 时间的问题,但没有一个回答了我的问题。
英文:
I have a Zulu time "2011-08-12T20:17:46.384Z". How can i extract date part using java joda time?
I have read multiple question on Zulu Time but none answer my question.
答案1
得分: 1
你想要日期使用你自己的时区还是使用Zulu(UTC)时区?问这个是因为不同时区的日期是不同的。
在Zulu时区的日期,与字符串中的日期相同,更容易实现:
String zuluString = "2011-08-12T20:17:46.384Z";
DateTime dt = DateTime.parse(zuluString);
LocalDate datePart = dt.toLocalDate();
System.out.println(datePart);
输出:
> 2011-08-12
获取日期在JVM的默认时区只需要多写几个字符:
LocalDate datePart = dt.withZone(DateTimeZone.getDefault()).toLocalDate();
在亚洲/澳门时区运行时的输出:
> 2011-08-13
哪个新的API?
Joda-Time处于维护模式,不再建议用于新的代码。Joda-Time的主页上写着:
> 请注意,Joda-Time被认为是一个基本上“完成”的项目。
> 不计划进行重大增强。如果使用Java SE 8,请迁移到java.time
(JSR-310)。
java.time
从Java 8开始内置于Java中,也已经被后移植到Java 6和7。您可以从以下第二个链接开始。
链接
- Joda-Time - 主页
- Oracle教程:日期时间,解释如何使用java.time。
英文:
Do you want the date in your own time zone or in Zulu (UTC)? Asking because it is never the same date in all time zones.
Date in Zulu time zone, the same date as in the string, is the easier requirement:
String zuluString = "2011-08-12T20:17:46.384Z";
DateTime dt = DateTime.parse(zuluString);
LocalDate datePart = dt.toLocalDate();
System.out.println(datePart);
Output:
> 2011-08-12
Getting the date in the default time zone of the JVM just requires a few chars more:
LocalDate datePart = dt.withZone(DateTimeZone.getDefault()).toLocalDate();
Output when run in Asia/Macau time zone:
> 2011-08-13
Which new API ?
Joda-Time is in maintenance mode and no longer recommended for new code. The Joda-Time home page says:
> Note that Joda-Time is considered to be a largely “finished” project.
> No major enhancements are planned. If using Java SE 8, please migrate
> to java.time
(JSR-310).
java.time is built into Java since Java 8 and has also been backported to Java 6 and 7. You may start by following the second link below.
Links
- Joda-Time - Home
- Oracle tutorial: Date Time explaining how to use java.time.
答案2
得分: 1
我建议您切换到现代日期时间 API。
使用现代的 java.time (JSR-310)
API:
import java.time.LocalDate;
import java.time.OffsetDateTime;
public class Main {
public static void main(String[] args) {
OffsetDateTime odt = OffsetDateTime.parse("2011-08-12T20:17:46.384Z");
System.out.println(odt);
LocalDate date = odt.toLocalDate();
System.out.println(date);
}
}
输出:
2011-08-12T20:17:46.384Z
2011-08-12
在**教程:日期时间**中了解更多关于现代日期时间 API 的内容。
请查看来自Joda-Time 文档首页的以下内容:
> Joda-Time 是 Java SE 8 之前的 Java 标准日期和时间库。
> 用户现在被要求迁移到 java.time (JSR-310)。
使用 Joda-Time API:
import org.joda.time.DateTime;
import org.joda.time.LocalDate;
public class Main {
public static void main(String[] args) {
DateTime dateTime = DateTime.parse("2011-08-12T20:17:46.384Z");
System.out.println(dateTime);
LocalDate date = dateTime.toLocalDate();
System.out.println(date);
}
}
输出:
2011-08-12T20:17:46.384Z
2011-08-12
英文:
I suggest you switch to the modern date-time API.
Using the modern java.time (JSR-310)
API:
import java.time.LocalDate;
import java.time.OffsetDateTime;
public class Main {
public static void main(String[] args) {
OffsetDateTime odt = OffsetDateTime.parse("2011-08-12T20:17:46.384Z");
System.out.println(odt);
LocalDate date = odt.toLocalDate();
System.out.println(date);
}
}
Output:
2011-08-12T20:17:46.384Z
2011-08-12
Learn more about the modern date-time API at Trail: Date Time.
Check out the following lines from the home page of Joda-Time documentation:
> Joda-Time is the de facto standard date and time library for Java
> prior to Java SE 8. Users are now asked to migrate to java.time
> (JSR-310).
Using the Joda-Time API:
import org.joda.time.DateTime;
import org.joda.time.LocalDate;
public class Main {
public static void main(String[] args) {
DateTime dateTime = DateTime.parse("2011-08-12T20:17:46.384Z");
System.out.println(dateTime);
LocalDate date = dateTime.toLocalDate();
System.out.println(date);
}
}
Output:
2011-08-12T20:17:46.384Z
2011-08-12
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论