Win a copy of Pro Spring MVC with WebFlux: Web Development in Spring Framework 5 and Spring Boot 2 this week in the Spring forum!
  • 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
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

Way to find a correct date.

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have been trying to figure out what is wrong with this code for quite some time. The point is to input a Date string lets say "5/10/2020". The code lets everything through the method that is 1/1, 1/12, 10/1, 10/11. But for instance, typing in a 0 before a day or month like 09, 012, 02 should throw a run time exception error. I got most of it to work except for one date which is "1/10" or having a one digit month and the day as 10, 20, or 30.

My code is here,



should I take,


and replace that with

I have tried that and it will not seem to work.
 
Sheriff
Posts: 16242
271
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
You're over complicating this with matches(). I would just use String.startsWith() -- simple and straightforward.
 
Steven Villarreal
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I visited the Java API and understood the startsWith method which is exactly what I needed. I just made two ifs, one with that method

and the other with the startsWith method and with

which looked like

Thanks!
 
Junilu Lacar
Sheriff
Posts: 16242
271
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
You could have also used String.split() to make your code more readable.


Sure, it's a few lines longer that what you wrote but nobody has to spend a lot of time deciphering an expression like geoDate.substring(3, 4) (yuck) .
 
Bartender
Posts: 10777
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Steven Villarreal wrote:The point is to input a Date string lets say "5/10/2020". The code lets everything through the method that is 1/1, 1/12, 10/1, 10/11. But for instance, typing in a 0 before a day or month like 09, 012, 02 should throw a run time exception error.


Why? Seems perfectly reasonable to me (well, maybe not "012"), and the way many dates are actually written.

However, my advice would be to forget "rolling your own", and use the parser provided by SimpleDateFormat - ie, parse(String).

You should read up on it carefully first because there's quite a lot to know; but it's a far more generic solution and you don't have to write a single line of code.

The class even allows you to set its "strictness" (see setLenient(boolean)).

Winston
 
We're being followed by intergalactic spies! Quick! Take this tiny ad!
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic