英文:
How to change Date and Time format in Java
问题
我知道这是一个简单的问题,但是我应该使用什么日期和时间格式来显示这个输出。
2019-02-06T3:26:40+08:00
我正在使用 java.time
。
我使用了 LocalDateTime
,但它只输出了 2020-10-06T16:40:04,没有带上 +08:00。
我只使用了这段代码:
LocalDateTime myObj = LocalDateTime.now();
System.out.println(myObj);
我不知道如何像上面的示例一样进行格式化。
英文:
i know this is a simple question but what date and time format should i use to show this output.
2019-02-06T3:26:40+08:00
I’m using java.time.
I use LocalDateTime
but it only outputs 2020-10-06T16:40:04 and didn’t have the +08:00.
I only use this:
LocalDateTime myObj = LocalDateTime.now();
System.out.println(myObj);
I don’t know how to format like the example above.
答案1
得分: 4
以下是代码的翻译部分:
// 获取特定偏移量的当前时间
OffsetDateTime offsetDateTime = OffsetDateTime.now(ZoneOffset.ofHours(8));
// 定义输出模式
String pattern = "uuu-MM-dd'T'H:mm:ssxxx";
// 并使用定义的模式打印一些结果,使用具有定义模式的格式化程序
System.out.println("使用模式格式化:"
+ pattern
+ ": "
+ offsetDateTime.format(DateTimeFormatter.ofPattern(pattern)));
将输出类似于以下行(时间可能会有所不同,至少如下所示):
使用模式格式化:uuu-MM-dd'T'H:mm:ssxxx: 2020-10-06T17:02:49+08:00
请注意,这部分只是代码的翻译。
英文:
tl;dr
// take the current time at a specific offset from UTC
OffsetDateTime offsetDateTime = OffsetDateTime.now(ZoneOffset.ofHours(8));
// define an output pattern
String pattern = "uuu-MM-dd'T'H:mm:ssxxx";
// and print some result using a formatter with the pattern defined
System.out.println("Formatted with pattern "
+ pattern
+ ": "
+ offsetDateTime.format(DateTimeFormatter.ofPattern(pattern)));
will output something similar to this line (time may/will differ, at least):
Formatted with pattern uuu-MM-dd'T'H:mm:ssxxx: 2020-10-06T17:02:49+08:00
Problem with your code:
-
Your code example was given in a comment, not in the question
(this is not a functional problem related to Java but still a problem here, you can edit that) -
You are currently using a
LocalDateTime
, which does not hold any information about an offset or a time zone -
You are not defining a desired output pattern but (implicitly) invoking the
toString()
method of theLocalDateTime
by just printing the instance in the following code (the one from your comment):
LocalDateTime myObj = LocalDateTime.now();
System.out.println(myObj);
outputs a String
like 2020-10-06T16:40:04
without the desired +08:00
.
<del>
You can create an OffsetDateTime
using an instance of LocalDateTime
and a desired ZoneOffset
, like
LocalDateTime localNow = LocalDateTime.now();
OffsetDateTime offsetDateTime = OffsetDateTime.of(localNow, ZoneOffset.ofHours(8));
System.out.println(offsetDateTime.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME));
which also uses a predefined output pattern, which slightly differs from your desired one (fractions-of-second is displayed between seconds and the offset).
</del>
But please note that this approach may return an incorrect point in time under certain circumstances (see the comment by @OleV.V. below this answer).
If you are sure every system that code is running on has and will always have a time zone setting with an offset of +08:00
, you can use OffsetDateTime.now()
without adding a ZoneOffset
explicitly or use the system default exlicitly by OffsetDateTime nowExplicitSystemOffset = OffsetDateTime.now(ZoneId.systemDefault());
.
I recommend having a look at Trail: Date Time (The Java Tutorials).
答案2
得分: 3
OffsetDateTime
请使用 OffsetDateTime
替代 LocalDateTime
:
OffsetDateTime myObj = OffsetDateTime.now(ZoneId.systemDefault());
System.out.println(myObj);
我刚刚在亚洲/香港时区运行了这个代码。输出结果是:
> 2020-10-07T03:25:03.254761+08:00
你要求的格式是 ISO 8601。java.time
包中的类通常从它们的 toString
方法中打印 ISO 8601 格式。LocalDateTime
对象不包含任何偏移量或时区信息。而 OffsetDateTime
正如其名,包含了一个类似 +08:00
的 UTC 偏移量,因此在调用 toString()
方法时会打印出偏移量信息。
在调用 now()
方法时,不严格需要传递 ZoneId.systemDefault()
作为参数。如果不传递它,你会得到相同的结果。由于当前时间取决于时区,我总是更喜欢明确指定我正在使用的时区,这样我和读者都知道我做了一个有意的选择。
链接
- Oracle 教程:日期和时间 解释了如何使用
java.time
。 - 维基百科文章:ISO 8601
英文:
OffsetDateTime
Use OffsetDateTime
instead of LocalDateTime
:
OffsetDateTime myObj = OffsetDateTime.now(ZoneId.systemDefault());
System.out.println(myObj);
I ran this just now in Asia/Hong_Kong time zone. The output was:
> 2020-10-07T03:25:03.254761+08:00
The format you asked for is ISO 8601. The classes of java,time generally print ISO 8601 format from their toString
methods. A LocalDateTime
object doesn’t include any offset or time zone. An OffsetDateTime
, as the name says, does include a UTC offset such as +08:00
and therefore also prints it from toString()
.
It is not strictly necessary to pass ZoneId.systemDefault()
as argument to now()
. You will get the same result if you leave it out. Since the current time is time zone dependent, I always prefer to make it explicit which time zone I am using, so I and the reader both know that I have made a conscious choice.
Links
- Oracle tutorial: Date Time explaining how to use java.time.
- Wikipedia article: ISO 8601
答案3
得分: 0
public class CurrentDateTimeExample2 {
public static void main(String[] args) {
System.out.println(java.time.Clock.systemUTC().instant());
}
}
示例输出:
> 2020-10-07T20:50:11.758582Z
英文:
public class CurrentDateTimeExample2 {
public static void main(String[] args) {
System.out.println(java.time.Clock.systemUTC().instant());
}
}
Example output:
> 2020-10-07T20:50:11.758582Z
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论