JDK8之前
日期与时间戳之间的转换
public class Test {public static void main(String[] args) {Date date = new Date();System.out.println("date = " + date); // date = Sun Sep 26 14:48:52 CST 2021Date date1 = new Date(1632638970000L);System.out.println("date1 = " + date1); // date1 = Sun Sep 26 14:49:30 CST 2021long time = date.getTime();System.out.println("time = " + time); // time = 1632639026166date.setTime(1632638970000L);System.out.println("date = " + date); // date = Sun Sep 26 14:49:30 CST 2021}
}
日历获取年月日字段
public class Test {public static void main(String[] args) {Calendar calendar = Calendar.getInstance();int year = calendar.get(Calendar.YEAR);int month = calendar.get(Calendar.MONTH) + 1;int day = calendar.get(Calendar.DAY_OF_MONTH);int hour = calendar.get(Calendar.HOUR_OF_DAY);int minute = calendar.get(Calendar.MINUTE);int second = calendar.get(Calendar.SECOND);int milliSecond = calendar.get(Calendar.MILLISECOND);// 2021-9-26 15:14:6:16System.out.print(year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second + ":" + milliSecond);}
}
日期格式化
public class Test {public static void main(String[] args) throws ParseException {SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");Date date = new Date();// 将日期对象格式化为字符串String format = sdf.format(date);System.out.println("format = " + format); // format = 2021-09-26 16:02:17// 将字符串解析为日期对象Date parse = sdf.parse(format);System.out.println("parse = " + parse); // parse = Sun Sep 26 16:02:17 CST 2021}
}
JDK8之后
之前存在的问题
- Java 1.0 中包含了一个 Date 类,但是它的大多数方法已经在 Java 1.1 中引入 Calendar 类的时候被废弃了。
-
格式化只对 Date 有效,Calendar 则不行。
-
它们不是线程安全的,不能处理闰秒等
注:API 功能更加强大,但好像用的更多的还是老版本写法
本地日期时间
-
Instant 是带时区的(以UTC为准),用来替换以前的 Date
-
LocalDateTIme 是不带时区(以本地时区为准)的,用来替换以前的 Calendar
public class Test {public static void main(String[] args) {LocalDate now = LocalDate.now();System.out.println("now = " + now); // now = 2021-09-26LocalTime now1 = LocalTime.now();System.out.println("now1 = " + now1); // now1 = 20:02:32.659LocalDateTime now2 = LocalDateTime.now();System.out.println("now2 = " + now2); // now2 = 2021-09-26T20:02:32.660Instant now = Instant.now(); System.out.println("now = " + now); // now = 2021-09-27T01:54:39.351ZLocalDateTime now1 = LocalDateTime.now();System.out.println("now1 = " + now1); // now1 = 2021-09-27T09:54:39.401LocalDateTime now = LocalDateTime.now();int year = now.getYear();System.out.println("获取年份:" + year);int monthValue = now.getMonthValue();System.out.println("获取月份:" + monthValue);int dayOfMonth = now.getDayOfMonth();System.out.println("获取天数:" + dayOfMonth);int dayOfYear = now.getDayOfYear();System.out.println("获取一年中的第" + dayOfYear + "天");DayOfWeek dayOfWeek = now.getDayOfWeek();System.out.println("获取星期" + dayOfWeek.getValue());int hour = now.getHour();System.out.println("获取小时:" + hour);int minute = now.getMinute();System.out.println("获取分钟:" + minute);int second = now.getSecond();System.out.println("获取秒:" + second);int nano = now.getNano();System.out.println("获取纳秒:" + nano);}
}
日期格式化
public class Test {public static void main(String[] args) {DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");// 将日期格式化为字符串String format = df.format(LocalDateTime.now());System.out.println("format = " + format); // format = 2021-09-27 13:55:51// 将字符串解析为日期TemporalAccessor parse = df.parse("2011-11-11 11:11:11");LocalDateTime from = LocalDateTime.from(parse);System.out.println("from = " + from); // from = 2011-11-11T11:11:11}
}