gpt4 book ai didi

Java:解析并应用新模式后日期发生变化

转载 作者:行者123 更新时间:2023-12-01 17:22:21 25 4
gpt4 key购买 nike

我正在尝试将用户可读的时间字符串转换为 SQL 兼容的字符串。因此我使用以下代码:

// User Date
SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.YYYY - HH:mm:ss");
String dateTimeUI = sdf.format(new Date());
System.out.println("UI: " + dateTimeUI);
labelDatumZeit.setText(dateTimeUI);
System.out.println("[UI]: " + dateTimeUI);

// SQL-Date
SimpleDateFormat sdf2 = new SimpleDateFormat("dd.MM.YYYY - HH:mm:ss");
Date d = sdf2.parse(dateTimeUI);
sdf2.applyPattern("yyyy-MM-dd HH:mm:ss");
dateTimeSQL = sdf2.format(d);
System.out.println("[SQL]: " + dateTimeSQL);

转换为新格式后,我的数据始终是 2012-12-31(+正确时间)。这是为什么?

最佳答案

YYYY 是星期年。 yyyy 是年份。如果您期望两者是同一件事,那么这就是问题所在。

来自javadoc :

A week year is in sync with a WEEK_OF_YEAR cycle. All weeks between the first and last weeks (inclusive) have the same week year value. Therefore, the first and last days of a week year may have different calendar year values.

For example, January 1, 1998 is a Thursday. If getFirstDayOfWeek() is MONDAY and getMinimalDaysInFirstWeek() is 4 (ISO 8601 standard compatible setting), then week 1 of 1998 starts on December 29, 1997, and ends on January 4, 1998. The week year is 1998 for the last three days of calendar year 1997. If, however, getFirstDayOfWeek() is SUNDAY, then week 1 of 1998 starts on January 4, 1998, and ends on January 10, 1998; the first three days of 1998 then are part of week 53 of 1997 and their week year is 1997.

您想在两种格式中使用yyyy

关于Java:解析并应用新模式后日期发生变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17674631/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com