Jump to content

Check date format

- - - - -

  • Please log in to reply
9 replies to this topic

#1
toto_7

toto_7

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 295 posts
Hello,

I need to check a String if has the correct date format (DD/MM/YYYY).

I have created
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");

but now i dont know how to compare my String with dateFormat to find if is valid. Also i have to check if DD is from 01-31 MM is from 01-12

"Programming is like sex. One mistake and you have to support it for the rest of your life."

-Michael Sinz

#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Check http://forum.codecal...ating-date.html

#3
toto_7

toto_7

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 295 posts
WoW thank you :) there is any way to fix the digits problem?

I want only valid input be DD/MM/YYYY
with this way D/M/YY is also valid

"Programming is like sex. One mistake and you have to support it for the rest of your life."

-Michael Sinz

#4
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
new SimpleDateFormat("dd/MM/yyyy");
Should do that

#5
toto_7

toto_7

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 295 posts
That's what i have :) and take it as valid

"Programming is like sex. One mistake and you have to support it for the rest of your life."

-Michael Sinz

#6
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Just check if the length is 10? :)

#7
ch3etah

ch3etah

    Newbie

  • Members
  • Pip
  • 8 posts

wim DC said:

Just check if the length is 10? :)

It will still validate 01/10/5678. What if you want to check for a given year, like a DOB?

#8
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas
01/10/5678 is a valid date (albeit 3667 years away). The only way you can check a DOB year for validity is compare the year component to this year and see if it is less than the current year.
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.

– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#9
toto_7

toto_7

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 295 posts
Hello Greg :) .. yes you are right. But first, my exercise was asking just check the structure, second come on, we are creating a program for civilization that will be living after 3667 years :P and finally, exams GONE !!! :) but ok useful for other members :)

"Programming is like sex. One mistake and you have to support it for the rest of your life."

-Michael Sinz

#10
ch3etah

ch3etah

    Newbie

  • Members
  • Pip
  • 8 posts
You can do something like this:
int year = Integer.parseInt(date.substring(6, 10));

now you can take the year and verify it against the desired DOB

ps. Hope your exam was OK.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users