0

First i have a java Date object lets say

Date firstdate;            // its initialized and equal to some date dont worry

with using this code i convert it to dd/MM/yyyy format

SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
                date.setText(sdf.format(firstdate));

But since sdf.format is returning to a string,after formating 1 time i cant format it again to my desired Monthname day,Year format before displaying to user.

i mean if sdf.format(firstdate) returns "01/01/2000" string,i would like to convert this to January 1,2000 and then display it to user.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
dongerpep
  • 109
  • 1
  • 4
  • 11
  • Why do you use "dd/MM/yyyy" is that's not the format you need? Anyway, you can turn a string back to date with parse() method. See: https://docs.oracle.com/javase/7/docs/api/java/text/DateFormat.html#parse(java.lang.String) – Yoav Gur Apr 07 '18 at 19:52
  • 1
    Consider throwing away the long outmoded and notoriously troublesome `SimpleDateFormat` and friends, and adding [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) to your Android project in order to use `java.time`, the modern Java date and time API. It is so much nicer to work with. – Ole V.V. Apr 07 '18 at 19:59
  • You need a completely different format object if you expect `format()` method to produce anything different – OneCricketeer Apr 07 '18 at 20:11
  • I recommend `DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG).withLocale(Locale.US);` since your requested format agrees with this. Do take advantage. And use `LocalDate` for your date. How to use on Android? See [How to use ThreeTenABP in Android Project](https://stackoverflow.com/questions/38922754/how-to-use-threetenabp-in-android-project) – Ole V.V. Apr 07 '18 at 20:16

1 Answers1

1

First get the String and convert to a Date:

String s = date.getText();  // e.g. "01/01/2000"
Date d = sdf.parse(s);      // parse the String to get a Date object

Then you need a second SimpleDateFormat to convert the Date to the desired String:

SimpleDateFormat sdf2 = new SimpleDateFormat("MMMMM dd, yyyy");
s = sdf2.format(d);         // "January 1, 2000"
Thomas Fritsch
  • 9,639
  • 33
  • 37
  • 49