如何以月份和年份格式的确切格式显示日期

时间:2014-10-24 15:41:23

标签: java android date

您好如何使用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();
      }

请帮我解决这个问题。

2 个答案:

答案 0 :(得分: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))(但当然要处理解析异常和潜在的空值)

答案 1 :(得分:0)

你为什么不做这样的事...... 查看字符串是否为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"