0

I have this String date="2021-04-25T18:54:18" and i should to format like that: HH:mm ,dd mmm yyyy

I tried this

String date="2021-04-25T18:54:18";
 Date format= null;
    try {
        format = new SimpleDateFormat("HH:mm, yyyy-MM-dd'T", Locale.ENGLISH).parse(date);
        holder.tvDate.setText(format.toString());

    } catch (ParseException e) {
        e.printStackTrace();
    }

But does not work

Alex Gedo
  • 45
  • 5
  • 1
    Are you using a pattern that matches your desired output for parsing input of a differnent format? I think you need 2 different ones... – deHaar Apr 25 '21 at 19:33
  • *"i should to format like that: HH:mm ,dd mmm yyyy"* but then you give format "HH:mm, yyyy-MM-dd'T". Note that **`HH:mm ,dd mmm yyyy` != `HH:mm, yyyy-MM-dd'T`**. Note especially that the second is bad because it has that `'T` at the end, with an unmatched apostrophe. – Andreas Apr 25 '21 at 20:08
  • I recommend you don’t use `SimpleDateFormat` and `Date`. Those classes are poorly designed and long outdated, the former in particular notoriously troublesome. Instead use `LocalDateTime` and `DateTimeFormatter`, both from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Apr 26 '21 at 05:08
  • For your future questions please know that for some of us a plain *But does not work* is our favourite aversion in Stack Overflow questions. You should paste any error message and/or stack trace into your question, or any wrong output you are getting. This will give us a lot more precision, make it a lot easier to help you and spare you of some downvotes. – Ole V.V. Apr 26 '21 at 05:12

2 Answers2

5

The legacy date-time API (java.util date-time types and their formatting API, SimpleDateFormat) are outdated and error-prone. It is recommended to stop using them completely and switch to java.time, the modern date-time API* .

Using modern date-time API:

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        String strDateTime = "2021-04-25T18:54:18";
        LocalDateTime ldt = LocalDateTime.parse(strDateTime);

        DateTimeFormatter dtfOutput = DateTimeFormatter.ofPattern("HH:mm ,dd MMM yyyy", Locale.ENGLISH);
        String output = dtfOutput.format(ldt);
        System.out.println(output);
    }
}

Output:

18:54 ,25 Apr 2021

Learn more about the modern date-time API from Trail: Date Time.

Using the legacy API:

You need two formatters: one for input pattern and one for output pattern. You didn't need two formatters in the case of the modern API because the modern API is based on ISO 8601 and your date-time string is already in this format.

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class Main {
    public static void main(String[] args) throws ParseException {
        String strDateTime = "2021-04-25T18:54:18";
        SimpleDateFormat sdfInput = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.ENGLISH);
        Date date = sdfInput.parse(strDateTime);

        SimpleDateFormat sdfOutput = new SimpleDateFormat("HH:mm ,dd MMM yyyy", Locale.ENGLISH);
        String output = sdfOutput.format(date);
        System.out.println(output);
    }
}

Output:

18:54 ,25 Apr 2021

* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
0

You are missing 1 step. SimpleDateFormat can only parse dates in the format you specify. You are trying to parse a "yyyy-MM-dd ..." based string into the "HH:mm ..." date. This will not work.

First convert your "yyyy-MM-dd" date string into a Date. Then, format that Date into the String you need

String input = "2021-04-25T18:54:18";
Date date = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.ENGLISH).parse(input);
String output = new SimpleDateFormat("HH:mm, yyyy-MM-dd", Locale.ENGLISH).format(date);
Entreco
  • 12,738
  • 8
  • 75
  • 95
  • 2
    `SimpleDateFormat` is part of the terrible date-time classes originally bundled with the earliest versions of Java. These were years ago supplanted by the *java.time* classes defined in JSR 310. Suggesting these legacy classes in 2021 is poor advice. See modern solution in the [Answer by Avinash](https://stackoverflow.com/a/67257530/642706). – Basil Bourque Apr 25 '21 at 20:49
  • Agreed. Alltough i prefer to explain the concept in terms of the original question, instead of making the assumption it's easy to switch to the new apis. – Entreco Apr 25 '21 at 21:21
  • 2
    Actually it *is* easy to mix in use of *java.time* within an existing app. No need to “switch”. The classes are built into Java 8 and later, and Android 26 and later. For earlier Android, most of the functionality is available in the latest tooling via “API desugaring”. For the now end-of-life’d Java 6 & 7, the *ThreeTen-Backport* library can be added to a project. – Basil Bourque Apr 25 '21 at 23:02