-
Date
Hello to everybody. In the following code i try to compare the date of the check that the user sets and the date of the moment, if the date that the user give is elder than this moment date the date ll insert in the database else the user must give again the date.
Code:
public void rantevou()throws IOException{
Calendar calendar = Calendar.getInstance();
System.out.println("Give the date of the check");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str, str1,str2,str3,str4,str5;
System.out.println("Give the date of the check:");
System.out.println("Give the year of the check:");
str3=br.readLine();
year=Integer.parseInt(str3);
System.out.println();
System.out.println("Give the month of the check:");
str=br.readLine();
month=Integer.parseInt(str);
System.out.println();
System.out.println("Give the day of the check:");
str=br.readLine();
day=Integer.parseInt(str);
System.out.println();
System.out.println("Give the hour of check from-to ***2 hours width****");
str4=br.readLine();
hour1=Integer.parseInt(str4);
System.out.println();
str5=br.readLine();
hour2=Integer.parseInt(str5);
System.out.println();
if(
(year>=calendar.get(Calendar.YEAR)) && (month>=calendar.get(Calendar.MONTH))
&& (day>=calendar.get(Calendar.DATE)) &&(hour1>=calendar.get(Calendar.HOUR))&&(hour1<hour2) )
{
System.out.println("The date of the check is :"+day+"/"+month+"/"+year);
System.out.println("The hour of the check is: "+hour1+"mexri tis: "+hour2);
}
else System.out.println("Give again the date of the check:");
}
My problem with this method is that when i put date like 18/2/2010 (today is 20/2/2009) the message from the compiler said "give the date again"!! How can I solve this problem???
Thank you...
-
i'm pretty sure there's a much easier way (probably several actually) to compare dates than where you're starting off: Compare 2 dates - Real's Java How-to
-
ok i ll try it.
Thank you
-
ok i find a state. When i put the date 2-12-2010 it said to me
that 2011-01-02 is after 2009-02-22. Ok how can i solve this? i made an array of months with the first element null but i can t put the array in the c1.set(year, month, day);
( Calendar c1=Calendar.getInstance();) like c1.set(year,a[month],day);
Any help?:cool: