Java常见的坑(四)—— 时间
package org.springblade.iot.controller;
import org.junit.jupiter.api.Test;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
public class TestControllerTest {
@Test
public void testDeploy() {
Date date = new Date();
System.out.println(date); //Thu Aug 19 09:12:17 CST 2021
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString = sdf.format(date);
System.out.println(dateString);
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println(localDateTime); //2021-08-19T09:12:17.681
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String localDateTimeString = dtf.format(localDateTime);
System.out.println(localDateTimeString);
/**
* 输出结果:
* Thu Aug 19 09:24:23 CST 2021
* 2021-08-19 09:24:23
* 2021-08-19T09:24:23.102
* 2021-08-19 09:24:23
*/
}
}