Reputation: 1
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?
Upvotes: 0
Views: 183
Reputation: 339512
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
. Be sure to specify a Locale
for the human language by which to translate the abbreviated name of the month.
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 lt = LocalTime.parse ( input, f );
MonthDay
Again to produce a MonthDay
object to hold the month and the day-of-month.
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
. 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" );
LocalDate
Determine a LocalDate
object by assigning a year to our MonthDay
object.
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. Result is a ZonedDateTime
object.
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
lt: 23:00
md: --03-25
ld: 2017-03-25
zdt: 2017-03-25T23:00-04:00[America/Montreal]
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.
OffsetDateTime zdt = OffsetDateTime.of ( ld, lt, ZoneOffset.UTC );
Notice the output, the strings generated by the toString
methods. The java.time classes use the ISO 8601 standard formats by default when parsing/generating strings. 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.
To generate strings in other formats, use the DateTimeFormatter
class. That topic is covered by many other Stack Overflow pages, so search for many examples and more discussion.
Upvotes: 3
Reputation: 11153
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
dd: 2 spaces for day
mmm: 3 spaces for month
yy: 2 spaces for year
Upvotes: 0