日期字段解析错误 [英] Parsing error for date field

查看:72
本文介绍了日期字段解析错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将 YYYY-MM-DD 格式的日期解析为 YYYYMMDD .如果使用以下功能,它将返回 YYYYMMDD 格式,但具有不同的DD.即: 2013-05-16 变为 20130515

I want to parse a date in YYYY-MM-DD format to YYYYMMDD. If I use the following function, it returns me a YYYYMMDD format but with a different DD. i.E: 2013-05-16 BECOMES 20130515

道歉的道歉:)我是Java的新手.

Apologies for sounding illiterate :) I am new to Java.

任何帮助将不胜感激.

String TestDate=yyyymmddParser.format(oLifEExtension.TestDate().getTime());
                    sb.append(TestDate)

推荐答案

不确定问题是针对 java.util.Date (日期加时间)还是 java.sql.Date (仅日期).在这两种情况下,都应该使用现代的java.time类,而不要使用麻烦的旧式日期时间类.

Not sure if the Question was meant for java.util.Date (a date plus time-of-day) or a java.sql.Date (a date-only). In both cases, you should be using the modern java.time classes rather than the troublesome legacy date-time classes.

其他一些 java.sql.Date 问题被链接为该问题的重复项.所以我在这里处理两个类(sql& util).

Some other java.sql.Date questions are linked as duplicates of this one. So I handle both classes (sql & util) here.

旧版 java.util.Date 类表示UTC时间轴上的时刻.这意味着一个带有日期的日期.诀窍是您的输入字符串仅用于日期值.您可以首先将字符串解析为仅日期的值,然后根据需要分配时间.

The legacy java.util.Date class represents a moment on the timeline in UTC. This means a date with a time-of-day. The trick is that your input string is for a date-only value. You can first parse your string as a date-only value, then assign a time-of-day if desired.

您的输入字符串符合YYYY-MM-DD的标准 ISO 8601 格式.解析/生成字符串时,java.time类默认为标准格式.因此,无需指定格式设置模式.

Your input string complies with the standard ISO 8601 format of YYYY-MM-DD. The java.time classes default to the standard formats when parsing/generating strings. So no need to specify a formatting pattern.

LocalDate ld = LocalDate.parse( "2013-05-16" ) ;

在一天中的某个时间,您可能需要一天的第一时刻.不要以为第一时间是 00:00:00 .在某些时区,诸如夏令时(DST)之类的异常可能会导致一天在不同的时间开始,例如 01:00:00 .为了解决这种异常情况,我们必须在确定一天的第一时刻时指定一个时区.

For a time-of-day, you probably want the first moment of the day. Do not assume the first moment is 00:00:00. In some time zones an anomaly such as Daylight Saving Time (DST) may cause a day to start at a different time such as 01:00:00. To account for such anomalies, we must specify a time zone in determining the first moment of the day.

大陆/地区,例如 美国/蒙特利尔 非洲/卡萨布兰卡 Pacific/Auckland .切勿使用3-4个字母的缩写,例如 EST IST ,因为它们不是真正的时区,不是标准化的,甚至不是唯一的(!).

Specify a proper time zone name in the format of continent/region, such as America/Montreal, Africa/Casablanca, or Pacific/Auckland. Never use the 3-4 letter abbreviation such as EST or IST as they are not true time zones, not standardized, and not even unique(!).

ZoneId z = ZoneId.of( "America/Montreal" ) ;

在询问java.time以确定该天的第一时刻时应用该区域.我们产生一个 ZonedDateTime 对象作为结果.

Apply that zone in asking java.time to determine the first moment of the day. We produce a ZonedDateTime object as the result.

ZonedDateTime zdt = ld.atStartOfDay( z ) ;

如果您需要一天中的特定时间,请应用 LocalTime 对象.请记住,您的时间在该特定区域的特定日期可能无效.例如,您可能要指定DST转换期间发生的时间.在这种情况下, ZonedDateTime 类具有用于调整以适应的策略.请务必阅读文档,以了解该策略以及由此产生的行为.

If you desire a specific time of day, apply a LocalTime object. Keep in mind that your time-of-day may not be valid on that particular date for that particular zone. For example, you may be specifying a time-of-day occurring during a DST cutover. In such a case, the ZonedDateTime class has a policy for adjusting to accommodate. Be sure to read the documentation to understand that policy and the resulting behavior.

LocalTime lt = LocalTime.of( 12 , 0 ) ; 
ZonedDateTime zdt = ZonedDateTime.of( ld , lt , z ) ;  // Time-of-day may be adjusted as needed.

java.sql.Date

无需使用 java.sql.Date .该类由 LocalDate 替换. LocalDate class表示不带日期和时区的仅日期值.

java.sql.Date

No need to use java.sql.Date. That class is replaced by LocalDate. The LocalDate class represents a date-only value without time-of-day and without time zone.

符合JDBC 4.2的JDBC驱动程序可以通过调用以下方法直接处理java.time类型:

JDBC drivers that comply with JDBC 4.2 can deal directly with java.time types by calling:

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆