Thursday, October 31, 2019

[Java][Example] Convert current date to "31st October,2019" format



import java.text.SimpleFateFormat;
import java.util.Date;
import java.util.Locale; 
class Main{  
    public static voild main(String[] args){
        System.out.println(getFormatDate(new Date()));  
    }   
    public static String getFormatDate(Date data){    
        SimpleDateFormat formatter = new SimpleDateFormat("dd MMMM,yyyy",Locale.ENGLISH);    
        String tempDate = formatter.format(date).toString();    
        String[] dateArr = tempDate.split(" ");    
        if(dateArr[0].equals("1")||dateArr[0].equals("21")||dateArr[0].equals("31")){
              dataArr[0] +="st";    
        }else if(dateArr[0].equals("2")||dateArr[0].equals("22")){
             dataArr[0] +="nd";    
        }else if(dateArr[0].equals("3")||dateArr[0].equals("23")){
             dataArr[0] +="rd";    
        }    
         return dataArr[0]+" "+dataArr[1];  
    }
}


Current date is Thur Oct 30 13:30:05 CST 2019  and the result should be:
30st October,2019

Current date is Fri Nov 01 10:47:08 CST 2019  and the result should be:
1st November,2019

No comments :

Post a Comment