I want to validate the code of Date.
Input come from Textbox where user enters it. and in code it will get calender's date instance and match it.
I want to put character in that SimpleDateFormat.
Code :
SimpleDateFormat formatter = new SimpleDateFormat("dd'ch' MMM, yyyy");
System.out.println("Date is :: " + formatter.format(Calendar.getInstance().getTime()));
String input = "20th Mar, 2014";
if(input.equals(formatter.format(Calendar.getInstance().getTime()))){
System.out.println("Matched");
}else{
System.out.println("Not Matched");
}
I want to put th, rd, st
on place of ch in SDF.
means it will take input from user so it can be any date so I want some mechanism so only three will be placed at there.
Anyone knows that how can I do this ? Help..
UPDATE
SimpleDateFormat formatterth = new SimpleDateFormat("dd'th' MMM, yyyy");
SimpleDateFormat formatterrd = new SimpleDateFormat("dd'rd' MMM, yyyy");
SimpleDateFormat formatternd = new SimpleDateFormat("dd'nd' MMM, yyyy");
SimpleDateFormat formatterst = new SimpleDateFormat("dd'st' MMM, yyyy");
String input = "20th Mar, 2014";
String input1 = "23rd Mar, 2014";
try {
if(input.equals(formatterth.parse(input1)) || input.equals(formatterrd.parse(input1)) || input.equals(formatternd.parse(input1)) || input.equals(formatterst.parse(input1))){
System.out.println("Matched");
}else{
System.out.println("Not Matched");
}
} catch (ParseException e) {
e.printStackTrace();
}
You can't, basically. You need three separate SimpleDateFormat
objects:
new SimpleDateFormat("dd'st' MMM, yyyy")
new SimpleDateFormat("dd'nd' MMM, yyyy")
new SimpleDateFormat("dd'th' MMM, yyyy")
... then try parsing with each of them. Note that even this only works with ordinals in English... and it will parse "20st Mar, 2014" which possibly it shouldn't.
Ordinals in date formats are fundamentally a pain, and I haven't personally seen any API which deals with them nicely - partly because they're a pain in localization in general.
See more on this question at Stackoverflow