转换日期时间为"2012年4月6日"格式

11 android

我想在一天中的"第六届第七..等等." 日期字符串.

我尝试过SimpleDateFormater并尝试使用DateFormatSymbols.我没有得到String Required.

有没有解决方法?

V.J*_*.J. 23

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());
Run Code Online (Sandbox Code Playgroud)

尝试这个,这看起来像一些静态,但工作正常......


waq*_*lam 6

干得好:

/**
 * 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;
}
Run Code Online (Sandbox Code Playgroud)

样品:

Log.i("myDate", getCustomDateString(new Date()));
Run Code Online (Sandbox Code Playgroud)

2012年4月25日