user3145373 ツ
user3145373 ツ

Reputation: 8146

Put character in SimpleDateFormat

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();
            }

Upvotes: 3

Views: 262

Answers (2)

Akash Thakare
Akash Thakare

Reputation: 22972

You can not do that with one Simple date format but you have to get Day from date check what kind of format you want to use.

Calendar cal = Calendar.getInstance();
int dayOfMonth = cal.get(Calendar.DAY_OF_MONTH);

SimpleDateFormat formatter;
if(dayOfMonth==1||dayOfMonth==21||dayOfMonth==31)
formatter = new SimpleDateFormat("dd'st'MMM, yyyy");

if dayOfMonth like 1,21,31 will use "dd'st' MMM YYYY" and so on.

One other way is try this...

String str="'th'";
String s="dd"+str+"MMM, yyyy";
SimpleDateFormat formatter = new SimpleDateFormat(str);

You can change str="'st'" and str="'rd'"

One more way come to my mind is By the use of method you just need ONE SimpleDate format

public String Method(String str)//pass"''th","'st'" or "'nd'"
{
 SimpleDateFormat formatter = new SimpleDateFormat("dd"+str+"MMM, yyyy");
return formatter.format(Calendar.getInstance().getTime()).toString();
}

Try this method(ANS for your UPDATED Question):

public boolean CheckForST(String yourdate)

{
    try {
        SimpleDateFormat formatter2 = new SimpleDateFormat("dd'st' MMM, yyyy");
        formatter2.parseObject(yourdate);

        return true;
    } catch (ParseException e) {

        return false;

    }
}

Which is only implemented for 'st' you can do the same for 'rd' or 'nd'

Upvotes: 2

Jon Skeet
Jon Skeet

Reputation: 1501013

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.

Upvotes: 3

Related Questions