2012-04-25 16 views
9

Voglio ottenere il giorno in "6 ° 7 .. ecc." della stringa Data.Converti ora in data "6 aprile 2012" formato

Ho provato SimpleDateFormater & anche provare con DateFormatSymbols. Non ricevo String Required.

C'è qualche soluzione?

+2

Crea il tuo metodo di – Lucifer

+0

il ' 'st' SimpleDateForter utilizzando Format(), 'nd', 'th'' parte sarà raggiunto con una certa logica, perché non v'è alcuna modificatore di data che lo gestisce automaticamente – waqaslam

+0

prova come risposta data a http://stackoverflow.com/questions/4011075/how-do-you-format-the-day-of-the-month-to-say-11th-21st- o-23rd-in-java – Khan

risposta

18
SimpleDateFormat format = new SimpleDateFormat("d"); 
String date = format.format(new Date()); 

if(date.endsWith("1") && !date.endsWith("11")) 
    format = new SimpleDateFormat("EE MMM d'st', yyyy"); 
else if(date.endsWith("2") && !date.endsWith("12")) 
    format = new SimpleDateFormat("EE MMM d'nd', yyyy"); 
else if(date.endsWith("3") && !date.endsWith("13")) 
    format = new SimpleDateFormat("EE MMM d'rd', yyyy"); 
else 
    format = new SimpleDateFormat("EE MMM d'th', yyyy"); 

String yourDate = format.format(new Date()); 

Prova questo, questo appare come un po 'statico, ma funziona bene ...

+0

heh, che dire del 12 °? o 13? –

+0

finisce il 2 e 3, quindi è valida –

+0

dovrebbe essere 12 e il 13 è il mio punto –

1

Si potrebbe sottoclasse SimpleDateFormat e sovrascrivere formato, e utilizzare una semplice funzione di utilità che prende in un valore intero o stringa e restituisce una stringa con uno "nd" o "st" allegata ... qualcosa di simile:

if (initialDate.equals("2") || initialDate.equals("22"){ 
    return initialDate += "nd"; 
}else if {initialDate.equals("3") || initialDate.equals("23"){ 
    return initialDate += "rd"; 
}else{ 
    return initialDate += "th"; 
} 
+0

3 °? Potrebbe voler risolvere il problema :) – dymmeh

+0

e la parte 'th'? :) – waqaslam

+0

1 °, 2 °, 3 °, 4 ° .. 5 ° .. 6 ° .. 7 ° .. 8 ° .. 9 ° .. 10 ° .. serve anche questo .. @Waqas "th" infatti! :) – dymmeh

5

Qui si va:

/** 
* Converts Date object into string format as for e.g. <b>April 25th, 2012</b> 
* @param date date object 
* @return string format of provided date object 
*/ 
public static String getCustomDateString(Date date){ 
    SimpleDateFormat tmp = new SimpleDateFormat("MMMM d"); 

    String str = tmp.format(date); 
    str = str.substring(0, 1).toUpperCase() + str.substring(1); 

    if(date.getDate()>10 && date.getDate()<14) 
     str = str + "th, "; 
    else{ 
     if(str.endsWith("1")) str = str + "st, "; 
     else if(str.endsWith("2")) str = str + "nd, "; 
     else if(str.endsWith("3")) str = str + "rd, "; 
     else str = str + "th, "; 
    } 

    tmp = new SimpleDateFormat("yyyy"); 
    str = str + tmp.format(date); 

    return str; 
} 

Esempio:

Log.i("myDate", getCustomDateString(new Date())); 

25 aprile 2012

1

Il seguente metodo può essere utilizzato per ottenere la stringa formattata della data che viene passato ad esso. Formatterà la data per dire 1st, 2nd, 3rd, 4th .. usando SimpleDateFormat in Java. ad esempio: - 1 set 2015

public String getFormattedDate(Date date){ 
     Calendar cal=Calendar.getInstance(); 
     cal.setTime(date); 
     //2nd of march 2015 
     int day=cal.get(Calendar.DATE); 

     switch (day % 10) { 
     case 1: 
      return new SimpleDateFormat("MMMM d'st', yyyy").format(date); 
     case 2: 
      return new SimpleDateFormat("MMMM d'nd', yyyy").format(date); 
     case 3: 
      return new SimpleDateFormat("MMMM d'rd', yyyy").format(date); 
     default: 
      return new SimpleDateFormat("MMMM d'th', yyyy").format(date); 
    }