1、SimpleDateFormat
import java.text.SimpleDateFormat; import java.util.Date; public class DateFormatTest { public static void main(String[] args) { /* yyyy 年份 注意和YYYY的区别,一般都用yyyy MM 月份 dd 日 HH 时(hh是十二小时制) mm 分 ss 秒 SSS 毫秒 1000毫秒代表1秒 */ Date dte = new Date(); //Sat Dec 19 17:54:57 CST 2020 SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy/MM/dd HH:mm");//2020/12/19 05:54 SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss");//2020/12/19 05:54:57 SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");//2020-12-19 05:54:57 String str = sdf3.format(dte); } }
2、DateTimeFormatter
SimpleDateFormat是线程不安全的。
所以现在一般都推荐使用LocalDateTime它是线程安全的,并且性能更好,代码更简洁,提供了更丰富的计算方法
public class TimeTest { public static void main(String[] args) { // LocalDate:只含年月日的日期对象 // LocalTime:只含时分秒的时间对象 // LocalDateTime:同时含有年月日时分秒的日期对象 //1、LocalDate LocalDate now = LocalDate.now(); LocalDate date = LocalDate.parse("2021-05-23");//2021-05-23 //LocalDate localDate2 = LocalDate.parse("2021/05/23"); //错误:java.time.format.DateTimeParseException System.out.println("当前时间 = " + now); System.out.println("date = " + date); //输出: 当前时间 = 2021-05-23 //2、LocalTime LocalTime localTime = LocalTime.now(); System.out.println("localTime = " + localTime); //输出: localTime = 23:55:56.258 //3、LocalDateTime LocalDateTime localDateTime = LocalDateTime.now(); System.out.println("localDateTime = " + localDateTime); //输出: localDateTime = 2020-07-06T22:32:45.994 //4、获取指定日期(参数依次 年、月、日) LocalDate localDate = LocalDate.of(2021, 5, 23); System.out.println("年月日 = " + localDate); //输出: 年月日 = 2021-05-23 //5、获取指定时间(参数依次 时、分、秒、纳秒 LocalTime localTimeOf = LocalTime.of(12, 24, 12, 4444); System.out.println("localTimeOf = " + localTimeOf); //输出: localTimeOf = 12:24:12.000004444 //6、获取指定年月日,时分秒都有的日期(参数依次 年、月、日、时、分) LocalDateTime localDateTimeOf = LocalDateTime.of(2021, 7, 30, 12, 12); System.out.println("localDateTimeOf = " + localDateTimeOf); //输出: localDateTimeOf = 2021-07-30T12:12 //7、日期+时间 组成 包含年月日,时分秒都有的日期 LocalDateTime of = LocalDateTime.of(LocalDate.now(), LocalTime.now()); System.out.println("of = " + of); //of = 2021-05-24T00:02:47.093 //DateTimeFormatter DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); LocalDate fomatterLocalData = LocalDate.parse("2021-05-23",dateTimeFormatter); System.out.println("fomatterLocalData2 = " + fomatterLocalData);//fomatterLocalData2 = 2021-05-23 DateTimeFormatter dateTimeFormatter2 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); LocalDateTime fomatterLocalData2 = LocalDateTime.parse("2020-05-23 22:24:33",dateTimeFormatter2); System.out.println("fomatterLocalData2 = " + fomatterLocalData2);//fomatterLocalData2 = 2020-05-23T22:24:33
/** ---补充--- */
LocalDate localDate = LocalDate.parse("2021/07/28", DateTimeFormatter.ofPattern("yyyy/MM/dd"));
LocalDate localDate1 = LocalDate.of(2021,10,10);
System.out.println(localDate);
System.out.println(localDate1);
/** 比较时间 */
boolean before = localDate.isBefore(localDate1);//localDate是否比localDate1早,早的话返回true
/** 计算相差天数 */
Period period = Period.between(localDate, localDate1);//period的getDays()获取的不是间隔天数,而是相隔多少月的多少天
System.out.println("相差天数" +(localDate1.toEpochDay() - localDate.toEpochDay())); //74天
System.out.println(localDate.until(localDate1, ChronoUnit.DAYS)); //localDate1 - localDate = 74 时间早的被减
}
}
参考文章:LocalDateTime格式化日期