Car*_*dez 0 java format time datetime android
我很难尝试将字符串时间格式化为 MM:DD::YY 并且只有时间。我从 IP 获取以下格式的时间
2021-09-10T00:37:42Z
Run Code Online (Sandbox Code Playgroud)
我想在以下位置显示日期和时间:
09/08/2021
Run Code Online (Sandbox Code Playgroud)
时间
09:50PM
Run Code Online (Sandbox Code Playgroud)
将给定的字符串解析为OffsetDateTime然后从中获取LocalDate和LocalTime分离。
import java.time.LocalDate;\nimport java.time.LocalTime;\nimport java.time.OffsetDateTime;\nimport java.time.format.DateTimeFormatter;\nimport java.util.Locale;\n\npublic class Main {\n public static void main(String[] args) {\n String strDateTime = "2021-09-10T00:37:42Z";\n OffsetDateTime odt = OffsetDateTime.parse(strDateTime);\n LocalTime time = odt.toLocalTime();\n LocalDate date = odt.toLocalDate();\n System.out.println(time);\n System.out.println(date);\n\n // #########Custom formats #########\n\n DateTimeFormatter dtfDate = DateTimeFormatter.ofPattern("MM/dd/uuuu", Locale.ENGLISH);\n String formattedDateString = date.format(dtfDate);\n System.out.println(formattedDateString);\n\n DateTimeFormatter dtfTime = DateTimeFormatter.ofPattern("hh:mm a", Locale.ENGLISH);\n String formattedTimeString = time.format(dtfTime);\n System.out.println(formattedTimeString);\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n输出:
\n00:37:42\n2021-09-10\n09/10/2021\n12:37 AM\nRun Code Online (Sandbox Code Playgroud)\n\n从Trail: Date Time了解有关现代日期时间 API *的更多信息。
\n根据Ole VV的重要评论进行更新:
\n\n\nOP \xe2\x80\x94 或他们的用户 \xe2\x80\x94 可能需要他们自己时区的日期和时间。
\n
为了获取特定时区的日期和时间部分,例如America/Los_Angeles,您应该将给定的日期时间字符串解析为ZonedDateTime并使用 将其转换为ZonedDateTime特定时区的ZonedDateTime#withZoneSameInstant。其余的事情将与原始答案保持不变。
演示:
\nimport java.time.LocalDate;\nimport java.time.LocalTime;\nimport java.time.ZoneId;\nimport java.time.ZonedDateTime;\nimport java.time.format.DateTimeFormatter;\nimport java.util.Locale;\n\npublic class Main {\n public static void main(String[] args) {\n String strDateTime = "2021-09-10T00:37:42Z";\n ZonedDateTime zdt = ZonedDateTime.parse(strDateTime);\n\n ZonedDateTime zdtLosAngeles = zdt.withZoneSameInstant(ZoneId.of("America/Los_Angeles"));\n\n LocalTime time = zdtLosAngeles.toLocalTime();\n LocalDate date = zdtLosAngeles.toLocalDate();\n System.out.println(time);\n System.out.println(date);\n\n // #########Custom formats #########\n\n DateTimeFormatter dtfDate = DateTimeFormatter.ofPattern("MM/dd/uuuu", Locale.ENGLISH);\n String formattedDateString = date.format(dtfDate);\n System.out.println(formattedDateString);\n\n DateTimeFormatter dtfTime = DateTimeFormatter.ofPattern("hh:mm a", Locale.ENGLISH);\n String formattedTimeString = time.format(dtfTime);\n System.out.println(formattedTimeString);\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n输出:
\n17:37:42\n2021-09-09\n09/09/2021\n05:37 PM\nRun Code Online (Sandbox Code Playgroud)\n\n* 出于任何原因,如果您必须坚持使用 Java 6 或 Java 7,则可以使用ThreeTen-Backport,它将大多数java.time功能向后移植到 Java 6 和 7。如果您正在处理 Android 项目和 Android API level 仍然不符合 Java-8,请检查通过脱糖可用的 Java 8+ API以及如何在 Android 项目中使用 ThreeTenABP。\n
\n| 归档时间: |
|
| 查看次数: |
153 次 |
| 最近记录: |