• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

How can I count the numbers of valid dates in Java?

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was thinking of creating a method that should return the number of valid dates. How can I use SimpleDateFormat?

class ValidatorDate {
public static int ValidDates(String[] words) {
   DateFormat format = new SimpleDateFormat("dd/MM/yyyy");
   format.setLenient(false);
   }
}


public class Main {
public static void main(String[] args) {
   System.out.println(ValidatorDate.ValidDates(new String[]{"car","bench","01/04/2019",  "01/13/2019", "29/02/200s"}));
   // answer: 2(01/04/2019 and 29/02/200s are valid dates)
 }
}
 
Saloon Keeper
Posts: 14010
315
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I suggest you don't use SimpleDateFormat, but instead create an instance of DateTimeFormatter which you can then pass to a call to LocalDate.parse().

If LocalDate.parse() throws an exception, the string is not valid, so use that to determine which of your input strings are valid dates.
 
Marshal
Posts: 75866
361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Agree: I don't like SimpleDateFormat, but those Strings look like something you can apply a regular expression to. You get false or true from matches() and there is no need to bother with try‑catch.
 
Stephan van Hulst
Saloon Keeper
Posts: 14010
315
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Agree: I don't like SimpleDateFormat, but those Strings look like something you can apply a regular expression to. You get false or true from matches() and there is no need to bother with try‑catch.


What about 50/50/0000?
 
Campbell Ritchie
Marshal
Posts: 75866
361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
50th Demcespter? Always happens round here in early spring.

Good point. You can then parse that text splitting it on the /s and do further validation on it.
 
Master Rancher
Posts: 4224
57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you're eventually going to want to use a DateFormat or DateTimeFormatter of some sort, to handle information about how many days each month has, and which years are leap years.  Given that you need to use such classes eventually, and given that they do everything you need, I don't see why there would be any use for a regular expression first.  I suppose it could be a performance optimization - depending on how common is is to encounter grossly invalid dates in the dataset - but that seems very unlikely to matter much.  Better to concentrate on simple code that works.

Cris, I do see that you've identified "29/02/200s" as a valid date.  Is that a typo for "29/02/2005" maybe?  Also, do you have to worry about any other possibilities like "29/02/05", "02/29/05", "29/2/2005", "29-02-2005", "29.02.2005", "29 Feb 2005" or other things like that?  The hardest part may be getting a list of possible patterns that you need to accept.  If you try to accept too many possible patterns, it becomes impossible to determine what some dates actually are.  E.g. is "05/06/07" June 5, 2007?  Or May 6, 2007?  Or maybe May 7, 2005?  Hopefully only one of these is "obviously" right to you for the context this program is intended to operate in.  Otherwise, things can get ugly.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic