简体   繁体   English

在Java中,是否可以将日期和时间转换为没有时区的ISO 8601格式的字符串?

[英]In Java, Can I Convert A Date and Time To An ISO 8601 formatted String Without A Timezone?

A history table of an old source system has 2 fields regarding date/time: a date (ex: 12/20/2017) String and a time (14:33:00) String. 旧源系统的历史记录表具有2个有关日期/时间的字段:日期(例如:12/20/2017)字符串和时间(14:33:00)字符串。 I have no timezone information I can use. 我没有可以使用的时区信息。 The web service I'm creating is being consumed by a UI team which wants this information as an ISO 8601 formatted String. UI团队正在使用我正在创建的Web服务,该团队希望此信息为ISO 8601格式的String。 I'm using Java 8. Is creating an ISO 8601 formatted String version to return to the UI possible without a timezone? 我正在使用Java8。是否可以创建没有时区的ISO 8601格式的String版本以返回UI?

Yes, you can use DateTimeFormatter ISO_LOCAL_DATE_TIME to format without the time zone: 是的,您可以使用DateTimeFormatter ISO_LOCAL_DATE_TIME来格式化没有时区的格式:

String input = "12/20/2017 14:33:00";
DateTimeFormatter inputFormat = DateTimeFormatter.ofPattern("MM/dd/yyyy HH:mm:ss");
LocalDateTime date = LocalDateTime.parse(input, inputFormat);
String output = DateTimeFormatter.ISO_LOCAL_DATE_TIME.format(date);
System.out.println(output);

Output: 输出:

2017-12-20T14:33:00

shmosel's answer is correct. shmosel的答案是正确的。 I would find it clearer and cleaner to make explicit in the code that the date and the time come from separate fields: 我会发现在代码中明确指出日期和时间来自不同的字段会更加清晰明了:

    String dateString = "12/20/2017";
    String timeString = "14:33:00";

    DateTimeFormatter dateInputFormat = DateTimeFormatter.ofPattern("MM/dd/uuuu");
    String iso8601DateTimeString = LocalDate.parse(dateString, dateInputFormat)
            .atTime(LocalTime.parse(timeString))
            .toString();

This produces 2017-12-20T14:33 , which is ISO 8601 compliant. 这将产生符合ISO 8601的2017-12-20T14:33

Instead of toString() you may use format(DateTimeFormatter.ISO_LOCAL_DATE_TIME) . 可以使用format(DateTimeFormatter.ISO_LOCAL_DATE_TIME)代替toString() format(DateTimeFormatter.ISO_LOCAL_DATE_TIME) Funnily it doesn't always produce the same result: we now get 2017-12-20T14:33:00 , that is, the seconds are given even when they are 0. Both versions are ISO 8601 compliant. 有趣的是,它并不总是产生相同的结果:我们现在得到2017-12-20T14:33:00 ,即,即使它们是0,也给出秒。这两个版本都符合ISO 8601。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将ISO 8601日期时间字符串转换为java.util.LocalDateTime? - How do I convert an ISO 8601 date time String to java.util.LocalDateTime? 如何将ISO 8601格式的DateTime转换为Java中的另一个时区? - How to convert ISO 8601 formatted DateTime to another time zone in Java? 从ISO8601日期时间字符串中提取时区 - Extract timezone from ISO8601 date time string 将日期从ISO 8601 Zulu字符串转换为Java 8中的java.time.Instant - Convert Date from ISO 8601 Zulu string to java.time.Instant in Java 8 我需要一个可以处理任何有效的 W3C ISO 8601 日期/时间字符串的 java.time 解析器 - I need a java.time parser that can handle any valid W3C ISO 8601 date/time string SAP PI UDF 将没有时间的日期转换为日期时间 ISO8601 字符串 - SAP PI UDF to convert date without time to datetime ISO8601 string 根据 Java 中的 State 名称将 ISO8601 日期时间转换为另一种 ISO8601 格式? - convert ISO8601 date time to another ISO8601 format based on State name in Java? Java-将字符串转换为ISO 8601日期格式错误Joda-Time - Java - Convert string to ISO 8601 date format error Joda-Time 如何在 Java 中将 UTC 日期时间转换为 ISO 8601 格式? - how to convert UTC date-time into ISO 8601 format in Java? Java将ISO 8601字符串转换为忽略偏移量的日期 - Java convert ISO 8601 string to Date ignoring offset
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM