[英]how to convert string to date (HHMMMDD) (hour, month, day-of-month)
I try to convert a string into date type. 我尝试将字符串转换为日期类型。 This is the whole what I did.
这就是我所做的全部。
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HHMMMDD");
String date = "23Mar25";
System.out.println(date);
LocalDate localDate = LocalDate.parse(date, formatter);
System.out.println(localDate);
And I got the error information as 我得到了错误信息
Text '23Mar25' could not be parsed:
Unable to obtain LocalDate from TemporalAccessor:
{MonthOfYear=3, DayOfYear=25},ISO resolved to 23:00 of type
java.time.format.Parsed at
java.time.format.DateTimeFormatter.createError(DateTimeFormatter.java:1920)
Anyone can help me to solve it? 有人可以帮我解决吗?
LocalTime.parse( "23Mar25" , DateTimeFormatter.ofPattern ( "HHMMMdd", Locale.US )
.toString() // 23:00
…and… …和…
MonthDay.parse( "23Mar25" , DateTimeFormatter.ofPattern ( "HHMMMdd", Locale.US )
.toString() // --03-25
If you are certain that input indeed represents an hour-of-day, month, and day-of-month… 如果您确定输入确实代表一个小时,一个月和一个月中的某天…
DateTimeFormatter
Define a single DateTimeFormatter
. 定义一个
DateTimeFormatter
。 Be sure to specify a Locale
for the human language by which to translate the abbreviated name of the month. 确保为人类语言指定
Locale
,以翻译月份的缩写名称。
String input = "23Mar25";
DateTimeFormatter f = DateTimeFormatter.ofPattern ( "HHMMMdd", Locale.US );
LocalTime
Use that formatter twice. 两次使用该格式化程序。 Once to produce a time-of-day as a
LocalTime
object to hold the hour-of-day. 一次产生一个一天中的时间作为
LocalTime
对象以保持一天中的小时。
LocalTime lt = LocalTime.parse ( input, f );
MonthDay
Again to produce a MonthDay
object to hold the month and the day-of-month. 再次产生一个
MonthDay
对象来保存月份和月份。
MonthDay md = MonthDay.parse ( input , f );
ZoneId
To make a specific moment of that value, we must assign a year and a time zone. 要使该时刻成为特定值,我们必须指定一个年份和一个时区。 If we want to use use the current year, we need a time zone for that as well.
如果要使用当前年份,则也需要一个时区。 Keep in mind the fact that for any given moment both the date and the time-of-day vary around the globe by zone.
请记住,在任何给定时刻,日期和时间在全球范围内都会发生变化。
Specify a proper time zone name in the format of continent/region
, such as America/Montreal
, Africa/Casablanca
, or Pacific/Auckland
. 以
continent/region
的格式指定正确的时区名称 ,例如America/Montreal
, Africa/Casablanca
或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(!). 切勿使用
EST
或IST
等3-4个字母的缩写,因为它们不是真实的时区,不是标准化的,甚至不是唯一的(!)。
ZoneId z = ZoneId.of ( "America/Montreal" );
LocalDate
Determine a LocalDate
object by assigning a year to our MonthDay
object. 通过为我们的
MonthDay
对象分配年份来确定LocalDate
对象。
int yearNumber = Year.now ( z )
.getValue ( ) ;
LocalDate ld = md.atYear ( yearNumber );
ZonedDateTime
Combine with the LocalTime
to get an exact moment, a point on the timeline. 与
LocalTime
结合使用以获取准确的时刻,即时间轴上的一点。 Result is a ZonedDateTime
object. 结果是一个
ZonedDateTime
对象。
ZonedDateTime zdt = ZonedDateTime.of ( ld, lt, z );
Dump to console. 转储到控制台。
System.out.println ( "input: " + input );
System.out.println ( "lt: " + lt );
System.out.println ( "md: " + md );
System.out.println ( "ld: " + ld );
System.out.println ( "zdt: " + zdt );
input: 23Mar25
输入:25年3月23日
lt: 23:00
lt:23:00
md: --03-25
md:-03-25
ld: 2017-03-25
ld:2017-03-25
zdt: 2017-03-25T23:00-04:00[America/Montreal]
zdt:2017-03-25T23:00-04:00 [美国/蒙特利尔]
OffsetDateTime
If your input is intended for UTC , then instead of ZoneId
and ZonedDateTime
, use ZoneOffset.UTC
constant and OffsetDateTime
class in code similar to above. 如果您的输入用于UTC ,则可以使用
ZoneOffset.UTC
常量和OffsetDateTime
类代替上面的代码,而不是ZoneId
和ZonedDateTime
。
OffsetDateTime zdt = OffsetDateTime.of ( ld, lt, ZoneOffset.UTC );
Notice the output, the strings generated by the toString
methods. 注意输出,即
toString
方法生成的字符串。 The java.time classes use the ISO 8601 standard formats by default when parsing/generating strings. 解析/生成字符串时,java.time类默认使用ISO 8601标准格式。 I strongly suggest you and your data-source use these standard formats rather than invent your own such as seen in the Question.
我强烈建议您和您的数据源使用这些标准格式,而不要像在“问题”中看到的那样发明自己的格式。 The java.time classes can directly parse as well as generate such standard strings without needing to specify a formatting pattern.
java.time类可以直接解析以及生成此类标准字符串,而无需指定格式化模式。
To generate strings in other formats, use the DateTimeFormatter
class. 若要生成其他格式的字符串,请使用
DateTimeFormatter
类。 That topic is covered by many other Stack Overflow pages, so search for many examples and more discussion. 该主题在其他许多Stack Overflow页面中都有介绍,因此请搜索许多示例和更多讨论。
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HHMMMDD");
It is using a pattern for: 它使用以下模式:
HH: 2 spaces for hour
MMM: 3 spaces for month
DD: 2 spaces for day
I think you want a format like: `ddmmmyy 我认为您需要这样的格式:`ddmmmyy
dd: 2 spaces for day
mmm: 3 spaces for month
yy: 2 spaces for year
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.