Date
package test;
import java.time.LocalDateTime;
//取得当前的日期时间
public class GetDatetime {
public static void main(String[] args) {
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println(localDateTime); //2018-07-27T15:14:09.016
}
}
package test;
import java.time.LocalDate;
import java.time.MonthDay;
//判断是否闰年
import java.time.Year;
import java.time.YearMonth;
public class LeapYear {
public static void main(String[] args) {
Year year = Year.of(2018); //用指定的年获取一个Year
YearMonth yearMonth = year.atMonth(7); //从Year获取YearMonth
LocalDate localDate = yearMonth.atDay(27); //YearMonth指定日得到LocalDate
System.out.println("time = " + localDate); //time = 2018-07-27
//判断是否是闰年
System.out.println("isLeapYear : " + localDate.isLeapYear()); //isLeapYear : false
//自动处理闰年的2月日期
//创建一个MonthDay
MonthDay monthDay = MonthDay.of(2, 29);
LocalDate localDate2 = monthDay.atYear(2012);
System.out.println("leapYear : " + localDate2); //leapYear : 2012-02-29
//同一个monthDay关联另一个年份上
LocalDate nonLeapYear = monthDay.atYear(2014);
System.out.println("isnotleapyear : " + nonLeapYear); //isnotleapyear : 2014-02-28
}
}
package test;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class DateFormatDemo {
public static void main(String[] args) {
//获取当前时间
LocalDate localDate = LocalDate.now();
//指定格式规则
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu-MM-dd");
//将当前时间格式化
String str = localDate.format(formatter);
System.out.println("时间 : " + str); //时间 : 2018-07-27
}
}
拼命敲