Java时间格式转换


    SimpleDateFormat sim1 = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
    SimpleDateFormat sim2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    // 时间格式1:
    Object object = "Wed Sep 22 15:15:38 CST 2021";

    Date parse = sim1.parse(String.valueOf(object));
    System.out.println("parse = " + parse);
    String format = sim2.format(parse);
    System.out.println("format = " + format);


// 打印结果:
parse = Wed Sep 22 15:15:38 CST 2021
format = 2021-09-22 15:15:38

    // 时间格式2:
    Object object = "2021-08-13 11:11:11";

    Date parse = sim1.parse(String.valueOf(object));
    System.out.println("parse = " + parse);
    String format = sim2.format(parse);
    System.out.println("format = " + format);

// 打印结果:
parse = Fri Aug 13 11:11:11 CST 2021
format = 2021-08-13 11:11:11

    // 时间格式3:

    import org.joda.time.DateTime;

    Object object = "2021-08-13T11:11:11+08:00";

    Date date = new DateTime(String.valueOf(object)).toDate();
    System.out.println("date = " + date);
    String format =  sim2.format(date);
    System.out.println("format = " + format);

// 打印结果:
date = Fri Aug 13 11:11:11 CST 2021
format = 2021-08-13 11:11:11