【问题标题】:Date format with [20th] suffix带 [20th] 后缀的日期格式
【发布时间】:2013-11-28 05:45:03
【问题描述】:

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

谢谢。

【问题讨论】:

  • 我发现的是补丁。像 th,ed 等数组,我必须追加。他们按日期。
  • 发布您迄今为止尝试过的代码。

标签: java android date date-formatting


【解决方案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];
        }
    }
}

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

【讨论】:

    【解决方案2】:
        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(","));
    

    【讨论】:

      【解决方案3】:

      您可以使用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)
      

      【讨论】:

        【解决方案4】:

        试试这个:

        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作为参考。

        【讨论】:

          【解决方案5】:

          你可以做类似的事情。

              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));
          

          输出

              November, 20th 2013
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2014-10-26
            • 2022-07-13
            • 2011-01-30
            • 1970-01-01
            • 2017-01-07
            相关资源
            最近更新 更多