时间日期的API
JDK1.8之后提供了一套全新的时间日期API 这套全新的API在 java.time 包下;
这三个日期API里面采用静态方法 now() 获取当前的日期时间
年月日:
LocalDate
LocalDateTime now = LocalDateTime.now();
System.out.println(now);
时分秒:
LocalTime
LocalDate now1 = LocalDate.now();
System.out.println(now1);
年月日时分秒:
LocalDateTime
LocalTime now2 = LocalTime.now();
System.out.println(now2);
指定年月日时分秒:
指定年月日,时分秒 使用静态的 of():
public class MyTest2 {
public static void main(String[] args) {
LocalDateTime of = LocalDateTime.of(2010, 10, 10, 17, 20, 30);
LocalDate of1 = LocalDate.of(2019, 10, 10);
LocalTime of2 = LocalTime.of(18, 20, 30);
}
}
与获取相关的方法:
get系类的方法
now.getYear():
获取年
now.getMonth():
获得月份, 返回一个 Month 枚举值
now.getMonthValue():
获得月份(1-12)
now.getDayOfMonth():
获得月份天数(1-31)
now.getHour():
获取小时
now.getMinute():
获取分钟
now.getSecond():
获取秒
now.getDayOfWeek():
获取星期几
public static void main(String[] args) {
//注:ISO - 8601 日历系统是国际标准化组织制定的现代公民的日期和时间的表示法
LocalDateTime now = LocalDateTime.now();
//与获取相关的方法
int year = now.getYear();
Month month = now.getMonth();
int monthValue = now.getMonthValue();
System.out.println(year);
System.out.println(month.getValue());
System.out.println(monthValue);
System.out.println(now.getDayOfMonth());
System.out.println(now.getHour());
System.out.println(now.getMinute());
System.out.println(now.getSecond());
DayOfWeek dayOfWeek = now.getDayOfWeek();//获取星期几
System.out.println(dayOfWeek.getValue());
}
格式化日期日期字符串的方法 format():
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();
// System.out.println(now);
//DateTimeFormatter格式化日期的类
DateTimeFormatter f = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH时mm分ss秒");
//格式化日期的方法
String time = now.format(f);
System.out.println(time);
与转换相关的方法:
日期类型互相转换:</