Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

checking date

Status
Not open for further replies.

BaoBao

Programmer
Oct 28, 2002
3
0
0
US
Hi,

I am trying to check if an input is a valid date, for example,
the user is required to enter a value "YYYY-MM-DD HR-MI-SC"

And in java program, I want to check if the user has input a valid date, but I don't know how to do it.
Can someone please give me a hint.

Thank you very much
 
Have you tried using a StringTokenizer? I think this would be a good way to do it - basically, you can specify that the "-"'s and " "'s are separators and it will "tokenize" the string into blocks, saving it very much like a Vector. This is an example of how you could do it:

import java.util.*;

public class Test
{
public static void main(String[] args)
{
StringTokenizer tokenizer;
String inputDateTime = "YYYY-MM-DD HR-MI-SC";
String[] DateTimeArray = new String[6];
int arrayIncrementor = 0;

tokenizer = new StringTokenizer (inputDateTime, "- ", false);

do {
DateTimeArray[arrayIncrementor] = tokenizer.nextToken();
arrayIncrementor++;
} while(tokenizer.hasMoreTokens());
}
}

Then just parse the individual array sectors into Ints and do your appropriate tests on each of them... napa1m
hansen@ithsite.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top