How to format the current date with Suffix to Superscript?

后端 未结 2 447
一生所求
一生所求 2021-02-08 23:47

I am using the SimpleDateFormatter

 public static final DateFormat DATE_FORMAT_FULL_FULL_SPACES = 
     new SimpleDateFormat(\"dd MMMM yyyy\", Local         


        
2条回答
  •  一整个雨季
    2021-02-09 00:21

    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 "th";
            }
            switch (day % 10) {
                case 1:
                    return "st";
                case 2:
                    return "nd";
                case 3:
                    return "rd";
                default:
                    return "th";
            }
        }
    

    How to call?

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

    OutPut :

    enter image description here

提交回复
热议问题