• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

LocalDate Format Failure

 
Ranch Hand
Posts: 2223
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is my code but it is generating an error on third line:
SystemErr     R java.time.format.DateTimeParseException: Text '2022-04-25' could not be parsed at index 2

 
Marshal
Posts: 28263
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, yeah, if you try to parse a string like '2022-04-25' using the format 'MM/dd/uuuu' it isn't going to work. The most straightforward fix would be to use a format which matches the format of the strings you're producing.

Although I find it a bit odd that the second line is working with something which understands dates, then turns it into a string only to immediately convert the string to something which understands dates. Wouldn't converting from the date-understanding thing directly to LocalDate be more reliable?
 
Steve Dyke
Ranch Hand
Posts: 2223
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:Well, yeah, if you try to parse a string like '2022-04-25' using the format 'MM/dd/uuuu' it isn't going to work. The most straightforward fix would be to use a format which matches the format of the strings you're producing.



I see now format should be uuuu-MM-dd.

Suppose I have a LocalDate with the format 2022-04-25.
A remote data look up get a date 04/25/2025.

How do I get the remote date changed so I can compare the two dates?
 
Saloon Keeper
Posts: 10804
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you have two variables that are both LocalDate you can compare them.

LocalDate doesn't have a format per se, it just has a default string representation when asked for it but the string representation and the internal object state are not one and the same.
 
Paul Clapham
Marshal
Posts: 28263
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Steve Dyke wrote:A remote data look up get a date 04/25/2025.



Okay. But is this date value in that code a LocalDate, or an SQLDate, or some homebrew sort of date, or what?
 
Steve Dyke
Ranch Hand
Posts: 2223
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:

Steve Dyke wrote:A remote data look up get a date 04/25/2025.



Okay. But is this date value in that code a LocalDate, or an SQLDate, or some homebrew sort of date, or what?



It is pulled via sql as a String: 04/25/2024
 
Carey Brown
Saloon Keeper
Posts: 10804
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any particular reason that you're not getting it from the database as a java.sql.Date object?
 
Carey Brown
Saloon Keeper
Posts: 10804
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is it stored in the database as a DATE or some CHAR type?
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello

You can fix this by ensuring that the date string matches the pattern specified in the formatter. check below-
DateTimeFormatter fIn2 = DateTimeFormatter.ofPattern("uuuu-MM-dd");
String erpDate2 = dft.getJtToday().minusYears(2).toString();
LocalDate testLocalDate = LocalDate.parse(erpDate2, fIn2);


Thank you
 
Saloon Keeper
Posts: 15630
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The point we're trying to make is that parsing should be unnecessary.

Get the date from the database as a java.sql.Date object and then call toLocalDate().
 
Proudly marching to the beat of a different kettle of fish... while reading this tiny ad
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic