Java日期区域格式至简单格式

发布于 2025-01-26 02:41:02 字数 210 浏览 2 评论 0原文

是否可以在Java(可能ISO 8601)中转换这种格式>哪个是字符串,以日期或时间类型的类别的“ yyyy-mm-dd”为字符串,还是应该使用字符串方法完成?

例子:

you receive this -> "2022-11-11T00:00:00"
you convert to this -> "2022-11-11"

is it possible to convert this kind of format in Java (probably ISO 8601) "2022-11-11T00:00:00" or this "2022-11-11T12:00:00+01:00" which comes as a string, to simple format "yyyy-mm-dd" with Date or Time kind of classes, or it should be done with string methods?

Example:

you receive this -> "2022-11-11T00:00:00"
you convert to this -> "2022-11-11"

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

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

发布评论

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

评论(1

谁的新欢旧爱 2025-02-02 02:41:02

推荐方法:java.time

如果您需要根据值计算任何内容,或者可能会发现一周中的一天,则最好使用java.Time

public static void main(String[] args) {
    // example input in ISO format
    String first = "2022-11-11T00:00:00";
    String second = "2022-11-11T12:00:00+01:00";
    // parse them to suitable objects
    LocalDateTime ldt = LocalDateTime.parse(first);
    OffsetDateTime odt = OffsetDateTime.parse(second);
    // extract the date from the objects (that may have time of day and offset, too)
    LocalDate firstDate = ldt.toLocalDate();
    LocalDate secondDate = odt.toLocalDate();
    // format them as ISO local date, basically the same format as the input has
    String firstToBeForwarded = firstDate.format(DateTimeFormatter.ISO_LOCAL_DATE);
    String secondToBeForwarded = secondDate.format(DateTimeFormatter.ISO_LOCAL_DATE); 
    // print the results (or forward them as desired)
    System.out.println(firstToBeForwarded);
    System.out.println(secondToBeForwarded);
}

: 不建议使用此示例的输出

2022-11-11
2022-11-11

,但可能会出现:字符串操纵

如果您只需要

  • 提取日期零件(年度和每月的年度,每月),并且
  • 您确定它始终是第一个您收到的String s的10个字符

您可以简单地采用前10个字符:

String toBeForwarded = "2022-11-11T00:00:00".substring(0, 10);

此行将存储“ 2022-11-11”tobeforwarded 。

Recommended way: java.time

If you need to calculate anything based on the values or maybe find out the day of week, you will be best adviced using java.time:

public static void main(String[] args) {
    // example input in ISO format
    String first = "2022-11-11T00:00:00";
    String second = "2022-11-11T12:00:00+01:00";
    // parse them to suitable objects
    LocalDateTime ldt = LocalDateTime.parse(first);
    OffsetDateTime odt = OffsetDateTime.parse(second);
    // extract the date from the objects (that may have time of day and offset, too)
    LocalDate firstDate = ldt.toLocalDate();
    LocalDate secondDate = odt.toLocalDate();
    // format them as ISO local date, basically the same format as the input has
    String firstToBeForwarded = firstDate.format(DateTimeFormatter.ISO_LOCAL_DATE);
    String secondToBeForwarded = secondDate.format(DateTimeFormatter.ISO_LOCAL_DATE); 
    // print the results (or forward them as desired)
    System.out.println(firstToBeForwarded);
    System.out.println(secondToBeForwarded);
}

The output of this example is

2022-11-11
2022-11-11

Not recommended, but possible: String manipulation

If you just have to

  • extract the date part (year, month of year and day of month) and
  • you are sure it will always be the first 10 characters of the Strings you receive

you could simply take the first 10 characters:

String toBeForwarded = "2022-11-11T00:00:00".substring(0, 10);

This line would store "2022-11-11" in toBeForwarded.

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