• Post Reply Bookmark Topic Watch Topic
  • New Topic

Validation check for the day using util.  RSS feed

 
sarma kiran
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am using the following code to check the validation for the date which is entered is saturday or sunday. its not woring. Please check it.

String dateText = bookingDateText.getText();
DateFormat myDateFormat = new SimpleDateFormat("dd-mm-yyyy");
try {
int day = myDateFormat.parse(dateText).getDay();
if(day==0 || day==6){
JOptionPane.showMessageDialog(null, "Bank Holiday");
}
} catch (ParseException ex) {
System.out.println("Invalid Date Parser Exception ");
ex.printStackTrace();
}}


Can any one tell me whats wrong with this code...

Thanks in advance.

Regards,
Kiran
 
Gamini Sirisena
Ranch Hand
Posts: 378
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just a guess..
If you are not getting the correct day of week it's probably due to a problem with the date format you has specified.

i.e. the pattern for a two digit month is MM and not mm

Apart from that Date.getDay has been deprecated since jdk 1.1.

Deprecated. As of JDK version 1.1, replaced by Calendar.get(Calendar.DAY_OF_WEEK).


I believe this done as follows.


[ September 15, 2008: Message edited by: Gamini Sirisena ]
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gamini has also shown you another flaw in your code. mm is used for minutes, not months. Use MM for those.

Also, Calendar.get(DAY_OF_WEEK) is not 0 based but Calendar.SUNDAY based. Calendar.SUNDAY is not 0.
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rob Prime:
Also, Calendar.get(DAY_OF_WEEK) is not 0 based but Calendar.SUNDAY based. Calendar.SUNDAY is not 0.


The leading cause of suicide among Java developers in the pre-Joda Time era.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!