标签: datetimeformatter

在解析具有大写月份名称的日期时,我得到 java.time.format.DateTimeParseException

我有几行代码,如下所示

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MMM-yy");
formatter = formatter.withLocale( Locale.getDefault() );  
LocalDate date = LocalDate.parse("11-NOV-20", formatter);
Run Code Online (Sandbox Code Playgroud)

这给出了以下异常

 Exception in thread "main" java.time.format.DateTimeParseException: Text '11-NOV-20' could not be parsed at index 3
        at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1949)
        at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)
        at java.time.LocalDate.parse(LocalDate.java:400)
        at src.DateTimeTest.main(DateTimeTest.java:30)
Run Code Online (Sandbox Code Playgroud)

如果我将月份从11 月更改为11 月,效果很好。我从数据库表中获取这个值。我是否必须以编程方式更改它,或者有什么方法可以处理它?

java-8 java-time datetimeformatter

4
推荐指数
1
解决办法
919
查看次数

解析 LocalDate 但得到 DateTimeParseException;dd-MMM-uuuu

我正在尝试将 a 转换StringLocalDateusing DateTimeFormatter,但收到异常:

java.time.format.DateTimeParseException:无法在索引 5 处解析文本“ 2021-10-31 ”

我的代码是

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MMM-uuuu");
String text = "2021-10-31";
LocalDate date = LocalDate.parse(text, formatter);
Run Code Online (Sandbox Code Playgroud)

我正在尝试从输入日期转换2021-10-3131-Oct-2021.

java java-time localdate datetimeformatter datetimeparseexception

4
推荐指数
1
解决办法
856
查看次数

SimpleDateFormat 与 DateTimeFormatter

我有一个用例,我必须比较 2 个字符串日期,例如

final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
 System.out.println(dateFormat.parse("2019-07-07").compareTo(dateFormat.parse("2019-07-07 23:59:59"))>0);
Run Code Online (Sandbox Code Playgroud)

使用 SimpleDateFormat 的上述语句工作得很好,现在我尝试使用 DateTimeFormatter

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        System.out.println( LocalDate.parse("2019-07-07", formatter).compareTo(LocalDate.parse("2019-07-07 23:59:59", formatter))>=0);
Run Code Online (Sandbox Code Playgroud)

这会失败,但有例外:-

Exception in thread "main" java.time.format.DateTimeParseException: Text '2019-07-07 23:59:59' could not be parsed, unparsed text found at index 10
    at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1952)
    at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)
    at java.time.LocalDate.parse(LocalDate.java:400)
    at com.amazon.payrollcalculationengineservice.builder.EmployeeJobDataBuilder.main(EmployeeJobDataBuilder.java:226)
Run Code Online (Sandbox Code Playgroud)

如何使用 DateTimeFormatter 避免这种情况,我作为输入传递的字符串可以是任何格式,如 yyyy-mm-dd 或 yyyy-mm-dd hh:mm:ss ,我不想明确编写格式检查,所以可以我使用 DateTimeFormatter 因为我可以使用 SimpleDateFormat 库来做到这一点。

java simpledateformat datetimeformatter

4
推荐指数
1
解决办法
5832
查看次数

如何更改 Java 中的时间格式 - “am/pm”与“AM/PM”

先前创建的测试使用DateTimeFormatter.ofPattern("ha");并返回“10AM”(表示“ 2017-04-09T10:00-06:00[US/Mountain]”)。

在我的 MacO 和 Java 下 ['openjdk version "11.0.12"'] 我得到“10am”

"10AM" != "10am"
Run Code Online (Sandbox Code Playgroud)

在规范中,我看到“ha”应该创建“10AM”而不是“10am”,请参阅:https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html

有什么建议吗?

java datetime-format timeofday datetimeformatter

3
推荐指数
1
解决办法
2960
查看次数

如何在 Java 8 上使用 LocalDateTime.now() 获取当前的 LocalDateTime 包括秒数

我使用 LocalDateTime.now()在Java 8 中获得 LocalDateTime 。但有时这个 now() 函数以没有秒的格式返回时间给我。我认为这是因为秒为零,但我需要秒。

代码:

List <LocalDateTime> times = Arrays.asList(
                                 LocalDateTime.now(),
                                 LocalDateTime.parse("2020-09-13T20:53", DateTimeFormatter.ISO_LOCAL_DATE_TIME),
                                 LocalDateTime.parse(LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME))
                             );

for (LocalDateTime time: times)
    System.out.println("Time: " + time);
Run Code Online (Sandbox Code Playgroud)

安慰:

Time: 2020-09-13T18:42:25.775
Time: 2020-09-13T20:53
Time: 2020-09-13T18:42:25.779
Run Code Online (Sandbox Code Playgroud)

Time: 2020-09-13T20:53 已经没有秒了。

PD:我有一个调度程序,它每 30 秒运行一次“LocalDatetime.now ()”。这是它在控制台中显示的内容。

2020-09-10T09:14:00.001
2020-09-10T09:14:30.001
2020-09-10T09:15:00.001
2020-09-10T09:15:30.001
2020-09-10T09:16:00.001
2020-09-10T09:16:30
2020-09-10T09:17
2020-09-10T09:17:30.001
2020-09-10T09:18:00.001
2020-09-10T09:18:30
2020-09-10T09:19:00.001
2020-09-10T09:19:30.001
2020-09-10T09:20:00.001
2020-09-10T09:20:30
2020-09-10T09:21:00.001
2020-09-10T09:21:30
2020-09-10T09:22:00.001
2020-09-10T09:22:30.001
2020-09-10T09:23
2020-09-10T09:23:30.001
2020-09-10T09:24
2020-09-10T09:24:30
2020-09-10T09:25
2020-09-10T09:25:30
2020-09-10T09:26:00.001
2020-09-10T09:26:30
Run Code Online (Sandbox Code Playgroud)

java spring localdate localdatetime datetimeformatter

2
推荐指数
2
解决办法
671
查看次数

Kotlin DateTimeParseException

https://api.spacexdata.com/v3/launches获取日期 该日期的格式为:2006-03-25T10:30:00+12:00。我想将其转换为“dd,mm,yyyy”,但总是收到错误:“java.time.format.DateTimeParseException:文本'2006-03-25T10:30:00 + 12:00'无法解析,未解析的文本发现于索引 10"

我的代码:

val formatter = DateTimeFormatter.ofPattern("dd, mm, yyyy", Locale.US)
val myDate = LocalDate.parse(launchDate, formatter)

var launchDateConverted: String=  myDate.toString()
Run Code Online (Sandbox Code Playgroud)

我在字符串中获取数据,然后将其转换为日期进行格式化,然后将日期转换回字符串以在 UI 上显示。我使用了不同的方法,但找不到正确的方法。我当前的语言环境是“RU”。

datetime kotlin java-time datetimeformatter offsetdatetime

2
推荐指数
1
解决办法
2813
查看次数

在java中将字符串格式化为日期

我想比较 WebElements 日期以验证排序是否正确。然而,日期的值例如如下:“2021年4月5日12:30pm”、“2018年10月22日09:18am”、“2015年2月1日11:36pm”、

我尝试了下面的代码,但它返回 1970 作为日期,并且在日期为 2 位数字的情况下会出现错误:


DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("MMMM d yyyy HH:mma", Locale.US);
LocalDate date = LocalDate.parse(dt, dateFormatter);

// or

Date sdf = new SimpleDateFormat("MMMM d u hh:mma").parse(dt);



Run Code Online (Sandbox Code Playgroud)

java datetime simpledateformat datetimeformatter

2
推荐指数
1
解决办法
199
查看次数

如何将“2021-11-20+01:00”格式的日期字符串解析为 ZonedDateTime - 错误

如何将“2021-11-20+01:00”格式的日期解析为ZonedDateTime?我正在尝试:

String value = "2021-11-20+01:00";
ZonedDateTime zDate = ZonedDateTime.parse(value, DateTimeFormatter.ofPattern("yyyy-MM-ddxxx"));
Run Code Online (Sandbox Code Playgroud)

但出现这个奇怪的错误:

java.time.format.DateTimeParseException:无法解析文本“2021-11-20+01:00”:无法从 TemporalAccessor 获取 ZonedDateTime:{OffsetSeconds=3600},ISO 解析为 java 类型的 2021-11-20。时间.格式.解析

...不支持的字段:InstantSeconds...

有什么建议么?欧洲 VIES VAT ID 检查系统在接收 XML (SOAP) 响应时使用此时间格式: <requestDate>2021-11-20+01:00</requestDate>。同样的错误与OffsetDateTime..

有趣的是,Javadoc 说“三个字母 (x) 输出小时和分钟,带有冒号,例如 '+01:30'”。那么为什么上面的模式不起作用呢?

也尝试过这个 - 同样的错误:

ZonedDateTime zDate = ZonedDateTime.parse(value, DateTimeFormatter.ISO_OFFSET_DATE);
Run Code Online (Sandbox Code Playgroud)

完整的错误日志:

Exception in thread "main" java.time.format.DateTimeParseException: Text '2021-11-20 +01:00' could not be parsed: Unable to obtain OffsetDateTime from TemporalAccessor: {OffsetSeconds=3600},ISO resolved to 2021-11-20 of type java.time.format.Parsed
    at java.base/java.time.format.DateTimeFormatter.createError(DateTimeFormatter.java:2017)
    at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1952)
    at …
Run Code Online (Sandbox Code Playgroud)

java parsing localdatetime datetimeformatter

2
推荐指数
1
解决办法
1356
查看次数

DateTimeFormatter - java.lang.IllegalArgumentException:模式字母太多:a

我的代码中有以下行java,我试图格式化日期字符串

 ZonedDateTime zonedDateTime= ZonedDateTime.ofInstant(instant, tz);
 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm aa");

 return zonedDateTime.format(formatter);
Run Code Online (Sandbox Code Playgroud)

在第 2 行,我收到此错误:

java.lang.IllegalArgumentException: Too many pattern letters: a
at java.time.format.DateTimeFormatterBuilder.parseField(DateTimeFormatterBuilder.java:1774)
Run Code Online (Sandbox Code Playgroud)

我如何创建一个格式字符串,DateTimeFormatter.ofPattern例如:

2023-04-21 7.00pm
Run Code Online (Sandbox Code Playgroud)

java date-format java-time zoneddatetime datetimeformatter

2
推荐指数
1
解决办法
1112
查看次数

如何将“2016 年 11 月 20 日 12:00:00 AM”解析为 LocalDateTime?

我发现了非常有趣的错误或类似的东西。

我使用这种日期格式MMM d, yyyy hh:mm:ss a,它会像这样打印日期

Aug 13, 2020 01:19:50 pm
Run Code Online (Sandbox Code Playgroud)

但是,当我分析Nov 20, 2016 12:00:00 AMLocalDateTime,它抛出异常

java.time.format.DateTimeParseException: Text 'Nov 20, 2016 12:00:00 AM' could not be parsed at index 22.
Run Code Online (Sandbox Code Playgroud)

在我将 'AM' 更改为 'am' 后,它完美运行!所以LocalDateTime字面上无法解析日期,因为大写 a?p?e?s?字母?我该如何解决这个问题,而不用将 'AM' 替换为 'am' 并将 'PM' 替换为 'pm'

编辑

SimpleDateTime格式没有这个问题,他忽略了 a?p?e?s? 字母寄存器(我的意思是大写或小写)我不想转换DateLocalDateTime

编辑 2

MMM d, yyyy hh:mm:ss A 将 'a' 替换为 'A' 也不起作用

java datetime-format localdatetime datetimeformatter

1
推荐指数
1
解决办法
177
查看次数