i want to write a class in jave which can validate any inputed date. i.e., is the inputed date valid or not... please help me
17 replies to this topic
#1
Posted 12 March 2011 - 08:53 AM
|
|
|
#2
Posted 12 March 2011 - 09:20 AM
Please give us more info, i.e. the date format. Give us some examples of valid dates.
#3
Posted 12 March 2011 - 09:25 AM
i want to use date format as dd/mm/yyyy
now i entered a date as qw/12/200a and this is not a valid date
34/13/2a01 its not a valid date
01/01/1940 is a valid date
1/1/1940 is also a valid date.
hope i given examples to understand u my requred format.
just help me this code only in core java
now i entered a date as qw/12/200a and this is not a valid date
34/13/2a01 its not a valid date
01/01/1940 is a valid date
1/1/1940 is also a valid date.
hope i given examples to understand u my requred format.
just help me this code only in core java
eafkuor said:
Please give us more info, i.e. the date format. Give us some examples of valid dates.
#4
Posted 12 March 2011 - 09:33 AM
First of all, check the string to see if there are 2 '/' characters.
After that, use their position to get the 3 substrings that are supposed to be the day, the month, and the year. Then, just use Integer.parseInt(String s) to see if they're actually numbers and if they're in the right range (non negative, less than 31, less than 12..). Of course to check wether 30 is a valid day, you first have to check the month (february has only 28 days for example).
After that, use their position to get the 3 substrings that are supposed to be the day, the month, and the year. Then, just use Integer.parseInt(String s) to see if they're actually numbers and if they're in the right range (non negative, less than 31, less than 12..). Of course to check wether 30 is a valid day, you first have to check the month (february has only 28 days for example).
#5
Posted 12 March 2011 - 10:19 AM
so far i could think like the following code. if i input a date as ....... "123/1/2001" that will show a valid date. please help more
import java.io.*;
public class date
{
public static void main(String args[]) throws IOException
{
InputStreamReader read=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(read);
String s;
int count=0;
char b;
System.out.print("Input date [dd/MM/yyyy] format only ");
s=in.readLine();
int x=s.length();
for(int y=0;y<x;y++)
{
b=(s.charAt(y));
if(b=='/')
count=count+1;
}
if(count==2)
System.out.println("Valid Date");
else
System.out.println("Invalid Date");
//int p=s.indexOf('/');
//int p2=s.indexOf('/');
//System.out.println(s+"\t"+x);
}
}
eafkuor said:
First of all, check the string to see if there are 2 '/' characters.
After that, use their position to get the 3 substrings that are supposed to be the day, the month, and the year. Then, just use Integer.parseInt(String s) to see if they're actually numbers and if they're in the right range (non negative, less than 31, less than 12..). Of course to check wether 30 is a valid day, you first have to check the month (february has only 28 days for example).
After that, use their position to get the 3 substrings that are supposed to be the day, the month, and the year. Then, just use Integer.parseInt(String s) to see if they're actually numbers and if they're in the right range (non negative, less than 31, less than 12..). Of course to check wether 30 is a valid day, you first have to check the month (february has only 28 days for example).
#6
Posted 12 March 2011 - 10:26 AM
public static void main(String[] args) {
String date1 = "qw/12/200a";
String date2 = "34/13/2a01";
String date3 = "01/01/1940";
String date4 = "1/1/1940";
validateDate(date1);
validateDate(date2);
validateDate(date3);
validateDate(date4);
}
public static void validateDate(String date) {
SimpleDateFormat sdf = new SimpleDateFormat("d/M/yyyy");
sdf.setLenient(false);
ParsePosition pos = new ParsePosition(0);
Date d = sdf.parse(date, pos);
if (d == null) {
System.out.println(date + " is no valid date.");
} else {
System.out.println(date + " is a valid date.");
}
}
Note: "dd/MM/yyyy" would not validate 1/1/1940, as 'dd' means it would have to be '01' instead of just '1'.
Ditto with M. (M is case sensitive, m=minute, M=month)
If you don't use the parseposition, but just sdf.parse(String), it will not return null as date, but instead throw a parseException which must be caught. I find this a cleaner solution, than using errorthrowing.
It's easier using java's dateformat thing, because it would also check if it's a possible day, like 31/02/2011 doesn't exist. so it will return invalid.
#7
Posted 12 March 2011 - 10:30 AM
Wow... you know every. effing. class :D
#8
Posted 12 March 2011 - 10:32 AM
Yes ^^
I knew the SimpleDateFormat class existed, was hoping it had an isValid() method or so... then looked at the javadoc, and since parse with the parseposition doesn't throws errors that was good enough for me :)
I knew the SimpleDateFormat class existed, was hoping it had an isValid() method or so... then looked at the javadoc, and since parse with the parseposition doesn't throws errors that was good enough for me :)
#9
Posted 12 March 2011 - 10:36 AM
SimpleDateFormat sdf = new SimpleDateFormat("d/M/yyyy");
error msg : cannot find symbol - class SimpleDateFormatwhat is the reason?
#10
Posted 12 March 2011 - 10:38 AM
Propably import issues
Put these on top of your file. Outisde the class body:
Put these on top of your file. Outisde the class body:
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;
public class SomeClass(){
...
}
#11
Posted 12 March 2011 - 10:52 AM
with the help of wim DC's post i could generate the following code
import java.io.*;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;
public class date2{
public static void main(String[] args) throws IOException
{
// String date1 = "qw/12/200a";
// String date2 = "34/13/2a01";
// String date3 = "01/01/1940";
// String date4 = "1/1/1940";
InputStreamReader read=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(read);
String s;
System.out.print("Input date [dd/MM/yyyy] format only ");
s=in.readLine();
validateDate(s);
// validateDate(date1);
// validateDate(date2);
// validateDate(date3);
// validateDate(date4);
}
public static void validateDate(String date) {
SimpleDateFormat sdf = new SimpleDateFormat("d/M/yyyy");
sdf.setLenient(false);
ParsePosition pos = new ParsePosition(0);
Date d = sdf.parse(date, pos);
if (d == null) {
System.out.println(date + " is no valid date.");
} else {
System.out.println(date + " is a valid date.");
}
}
}
now i want to calculate number of days of a specific date... suppose the date is 3/1/2011 (i.e., 3rd January, 2011)... the calculated date is 3rd day of 2011. if the date is 3/2/2011 .. the calculated date is 31+3=34th day of 2011. please help me
#12
Posted 12 March 2011 - 11:01 AM
import java.util.Calendar; ... ... Calendar cal = Calendar.getInstance(); cal.setTime(d); System.out.println(cal.get(Calendar.DAY_OF_YEAR) + "th day of year.");
Often when working with Date you'll be better of with the Calendar class. Cause the Date class.. well it sucks :D
And Java knows it, a lot is depracated, but many methods like the parsing still return Date, Just to be backwards compatible
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









