如何将给定的时间(字符串)转换为 LocalTime?

22 浏览
0 Comments

如何将给定的时间(字符串)转换为 LocalTime?

我将要求用户输入一个特定的时间:上午10点,下午12点30分,下午2点47分,凌晨1点09分,下午5点等。\n我将使用Scanner来获取用户的输入。\n如何将该String解析/转换为LocalTime对象?Java中是否有任何内置函数可以实现这个功能?

0
0 Comments

问题出现的原因是因为代码中使用了默认的Locale,而不同的Locale对时间的表示方式是不同的,因此会导致解析失败。为了解决这个问题,可以使用DateTimeFormatter的ofPattern方法,指定一个特定的Locale,以确保解析的准确性。

以下是解决方法的代码示例:

import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
import java.util.stream.Stream;
public class Main {
    public static void main(String args[]) {
        DateTimeFormatter parser = DateTimeFormatter.ofPattern("h[:mm]a", Locale.ENGLISH);
        Stream.of(
                "10AM",
                "12:30PM",
                "2:47PM",
                "1:09AM",
                "5PM"
        )
        .map(s -> LocalTime.parse(s, parser))
        .forEach(System.out::println);
        DateTimeFormatter errorProneParser = DateTimeFormatter.ofPattern("h[:mm]a", Locale.CHINA);
        LocalTime localTime = LocalTime.parse("10AM", errorProneParser);
    }
}

以上代码将给定的时间字符串转换为LocalTime对象,使用了指定的Locale.ENGLISH确保解析的准确性。输出结果如下:

10:00
12:30
14:47
01:09
17:00

这段代码展示了使用不同的Locale解析时间字符串的结果。当使用默认的Locale(例如Locale.CHINA)时,解析会失败,并抛出异常:

Exception in thread "main" java.time.format.DateTimeParseException: Text '10AM' could not be parsed at index 2
        at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:2052)
        at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1954)
        at java.base/java.time.LocalTime.parse(LocalTime.java:465)
        at Main.main(Main.java:24)

通过指定特定的Locale,可以避免解析失败的问题,确保代码在不同的环境下的可靠性。

0
0 Comments

问题的出现原因是想要将给定的时间字符串转换为LocalTime对象,但是现有的代码只能处理特定格式的时间字符串,无法处理其他格式的时间字符串。为了解决这个问题,可以使用DateTimeFormatter类来定义时间字符串的格式,然后使用LocalTime类的parse方法将时间字符串转换为LocalTime对象。

具体的解决方法如下所示:

DateTimeFormatter parseFormat = new DateTimeFormatterBuilder().appendPattern("hh[:mm]a").toFormatter();
LocalTime localTime = LocalTime.parse(timeValue, parseFormat);

上述代码中,首先使用DateTimeFormatterBuilder类的appendPattern方法定义时间字符串的格式。其中,"hh"表示小时,"[:mm]"表示可选的分钟,"a"表示上午或下午的标识。然后,使用toFormatter方法将定义好的格式转换为DateTimeFormatter对象。

接下来,使用LocalTime类的parse方法将时间字符串转换为LocalTime对象。其中,timeValue表示给定的时间字符串。

需要注意的是,上述代码只能处理格式为"hh:mma"(如12:30AM)的时间字符串,无法处理其他格式的时间字符串(如5AM或2:12PM)。

0
0 Comments

如何将给定的时间(字符串)转换为LocalTime?

问题的原因:

需要将给定的时间字符串转换为LocalTime类型,以便在Java程序中进行时间计算和处理。

解决方法:

可以使用java.time.format.DateTimeFormatter类来实现时间字符串到LocalTime类型的转换。具体步骤如下:

1. 创建一个DateTimeFormatter对象,使用ofPattern方法指定时间字符串的格式。例如,我们可以使用"h[:mm]a"作为格式,其中"h"表示12小时制的小时数,"mm"表示分钟,"a"表示AM或PM。

2. 调用LocalTime类的parse方法,传入时间字符串和DateTimeFormatter对象作为参数,将时间字符串转换为LocalTime类型的对象。

下面是一个示例代码:

DateTimeFormatter parser = DateTimeFormatter.ofPattern("h[:mm]a");
LocalTime localTime = LocalTime.parse("10AM", parser);

解释格式的含义:

- "h"表示1到12之间的上午/下午小时数,可以是1位或2位数字。

- "[]"表示可选部分的分隔符(方括号内的内容都是可选的)。

- ":mm"表示一个冒号和2位数字的分钟数。

- "a"表示上午或下午的标识符。

通过上述代码和解释,我们可以将任何合法的时间字符串转换为LocalTime类型的对象。

0