SimpleDateFormat Demo

Test Code

package com.example.demo;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class Test {
    public static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private static final ThreadLocal<SimpleDateFormat> tl = ThreadLocal.withInitial(()->new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));

    public static void main(String[] args) throws ParseException {
        List<String> list = new ArrayList<>();
        list.add("2022-11-12 00:00:00");
        list.add("2022-12-12 01:00:00");
        list.add("2021-11-15 03:30:00");
        list.add("2012-11-12 10:00:00");
        list.add("2022-21-22 10:01:00");

        // thread not safe test
        List<Thread> threads = new ArrayList<>();
        for (String s: list) {
            Thread thread = new Thread(()->{
                try {
                    System.out.println(s);
//                    Date parseDate = tl.get().parse(s);
                    Date parseDate = sdf.parse(s);
                    System.out.println(parseDate);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            });
            threads.add(thread);
        }
        for (Thread t:threads) {
            t.start();
        }
    }
}

Source code(follow jdk1.8)

parse(): parsedDate = calb.establish(calendar).getTime();
format(): calendar.setTime(date);
DateFormat.class: protected Calendar calendar;

An immutable class is simply a class whose instances cannot be modified. All of the information contained in each instance is provided when it is created and is fixed for the lifetime of the object. So we can compare Calendar with LocalDateTime and check why calendar can be modified.