`ZonedDateTime.parse`在解析带有上午或下午时间的情况下无法正常工作。

huangapple go评论113阅读模式
英文:

ZonedDateTime.parse not working for parsing time with am or pm

问题

我正在学习Java尝试构建一个工具根据用户输入时间时区A和时区B),将特定时区A的时间转换为时区B的时间这是工具收集特定格式时间并将其转换为ZonedDateTime对象的部分

```java
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

public static String fullTime;
public static DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm a");
public static ZonedDateTime newTime;

public static void getHourAndMinutes(){
        System.out.print("请输入您想要的时间,格式为hh:mm am/pm\n");
        Scanner in = new Scanner(System.in);
        fullTime = in.nextLine();
        System.out.println(fullTime);
        newTime = ZonedDateTime.parse(fullTime, formatter);

我已经尝试过以类似于10:30PM、10:30 PM、10:30pm、10:30 pm、10:30p、10:30 p的格式输入时间,所有这些输入都导致抛出异常错误,我得到类似于以下的错误:

Exception in thread "main" java.time.format.DateTimeParseException: Text '10:30 pm' could not be parsed at index 6

您有任何想法我可能做错了什么吗?谢谢!


<details>
<summary>英文:</summary>

I am learning java, trying to build a tool to convert a specific time from timezone A to timezone B based on user input (input of the time, timezone A, and timezone B). This is about the part where the tool gathers a time in a specific format to convert it into a ZonedDateTime object.

import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

public static String fullTime;
public static DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm a");
public static ZonedDateTime newTime;

public static void getHourAndMinutes(){
System.out.print("Please type in the time you have in mind in format hh:mm am/pm\n");
Scanner in = new Scanner(System.in);
fullTime = in.nextLine();
System.out.println(fullTime);
newTime = ZonedDateTime.parse(fullTime, formatter);


I have tried to enter the time in formats like  10:30PM, 10:30 PM, 10:30pm, 10:30 pm, 10:30p, 10:30 p, all of these entries has caused exception error to be thrown, I&#39;m getting errors like this one

Exception in thread "main" java.time.format.DateTimeParseException: Text '10:30 pm' could not be parsed at index 6


Any idea what I might be doing wrong? Thanks!

</details>


# 答案1
**得分**: 4

由于用户输入的内容只表示时间,您需要将其解析为[LocalTime][1],另一个错误是您使用了错误的模式,`H`表示小时(0-23);您需要在[DateTimeFormatter][2]中使用`h`。

```java
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("hh:mm a");

LocalTime localTime = LocalTime.parse("10:30 PM", formatter);

将输入解析为LocalTime后,您可以将其转换为ZonedDateTime。但是,您必须指定日期(LocalDate)以及时间和时区。您的问题中的代码只包含了时间,缺少了实例化ZonedDateTime所需的日期和时区。

ZonedDateTime dateTime = localTime.atDate(LocalDate.now()).atZone(ZoneId.systemDefault());

然后,您可以使用withZoneSameInstant将其转换为另一个时区。

ZonedDateTime result = dateTime.withZoneSameInstant(ZoneId.of("America/New_York"));
英文:

Since the user entering input is just representing time you need to parse it into LocalTime, and the other mistake is you are using the wrong pattern, H is hour-of-day (0-23); you need h in DateTimeFormatter

DateTimeFormatter formatter = DateTimeFormatter.ofPattern(&quot;hh:mm a&quot;);

LocalTime localTime = LocalTime.parse(&quot;10:30 PM&quot;,formatter);

After parsing the input into LocalTime you can convert it into ZonedDateTime. But you must specify a date (LocalDate) as well as the time and the zone. Your code in the Question had only the time-of-day, and lacked the date and zone needed to instantiate a ZonedDateTime.

ZonedDateTime dateTime = localTime.atDate(LocalDate.now()).atZone(ZoneId.systemDefault());

And then you can convert it into another zone using withZoneSameInstant

ZonedDateTime result = dateTime.withZoneSameInstant(ZoneId.of(&quot;America/New_York));

答案2

得分: 1

抱歉,我只返回翻译好的部分,不提供额外的内容:

  1. 对于 ZonedDateTime,您需要日期、时间和时区。如果要将仅包含一天时间的字符串解析为 ZonedDateTime,则需要提供默认日期和默认时区。但是,我建议您将其解析为 LocalTime,它恰好是没有日期和时区的时间。解析后,您可以进行转换。您需要为您的转换选择一个日期,因为时区 A(和 B)可能因为夏令时(DST)和/或基本UTC偏移的历史和/或未来更改而在不同日期使用不同的UTC偏移。

  2. 您需要为您的格式化程序提供一个区域设置,以告诉它使用哪种语言来表示上午和下午。例如:DateTimeFormatter.ofPattern("HH:mm a", Locale.ENGLISH)

  3. 您需要以正确的大小写(大写或小写)输入区域设置中指定的上午或下午。

  4. 要解析包含在上午或下午的小时的字符串,您需要在格式模式字符串中使用小写 hh,而不是大写 HH,后者用于表示一天中的小时,范围从00到23。

英文:

> Any idea what I might be doing wrong? Thanks!

A number of things, I am afraid.

  1. For a ZonedDateTime you need a date and a time and a time zone. For parsing a string containing only time of day into a ZonedDateTime you would have needed to supply a default date and a default time zone. However, instead I would parse into a LocalTime, which is exactly a time if day without date and without time zone. After parsing you may convert. You need to decide a date for your conversion since your time zone A (and B too) probably uses a different UTC offset on different dates because of summer time (DST) and/or historic and/or future changes in their base UTC offset.
  2. You need to provide a locale for your formatter to tell it in which language to assume AM and PM. For example DateTimeFormatter.ofPattern(&quot;HH:mm a&quot;, Locale.ENGLISH).
  3. You need to enter AM or PM in the correct case (upper or lower case) for the locale you specified.
  4. For parsing a string with hour within AM or PM from 01 through 12 you need to use lower case hh in the format pattern string. Not upper case HH, which would be for hour of day from 00 through 23.

huangapple
  • 本文由 发表于 2020年4月9日 13:02:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/61114302.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定