- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我们需要使用输入格式
dd.MM. HH:mm
对于扫描仪(不使用额外的变量!),如何将这些扫描值放入开始变量中?
我的程序无法运行。总是打印“无效输入!”虽然我的输入似乎没有错误:
System.out.println("Start:");
sc.nextLine();
sc.findInLine("(\\d\\d)\\.(\\d\\d)\\. (\\d\\d):(\\d\\d)");
try{
MatchResult mr =sc.match();
int month = Integer.parseInt(mr.group(2));
int day = Integer.parseInt(mr.group(1));
int hour = Integer.parseInt(mr.group(3));
int minute = Integer.parseInt(mr.group(4));
LocalDateTime start = LocalDateTime.of(year, month, day, hour, minute);
System.out.println(start);
} catch (IllegalStateException e)
{
System.err.println("Invalid input!");
}
我的输入:
20.08 13:00
最佳答案
在打印 Start:
后,不应调用 sc.nextLine();
。这有效地告诉扫描仪读取行 20.08。 13:00
您正在打印并忽略它,因为您没有存储结果。然后,当您调用 findInLine
时,扫描仪会尝试匹配下一个输入行(我猜该行是空的,您只需再次按 Enter 即可)但失败.
然后,由于没有匹配,sc.match()
抛出一个IllegalStateException
:
Returns the match result of the last scanning operation performed by this scanner. This method throws
IllegalStateException
if no match has been performed, or if the last match was not successful.
因此更正后的代码将是:
Scanner sc = new Scanner(System.in);
System.out.println("Start:");
// sc.nextLine(); <-- don't do that, this reads and ignore your input
sc.findInLine("(\\d\\d)\\.(\\d\\d)\\. (\\d\\d):(\\d\\d)");
try {
MatchResult mr = sc.match();
int month = Integer.parseInt(mr.group(2));
int day = Integer.parseInt(mr.group(1));
int hour = Integer.parseInt(mr.group(3));
int minute = Integer.parseInt(mr.group(4));
LocalDateTime start = LocalDateTime.of(2015, month, day, hour, minute);
System.out.println(start);
} catch (IllegalStateException e) {
System.err.println("Invalid input!");
}
关于java - 如何正确扫描LocalDateTime?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35611653/
我在从两个 LocalDateTime 对象获取秒数时遇到问题。 // a timestammp for the time when the connection was established pu
我有字段 initiationDate,它由 ToStringSerializer 类序列化为 ISO-8601 格式。 @JsonSerialize(using = ToStringSerializ
我有一个初始日期和一个 cron 表达式。我怎样才能找到满足此 cron 表达式的下一个日期?。 String cronExpresion = "* * * * * *" LocalD
我需要做一个 LocalDateTime UTC 日期到另一个日期的转换 LocalDateTime考虑特定时区 tz 的变量。 在我的研究中,我找到了很多解决方案,但它们都转换了 LocalDate
这个问题在这里已经有了答案: Java 8: Difference between two LocalDateTime in multiple units (11 个答案) 关闭 6 年前。 我知道
给定日期时间和时间, LocalDateTime rightDateWrongTime = new LocalDateTime("2017-03-02T15:23:00.000");
将 LocalDateTime 转换为 UTC 中的 LocalDateTime。 LocalDateTime convertToUtc(LocalDateTime date) { //do
这是我的代码: private final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMM dd HH:mm:ss");
我想为一些沉迷于日期值的数据创建一个小型 GUI。所以我在 JavaFX 中使用 LocalDateTimeTextField。所以我将使用以下代码获取选定的时间: LocalDateTimeText
解析 DateTime 时出现异常。我在这里缺少什么 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("0DDDHHmmss");
我有 REST 网络服务公开资源和创建日期。它是用 Java 8 编写的 - 使用 LocalDateTime。 Jackson 2 正在将其序列化为: “创建日期”:[2016, 5, 19, 18
有什么简单的方法可以将 Java 8 的 LocalDateTime 转换为 Joda 的 LocalDateTime? 其中一种方法是将其转换为字符串,然后从该字符串创建 Joda 的 LocalD
我在 Java 8 中使用 LocalDateTime.now() 获取 LocalDateTime。但有时这个 now() 函数会以没有秒的格式返回时间给我。我认为这是因为秒为零,但我需要秒。 代码
Date与LocalDateTime和LocalDate互相转换思路 Date转LocalDateTime和LocalDate都可以通过Date先转换成Instant然后再转换成LocalDateTi
在本教程中,我们将为LocalDate和LocalDateTime类编写自定义序列化程序和反序列化程序。 LocalDateSerializer LocalDateTimeSerializer Loc
从 API 传递的字符串通常遵循格式 yyyy-MM-dd HH:mm:ss.SSS但是当时间戳中有尾随 0 时,它们会被截断,例如 2019-07-16 13:29:15.100转换为 2019-0
我在 Java 8 中使用 spring boot 2.2.6 和 Jackson 2.10.3。我在整个项目中使用 localdatetime 对象。 Jackson 无法正确解析 LocalDat
我有使用年份和一年中的某一天 (1-365/366) 加上一天中的时间的数据,例如 2018-338T14:02:57.47583,而不是年月日。 我正在尝试编写一个输入时间戳的函数,它通过一堆正则表
我正在尝试将以下日期时间字符串解析为 LocalDateTimeObject,但是我无法识别日期时间字符串的格式。 2021 年 10 月 9 日星期六 02:10:23 -0400 Stri
我尝试并失败(或部分成功)为 LocalDateTime 实现扩展功能 这是我的尝试: fun LocalDateTime.isNotBefore(other: ChronoLocalDateTime
我是一名优秀的程序员,十分优秀!