日期字段解析错误 [英] Parsing error for date field
问题描述
我想将 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:
- 要向用户展示
LocalDate
,请生成一个字符串,以显示在用户界面中.使用DateTimeFormatter
自动进行本地化.要本地化,请指定:For presentation of the
LocalDate
to the user, generate a String for display in your user-interface. Use aDateTimeFormatter
to automatically localize. To localize, specify:-
FormatStyle
确定字符串应为多长或缩写. -
Locale
确定(a)日名,月名等的翻译所用的人类语言,以及(b)决定缩写,大写,标点,分隔符等问题的文化规范.
示例:
Locale l = Locale.CANADA_FRENCH ; DateTimeFormatter f = DateTimeFormatter.ofLocalizedDate( FormatStyle.MEDIUM ).withLocale( l ); String output = zdt.format( f );
You can go the other direction as well, parsing an input string to get a date.
LocalDate ld = LocalDate.parse( input , f ) ;
捕获用户输入错误或意外时引发的异常.
Trap for the exception thrown if the user’s input is faulty or unexpected.
try{ LocalDate ld = LocalDate.parse( input , f ) ; myPrepStmt.setObject( … , ld ) ; } catch ( DateTimeParseException e ) { … // Handle the error condition of faulty/unexpected input by user. }
关于java.time
java.time 框架已内置在Java 8及更高版本中.这些类取代了麻烦的旧版日期时间类,例如
Calendar
,&SimpleDateFormat
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as
java.util.Date
,Calendar
, &SimpleDateFormat
.Joda-Time 项目,现在位于 java.time 类.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
要了解更多信息,请参见 Oracle教程.并在Stack Overflow中搜索许多示例和说明.规范为 JSR 310 .
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
在哪里获取java.time类?
Where to obtain the java.time classes?
- Java SE 8 , Java SE 9 和更高版本
- 内置.
- 具有捆绑的实现的标准Java API的一部分.
- Java 9添加了一些次要功能和修复.
- 许多java.time功能都向后移植到Java 6&中.7在> ThreeTen-Backport 中./li>
- > ThreeTenABP 项目适应了<专门用于Android的em> ThreeTen-Backport (上面提到).
- 请参阅 如何使用ThreeTenABP… .
- The ThreeTenABP project adapts ThreeTen-Backport (mentioned above) for Android specifically.
- See How to use ThreeTenABP….
> ThreeTen-Extra 项目扩展了java.time与其他班级.该项目为将来可能在java.time中添加内容提供了一个试验场.您可能会在这里找到一些有用的类,例如
间隔
,年周"
,YearQuarter
和更多这篇关于日期字段解析错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
-