• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

problem parsing date / time

 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm trying to parse several strings into 2 dateTimes that I can then compare. When today's date is "Thursday April 09" and the time is "11:00 pm" and the 2nd time is "1:00 am" I need to add a day to the 2nd one before I compare the time difference. So my plan was: Parse the date into LocalDate, parse the time into LocalTime and then merge them into two LocalDateTime using LocalDateTime.of(date, time) and if the 2nd one is < the 1st...add a day.

So, I've got it that I can parse the time into LocalTime using:

But I'm having trouble with parsing the date:

I get a parse exception for it and I've tried several variations. It does work if I use SimpleDateFormat:


But then I have to convert it. So, why am I getting the parse exception for DateTimeFormatter? Would it be easier to use a different method for getting the results I eventually want?

What do you suggest?
 
Sheriff
Posts: 17711
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It seems like the problem is because you're missing the year portion of the date so parsing will result in an incomplete date value, which apparently the LocalDate class will not allow. You may have to write some specialized classes / enums that will add the year in for you by default.
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can parse to a MonthDay and adjustInto(LocalDate.now()) which will set the current year as default. But read the API for MonthDay: you may have to make a special case for February 29 in a leap year.

edit: or LocalDate.now().with(monthDay) which doesn't require casting the returned value.
 
Darryl Burke
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For the second form, "MMMM dd h:mm a" / "April 09 11:00 PM" you can similarly parse to a MonthDay and a LocalTime, and use LocalDateTime#with(...) to get the result you want.
 
Darryl Burke
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since you've been a member for 10 years, I guess the principle of not spoonfeeding doesn't apply here
 
Junilu Lacar
Sheriff
Posts: 17711
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, Darryl, I learned something from that

Have a cow!
 
Darryl Burke
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you, Junilu.

edit: I learned something too, namely that parsing ignores unsupported fields specified in the formatter (lines 25-26).
 
Karina Guenther
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:It seems like the problem is because you're missing the year portion of the date so parsing will result in an incomplete date value, which apparently the LocalDate class will not allow. You may have to write some specialized classes / enums that will add the year in for you by default.



I wondered if it might be that, and thought about testing it to see by grabbing the system date and appending it somehow.

I really appreciate all the varied responses because I wanted to get a variety of ideas on what might be the 'best' approach since I'm still figuring out the new stuff from Java 8 and when it's worthwhile to use it over my normal habits.
 
Karina Guenther
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh and some additional information that's not included in the java doc for time parsing: hh:mm a (for am pm) will only parse uppercase. 9:00 AM works, 9:00 am doesn't.
 
The knights of nee want a shrubbery. And a tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic