1.Date类概述和构造方法


2.Date的常用方法

public class DateDemo2 {
public static void main(String[] args) {
Date date1 = new Date();
date1.setTime(0L);
System.out.println(date1);
//methods1();
}
public static void methods1() {
//getTime():获取当前时间
Date date1 = new Date();
System.out.println(date1.getTime());
}
}
SimpleDateFormat可以对Date对象,进行格式化和解析
1.SimpleDateFormat的构造方法

2.SimpleDateFormat格式化和解析日期
① 格式化(从Date到String)
② 解析(从String到Date)
格式化
public class DateDemo3 {
public static void main(String[] args) {
Date date = new Date();
//SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd HH时mm分ss秒");
//SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd");
//SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String format = sdf.format(date);
System.out.println(format);
}
}
解析
public class DateDemo4 {
public static void main(String[] args) throws ParseException {
String s = "2022-11-13";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = sdf.parse(s);
System.out.println(date);
}
}
1.JDK8Date分类

2.LocalDateTime创建时间

public class JDK8DateDemo2 {
public static void main(String[] args) throws ParseException {
LocalDateTime now = LocalDateTime.now();
System.out.println(now);
LocalDateTime time = LocalDateTime.of(2020, 11, 1, 8, 10, 10);
DateTimeFormatter dft = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String format = time.format(dft);
System.out.println(format);
}
}
3.LocalDateTime获取方法

public class JDK8DateDemo3 {
public static void main(String[] args) throws ParseException {
LocalDateTime dateTime = LocalDateTime.of(2020, 11, 12, 8, 10, 10);
//public int getYear()
int timeYear = dateTime.getYear();
System.out.println("年为 " + timeYear);
//public int getMonthValue()
int monthValue = dateTime.getMonthValue();
System.out.println("月为 " + monthValue);
//public int getDayOfMonth()
int dayOfMonth = dateTime.getDayOfMonth();
System.out.println("月中第 " + dayOfMonth + "天");
//public int getDayOfYear()
int dayOfYear = dateTime.getDayOfYear();
System.out.println("年中第 " + dayOfYear + "天");
//public DayOfWeek getDayOfWeek()
DayOfWeek dayOfWeek = dateTime.getDayOfWeek();
System.out.println("星期为 " + dayOfWeek);
//public int getHour()
int hour = dateTime.getHour();
System.out.println("小时为 " + hour);
//public int getMinute()
int minute = dateTime.getMinute();
System.out.println("分钟为 " + minute);
//public int getSecond()
int second = dateTime.getSecond();
System.out.println("秒为 " + second);
}
}
4.LocalDateTime转换方法


public class JDK8DateDemo4 {
public static void main(String[] args) throws ParseException {
LocalDateTime dateTime = LocalDateTime.of(2020, 11, 12, 8, 10, 10);
LocalDate localDate = dateTime.toLocalDate();
System.out.println(localDate);
LocalTime localTime = dateTime.toLocalTime();
System.out.println(localTime);
}
}
5.LocalDateTime格式化和解析

6.JDK8的日期格式化器:DateTimeFormatter

public class JDK8DateDemo5 {
public static void main(String[] args) throws ParseException {
method2();
// method1();
}
public static void method2() {
String s ="2008年08月07日 13:14:15";
DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
LocalDateTime date= LocalDateTime.parse(s, pattern);
System.out.println(date);
}
public static void method1() {
LocalDateTime dateTime = LocalDateTime.of(2020, 11, 12, 8, 10, 10);
System.out.println(dateTime);
DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String date = dateTime.format(pattern);
System.out.println(date);
}
}
7.LocalDateTime增加或者减少时间的方法

方法返回一个新的LocalDateTime对象,返回值就是修改之后的结果
参数为正,就是往后加
参数为负,就是往前减

public class JDK8DateDemo6 {
public static void main(String[] args) throws ParseException {
LocalDateTime dateTime = LocalDateTime.of(2020, 11, 12, 8, 10, 10);
LocalDateTime nweDateTime = dateTime.plusYears(1);
System.out.println(nweDateTime);
}
}
8.LocalDateTime修改方法

public class JDK8DateDemo7 {
public static void main(String[] args) throws ParseException {
LocalDateTime dateTime = LocalDateTime.of(2020, 11, 12, 8, 10, 10);
LocalDateTime newDate = dateTime.withYear(2066);
System.out.println(newDate);
LocalDateTime dateTime1 = dateTime.withMonth(10);
System.out.println(dateTime1);
}
}

public class JDK8DateDemo8 {
public static void main(String[] args) throws ParseException {
LocalDate dateTime1 = LocalDate.of(2020, 11, 12);
LocalDate dateTime2 = LocalDate.of(2028, 10, 10);
//public static Period between(LocalDate startDateInclusive, LocalDate endDateExclusive)
Period period = Period.between(dateTime1, dateTime2);
System.out.println(period);//P7Y10M28D
//public int getYears() //这段时间的年数
System.out.println(period.getYears());//7
//public int getMonths()//这段时间的月数
System.out.println(period.getMonths());//10
//public int getDays()//这段时间的天数
System.out.println(period.getDays());//28
// public long toTotalMonths()//这段时间的总月数
System.out.println(period.toTotalMonths());//94
}
}

public class JDK8DateDemo9 {
public static void main(String[] args) throws ParseException {
LocalDateTime localDateTime1 = LocalDateTime.of(2020, 11, 12, 10, 13, 14);
LocalDateTime localDateTime2 = LocalDateTime.of(2020, 11, 13, 12, 10, 15);
Duration duration = Duration.between(localDateTime1, localDateTime2);
System.out.println(duration);//PT25H57M1S
// public long toHours(): 时间间隔的小时
System.out.println(duration.toHours());//25
// public long toMinutes(): 时间间隔的分钟
System.out.println(duration.toMinutes());//1557
//public long toSeconds(): 时间间隔的秒
System.out.println(duration.toSeconds());93421
//public long toMillis():时间间隔的毫秒
System.out.println(duration.toMillis());//93421000
//public long toNanos():时间间隔的纳秒
System.out.println(duration.toNanos());//93421000
}
}