d3m0li5h3r
d3m0li5h3r

Reputation: 1967

Date format in Java (or Android)

I have date in dd/mm/yyyy format but i want to parse it in something like 2nd May or 5th June I'm able to parse it to 2 May or 5 June but I need to append that nd or th with date too Can anyone please suggest something using DateFormat or SimpleDateFormat class?

Edit: A little snapshot of what I've already tried:-

Date d = Date.parse("20/6/2013");
SimpleDateFormat sdf = new SimpleDateFormat("dd MMM");
String dateString = sdf.format(d);

Upvotes: 2

Views: 1585

Answers (4)

Sai Kiran
Sai Kiran

Reputation: 465

You can use some method like this :-

String getDaySuffix(final int n) {
    if(n < 1 || n > 31)
        return "Invalid date";
    if (n >= 11 && n <= 13) 
        return "th";

    switch (n % 10) {
        case 1:  return "st";
        case 2:  return "nd";
        case 3:  return "rd";
        default: return "th";
    }
}

Upvotes: 8

Chandu
Chandu

Reputation: 1

There is no inbuilt function to get the date format like 1st or 5th .. we have to manually add the suffix to a date..Hope the below code might be usefull for you.

public class WorkWithDate {

private static String getCurrentDateInSpecificFormat(Calendar currentCalDate) {
    String dayNumberSuffix = getDayNumberSuffix(currentCalDate.get(Calendar.DAY_OF_MONTH));
    DateFormat dateFormat = new SimpleDateFormat(" d'" + dayNumberSuffix + "' MMMM yyyy");
    return dateFormat.format(currentCalDate.getTime());
}

private static String getDayNumberSuffix(int day) {
    if (day >= 11 && day <= 13) {
        return "th";
    }
    switch (day % 10) {
        case 1:
            return "st";
        case 2:
            return "nd";
        case 3:
            return "rd";
        default:
            return "th";
    }
}
public static void main(String [] args) throws ParseException {
    System.out.println(getCurrentDateInSpecificFormat(Calendar.getInstance()));
}

}

Upvotes: 0

Arpit Patel
Arpit Patel

Reputation: 1571

you need to split it with the " ". like below:

Staring split[] = dateString .split[" "];
String date = split[0];
String suffix = getDate(Integer.parseInt(date));
String YourDesireString = date + suffix + " " + split[1];

The function of getDate is as below

String getDate(final int n) {
    if(n <= 1 || n >= 31)
        return "Invalid date";
    if (n >= 11 && n <= 13) {
        return "th";
    }
    switch (n % 10) {
        case 1:  return "st";
        case 2:  return "nd";
        case 3:  return "rd";
        default: return "th";
   }
}

YourDesireString is the answer what you want is. good luck

Upvotes: 1

vinay kumar
vinay kumar

Reputation: 1449

I don't think it can be done by SimpleDateFormat. But here is the alternate solution to achieve the same.

static String[] suffixes =
  //    0     1     2     3     4     5     6     7     8     9
     { "th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th",
  //    10    11    12    13    14    15    16    17    18    19
       "th", "th", "th", "th", "th", "th", "th", "th", "th", "th",
  //    20    21    22    23    24    25    26    27    28    29
       "th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th",
  //    30    31
       "th", "st" };

  Date date = new Date();
 int day = Calendar.getInstance().setTime(date).get(Calendar.DAY_OF_MONTH);
 String dayStr = day + suffixes[day];

Upvotes: 0

Related Questions