0

Hello, I am receiving dateTime in this format :

2016-01-21T11:03:56

I want to convert this into a more readable format like :

January-02-2021 or similar

I tried these guides but unable to do so https://androidwave.com/format-datetime-in-android/ and https://www.javatpoint.com/java-date-to-string

Also, tried this code, but no luck :

 Date date = new Date();  
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");  
String strDate = formatter.format(date);  
System.out.println("Date Format with MM/dd/yyyy : "+strDate);  

Pls Guide

bhanu
  • 1,730
  • 3
  • 12
  • 30
Sainita
  • 332
  • 1
  • 4
  • 16
  • 1
    As an aside consider throwing away the long outmoded and notoriously troublesome `SimpleDateFormat` and friends. See if you either can use [desugaring](https://developer.android.com/studio/write/java8-support-table) or add [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. Jun 02 '21 at 04:34
  • 1
    I’m immodest enough to recommend [my answer here](https://stackoverflow.com/a/55677650/5772882) for what you are trying to do. – Ole V.V. Jun 02 '21 at 04:37

1 Answers1

1

You can print the date based on the formats, the snnipt below includes 2 type of result.

try {
        //normal format of date
        SimpleDateFormat resultFormat = new SimpleDateFormat("dd-MM-yyyy");
        SimpleDateFormat monthFormat = new SimpleDateFormat("MMMM-dd-yyyy");
        SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
        String input = "2016-01-21T11:03:56";
        Date resultDate = inputFormat.parse(input);
        String normalDateResult = resultFormat.format(resultDate);
        String monthDateResult = monthFormat.format(resultDate);
        System.out.println(normalDateResult); // 21-01-2016
        System.out.println(monthDateResult); //January-21-2016

    } catch (ParseException e) {
        e.printStackTrace();
    }
Nimantha
  • 6,405
  • 6
  • 28
  • 69
MustafaKhaled
  • 1,343
  • 9
  • 25