为什么需要LocalDate、LocalTime、LocalDateTime
这个问题等同于java8之前的Date类(java.util.Date)有什么不足。
- Date类从JDK1.0开始就存在了,但因为它历史悠久,所以它的大部分构造器、方法都已经过时了,不再推荐使用了。
- Date类只能存储时间,如果涉及到时间的操作需要配合
SimpleDateFormat
使用。 - 所有Date类都是可变的,因此它们不是线程安全的。这是Java Date和Calendar类的最大问题之一。
localDate
LocalDate不包含时间,只是单纯的年月日
package example;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Month;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.Date;
public class JavaDateTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
java8LocalDateTest();
}
private static void java8LocalDateTest() {
System.out.println("========java8LocalDateTest-Begin========");
// 取当前日期:
LocalDate today = LocalDate.now(); // -> 2020-05-18
// 根据年月日取日期:
LocalDate crischristmas = LocalDate.of(2020, 05, 17); // -> 2020-05-17
// 根据字符串取:
LocalDate endOfFeb = LocalDate.parse("2020-02-29"); // 严格按照ISO yyyy-MM-dd验证,02写成2都不行,当然也有一个重载方法允许自己定义格式
// LocalDate.parse("2020-02-30"); // 无效日期无法通过:DateTimeParseException: Invalid date
System.out.println(today);
System.out.println(crischristmas);
System.out.println(endOfFeb);
// 取本月第1天:
LocalDate firstDayOfThisMonth = today.with(TemporalAdjusters.firstDayOfMonth()); // 2020-05-01
// 取本月第2天:
LocalDate secondDayOfThisMonth = today.withDayOfMonth(2); // 2020-05-02
// 取本月最后一天,再也不用计算是28,29,30还是31:
LocalDate lastDayOfThisMonth = today.with(TemporalAdjusters.lastDayOfMonth()); // 2020-05-31
// 取下一天:
LocalDate firstDayOf2020 = lastDayOfThisMonth.plusDays(1); // 变成了2020-06-01
// 取2020年1月第一个周一,用Calendar要死掉很多脑细胞:
LocalDate firstMondayOf2020 = LocalDate.parse("2020-01-01").with(TemporalAdjusters.firstInMonth(DayOfWeek.MONDAY)); // 2020-01-06
System.out.println(firstDayOfThisMonth);
System.out.println(secondDayOfThisMonth);
System.out.println(lastDayOfThisMonth);
System.out.println(firstDayOf2020);
System.out.println(firstMondayOf2020);
// toEpochDay(),从1970-01-01(ISO)开始的天数
System.out.println(today.toEpochDay());
System.out.println("========java8LocalDateTest-End========");
}
}
/*
* ========java8LocalDateTest-Begin========
* 2020-05-18
* 2020-05-17
* 2020-02-29
* 2020-05-01
* 2020-05-02
* 2020-05-31
* 2020-06-01
* 2020-01-06
* 18400
* ========java8LocalDateTest-End========
*/
localTime
package example;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Month;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.Date;
public class JavaDateTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
java8LocalTimeTest();
}
private static void java8LocalTimeTest() {
System.out.println("========java8LocalTimeTest-Begin========");
LocalTime now = LocalTime.now(); // 23:11:08.006
LocalTime zero = LocalTime.of(0, 0, 0); // 00:00:00
LocalTime mid = LocalTime.parse("12:00:00"); // 12:00:00
System.out.println("当前时间:" + now);
System.out.println(zero);
System.out.println(mid);
System.out.println("========java8LocalTimeTest-End========");
}
}
/*
* 当前时间:11:03:08.282
* 00:00
* 12:00
* ========java8LocalTimeTest-End========
*/
localDateTime
package example;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Month;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.Date;
public class JavaDateTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
java8LocalDateTimeTest();
}
private static void java8LocalDateTimeTest() {
// 获取当前此刻的时间
System.out.println("========java8LocalDateTimeTest-Begin========");
LocalDateTime rightNow = LocalDateTime.now();
System.out.println( "当前时刻:" + rightNow );
System.out.println( "当前年份:" + rightNow.getYear() );
System.out.println( "当前月份:" + rightNow.getMonth() );
System.out.println( "当前日份:" + rightNow.getDayOfMonth() );
System.out.println( "当前时:" + rightNow.getHour() );
System.out.println( "当前分:" + rightNow.getMinute() );
System.out.println( "当前秒:" + rightNow.getSecond() );
// 构造一个指定年、月、日的时间:
LocalDateTime beforeDate = LocalDateTime.of(2020, Month.MAY, 1, 1, 0, 0);
System.out.println("构造的时间:" + beforeDate);
// 修改日期
LocalDateTime afterDateTime = changeDateTime(beforeDate);
System.out.println("修改后的时间:" + afterDateTime);
formatPrintDate(rightNow);
System.out.println("========java8LocalDateTimeTest-End========");
}
private static LocalDateTime changeDateTime(LocalDateTime ldt) {
// 减少两年
ldt = ldt.minusYears(2);
// 增加三个月
ldt = ldt.plusMonths(3);
// 修改年份
ldt = ldt.withYear(2019);
// 修改小时
ldt = ldt.withHour(2);
//减少一天
ldt = ldt.minusDays(1);
return ldt;
}
private static void formatPrintDate(LocalDateTime ldt) {
String result1 = ldt.format( DateTimeFormatter.ISO_DATE );
String result2 = ldt.format( DateTimeFormatter.BASIC_ISO_DATE );
String result3 = ldt.format( DateTimeFormatter.ofPattern("yyyy/MM/dd") );
System.out.println("格式化后的日期(基本样式一举例):" + result1);
System.out.println("格式化后的日期(基本样式二举例):" + result2);
System.out.println("格式化后的日期(自定义样式举例):" + result3);
}
}
/*
* ========java8LocalDateTimeTest-Begin========
* 当前时刻:2020-05-18T11:03:08.282
* 当前年份:2020
* 当前月份:MAY
* 当前日份:18
* 当前时:11
* 当前分:3
* 当前秒:8
* 构造的时间:2020-05-01T01:00
* 修改后的时间:2019-07-31T02:00
* 格式化后的日期(基本样式一举例):2020-05-18
* 格式化后的日期(基本样式二举例):20200518
* 格式化后的日期(自定义样式举例):2020/05/18
* ========java8LocalDateTimeTest-End========
*/
JDBC
最新JDBC映射将把数据库的日期类型和Java 8的新类型关联起来:
SQL -> Java
--------------------------
date -> LocalDate
time -> LocalTime
timestamp -> LocalDateTime
面试题
输入某年某月某日,判断这一天是这一年的第几天?
package com.neusoft.test;
import java.time.LocalDate;
import java.util.Scanner;
/*
* 输入某年某月某日,判断这一天是这一年的第几天?
*/
public class Unit14 {
public static void main(String[] args) {
int year, month, day;
Scanner s = new Scanner(System.in);
System.out.print("请输入年:");
year = s.nextInt();
System.out.print("请输入月:");
month = s.nextInt();
System.out.print("请输入日:");
day = s.nextInt();
LocalDate date = LocalDate.of(year, month, day);
System.out.println(date.toEpochDay() - LocalDate.of(year, 1, 1).toEpochDay());
}
}