-1

I would appreciate if you could help me to achieve a conversion from DateTime format (08/30/2018) to this type of String "August 30, 2018". I am working with refreshes and I would like to show on a TextView something like that. Thank you

JJeset BC
  • 23
  • 3

4 Answers4

1

Use this code for conversion:

SimpleDateFormat inputFormat = new SimpleDateFormat("MM/dd/yyyy", Locale.getDefault());
SimpleDateFormat outputFormat = new SimpleDateFormat("MMMM dd, yyyy", Locale.getDefault());
Date date;
String result = "";
try {
    date = inputFormat.parse("set your input date here");
    result = outputFormat.format(date); // here will be output date
} catch (e: ParseException) {
    Log.e("Error", "Parse exception", e);
}
Jeel Vankhede
  • 11,592
  • 2
  • 28
  • 58
0

try this

 String date ="2017-05-05 13:58:50 ";
            SimpleDateFormat input = new SimpleDateFormat("MM/dd/yyyy");
            SimpleDateFormat output = new SimpleDateFormat("MMMM dd,yyyy");
            try {
                 Date oneWayTripDate = input.parse(date);  // parse input
            } catch (ParseException e) {
                e.printStackTrace();
           }
AhmetAcikalin
  • 328
  • 2
  • 7
0

This

String d = "08/30/2018";
LocalDate date = LocalDate.parse(d, DateTimeFormatter.ofPattern("MM/dd/yyyy"));
String newDate = date.format(DateTimeFormatter.ofPattern("MMMM dd, yyyy"));
System.out.println(newDate);

will print

August 30, 2018

Edit if you want to take into account lower apis, you can do this:

String d = "08/30/2018";
String newDate = "";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    LocalDate date = LocalDate.parse(d, DateTimeFormatter.ofPattern("MM/dd/yyyy"));
    newDate = date.format(DateTimeFormatter.ofPattern("MMMM dd, yyyy"));
} else {
    SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
    Date date = null;
    try {
        date = sdf.parse(d);
        sdf = new SimpleDateFormat("MMMM dd, yyyy");
        newDate = sdf.format(date);
    } catch (ParseException e) {
        e.printStackTrace();
    }
}
forpas
  • 160,666
  • 10
  • 38
  • 76
0

Use this sample:

SimpleDateFormat sdf = new SimpleDateFormat("HH:mm dd/MM/yyyy"); Date now = new Date(); String time = sdf.format(now);
Tru Nguyen
  • 11
  • 2