Skip to content

Java format yyyy-MM-dd'T'HH:mm:ss.SSSz to yyyy-mm-dd HH:mm:ss

To convert a date in the format yyyy-MM-dd'T'HH:mm:ss.SSSz to the format yyyy-MM-dd HH:mm:ss, you can use the SimpleDateFormat class in Java. Here's an example of how you can do it:


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

public class DateFormatExample {
    public static void main(String[] args) {
        // Parse the input string
        SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSz");
        Date date = inputFormat.parse("2022-03-14T16:53:01.000Z");

        // Format the date according to the output string
        SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String outputString = outputFormat.format(date);

        System.out.println(outputString);
    }
}

This will output the date as 2022-03-14 16:53:01. Note that MM represents the month, dd represents the day, mm represents minutes, and ss represents seconds in the format pattern. For modern Java applications (Java 8+), consider using the java.time API (DateTimeFormatter) instead of the legacy SimpleDateFormat class.

Do you find this helpful?

Dual-run preview — compare with live Symfony routes.