简体   繁体   中英

String to date format java

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

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 .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM