how to validate the pattern of date format
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   zhebin_cong
Posted On:   Monday, December 7, 2009 12:45 AM


hello
i want to validate if the pattern of date format string is valid? such as "yyyy-MM-dd" is valid, but "oooo-oo-uu" is not valid, i know that i can valid it by constructor of SimpleDateFormat, something like:
new SimpleDateFormat( ); if it is invalid, the exception will throw. but i think it looks not a good practise, i wonder if there are other ways?

thanks

Re: how to validate the pattern of date format

Posted By:   Anonymous  
Posted On:   Friday, February 5, 2010 05:08 PM

i am just created the validation of date with a program, you must read this.


/**
* @(#)Date.java
*
*
* @author abansari
* @version 1.00 2010/2/4
* Date Format: 12/01/10
*/

import java.util.*;
public class Date
{
public static void main (String[] args)
{
String[] Months = {"a","Jan","Feb","Mar","Apr","May","June","July","Aug","Sept",
"Oct","Nov","Dec"};
String myDate = "01/01/10";
int token, month,day;
int len = myDate.length();
String[] temp;

// check

temp = myDate.split("/");
month = Integer.parseInt(temp[1]);
// day = temp[0];
token = temp.length;

if(len<8 || len>8 || token!=3)
{
System.out.println ("Invalid Format:");
System.out.println ("DD//MM//YY");
}
// else
{
System.out.println (temp[0]+"st " + Months[month] + " " + temp[2]);
}




}
}
About | Sitemap | Contact