Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to format the current date with Suffix to Superscript?

Tags:

java

android

I am using the SimpleDateFormatter

 public static final DateFormat DATE_FORMAT_FULL_FULL_SPACES = 
     new SimpleDateFormat("dd MMMM yyyy", Locale.getDefault());

and Current Date is passed at that time, It should display as 1st JULY 2014 where st should be superscript.

How can I go further ?

like image 417
TexGeek Avatar asked Oct 13 '14 10:10

TexGeek


1 Answers

Create these methods

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

    private String getDayNumberSuffix(int day) {
        if (day >= 11 && day <= 13) {
            return "<sup>th</sup>";
        }
        switch (day % 10) {
            case 1:
                return "<sup>st</sup>";
            case 2:
                return "<sup>nd</sup>";
            case 3:
                return "<sup>rd</sup>";
            default:
                return "<sup>th</sup>";
        }
    }

How to call?

String str = getFormatedDate();
        txtDate.setText(Html.fromHtml(str));

OutPut :

enter image description here

like image 114
Biraj Zalavadia Avatar answered Sep 18 '22 20:09

Biraj Zalavadia