-1

Hi how to display given string with July 3, 1969 for string 1969-07-03

String a="1969-07-03";

Expected output: July 3, 1969

I used this method initially to convert it to the right format.

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

Please help me solve this.

Devin Snyder
  • 142
  • 2
  • 10
Splash
  • 11
  • 4

2 Answers2

1

Use SimpleDateFormat: http://developer.android.com/reference/java/text/SimpleDateFormat.html

Something like SimpleDateFormat output = new SimpleDateFormat("MMM d, yyyy", Locale.getDefault())

You'll have to convert that string representation of a date to a Date object like:

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

and then use String formattedDate = output.format(getDate(input)) (but of course handle the parse exception and potential null value)

darnmason
  • 2,672
  • 1
  • 17
  • 23
0

Why don't you do something like this... See if the string is 1969-07-03 and you want output July 3, 1969

Pseudocode:

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"
Daksh Shah
  • 2,997
  • 6
  • 37
  • 71
  • 2
    And why should he create a functionality that already exists? (`SimpleDateFormat`) – Tom Oct 24 '14 at 16:30