【问题标题】:How to display date in exact format with month and year format [duplicate]如何使用月份和年份格式以精确格式显示日期[重复]
【发布时间】:2014-12-20 11:56:34
【问题描述】:

您好,如何使用July 3, 1969 显示给定的字符串,用于字符串1969-07-03

String a="1969-07-03";

预期输出:July 3, 1969

我最初使用此方法将其转换为正确的格式。

  textView.setText(reverseIt(a)); // but this reverse the whole string.

    public static String reverseIt(String source) {
        int i, len = source.length();
        StringBuffer dest = new StringBuffer(len);

        for (i = (len - 1); i >= 0; i--)
          dest.append(source.charAt(i));
        return dest.toString();
      }

请帮我解决这个问题。

【问题讨论】:

    标签: java android date


    【解决方案1】:

    使用SimpleDateFormathttp://developer.android.com/reference/java/text/SimpleDateFormat.html

    类似SimpleDateFormat output = new SimpleDateFormat("MMM d, yyyy", Locale.getDefault())

    您必须将该日期的字符串表示形式转换为 Date 对象,例如:

    public static Date getDate(String dateString) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
        try {
            return df.parse(dateString);
        } catch (ParseException e) {
            return null;
        }
    }
    

    然后使用String formattedDate = output.format(getDate(input))(当然要处理解析异常和潜在的空值)

    【讨论】:

      【解决方案2】:

      你为什么不做这样的事情...... 查看字符串是否为1969-07-03 并且要输出July 3, 1969

      伪代码:

      int Year = First 4 chars //First break the string into three integers.
      int month = 6-7 char
      int date = 9-10 char
      
      and then you can do something like
      
      if(month==1){
          string monthname=January}
      else if(month==2){string monthname= Feburary}....and so on....
      
      and then print "monthname date, year"
      

      【讨论】:

      • 他为什么要创建一个已经存在的功能? (SimpleDateFormat)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多