带[20]后缀的日期格式

时间:2013-11-28 05:45:03

标签: java android date date-formatting

如何将 yyyy-MM-dd hh:mm:ss 格式转换为 2013年11月20日。格式以供显示之用?

感谢。

5 个答案:

答案 0 :(得分:1)

你需要这个实用工具方法来追加thnd,我没有写它,我从某处复制到我的代码中(现在不记得了):

public class NumberUtils {
    public static String ordinal(int i) {
        String[] sufixes = new String[] { "th", "st", "nd", "rd", "th", "th",
            "th", "th", "th", "th" };
        switch (i % 100) {
        case 11:
        case 12:
        case 13:
        return i + "th";
        default:
            return i + sufixes[i % 10];
        }
    }
}

将其与其他答案相结合,以获得所需的结果。

答案 1 :(得分:0)

    long yourmilliseconds =System.currentTimeMillis();
                             SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss,SSS",Locale.US);
                             GregorianCalendar calendar = new GregorianCalendar(TimeZone.getTimeZone("US/Central"));
                             calendar.setTimeInMillis(yourmilliseconds);
                                 time1=sdf.format(calendar.getTime());

 time1 = time1.substring(0, time1.indexOf(","));

答案 2 :(得分:0)

您可以使用http://developer.android.com/reference/java/text/SimpleDateFormat.html

像这样:

SimpleDateFormat sourceFormat = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a");
SimpleDateFormat desiredFormat =new SimpleDateFormat("EEE,dd-MMM-yyyy");    
Date dt = sourceFormat.parse(your_date);
String desiredDateString = desiredFormat.format(dt)

答案 3 :(得分:0)

试试这个:

DateFormat inputFormat = new DateFormat("yyyy-MM-dd hh:mm:ss");
DateFormat outputFormat = new DateFormat("MMMM, dd yyyy");

Date date = inputFormat.parse("2013-11-28 00:52:19");
System.out.println(outputFormat.format(date));

您可以使用this document作为参考。

答案 4 :(得分:0)

你可以做类似的事情。

    SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
    Date date=df.parse("2013-11-20 12:00:00");
    df=new SimpleDateFormat("MMMM, dd'th' yyyy");
    System.out.println(df.format(date));

Out put

    November, 20th 2013
相关问题