Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

problem parsing date / time

 
Karina Guenther
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • 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?
 
Junilu Lacar
Sheriff
Posts: 10077
120
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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.
 
Darryl Burke
Bartender
Posts: 5155
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • 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: 5155
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • 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: 5155
11
Java Netbeans IDE Opera
  • Likes 2
  • Mark post as helpful
  • send pies
  • 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: 10077
120
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Darryl, I learned something from that

Have a cow!
 
Darryl Burke
Bartender
Posts: 5155
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • 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: 65
  • Mark post as helpful
  • send pies
  • 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: 65
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!