作者:Java伴我余生
来源:博客园
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
概念#
时区#
不同时区在同一时刻,它们的本地时间是不同的,全球一共有24个时区,我们把伦敦所在的时区称为标准时区,其他时区按照东西偏移的小时来区分,北京所在的时区是东八区(GMT+08:00)。
时区的表现方式如下:GMT+08:00
UTC+08:00
CST(China Standard Time / Central Standard Time USA)
Asia/Shanghai
地区#
Local 表示一个国家或地区的日期、时间、数字、货币等格式zh_CN: 表示的是中国的Local,日期用年月日表示2020-07-05
en_US: 表示的是美国的Local,日期用日月年表示05/07/2020
计算机用Local在日期、时间、货币和字符串之间进行转换
中国用户 美国用户 购买价格: 12000.0 12,000.00 购买日期: 2020-07-05 05/07/2020 java.time的API#
java.time提供了新的日期和时间APILocalDate/LocalTime/LocalDateTime
ZonedDateTime/ZoneId
Instant
Formatter
...
新API的特点:严格区分日期、时间
不变类(类似String)
Month范围1~12(Jan~Dec)
Week范围1~7(Mon~Sun)
LocalDateTime#
LocalDate d = LocalDate.now(); // 当前日期 LocalTime t = LocalTime.now(); //当前时间 LocalDateTime dt = LocalDateTime.now(); // 当前日期时间 System.out.println(dt); // 严格按照ISO 8601格式打印 2020-07-05T16:38:37.356 //指定日期和时间 LocalDate d2 = LocalDate.of(2020, 7, 5); // 2020-07-05,注意 7=7月 LocalTime t2 = LocalTime.of(16, 38, 37); // 16:38:37 LocalDateTime dt2 = LocalDateTime.of(2020, 7, 5,16, 38, 37); // 2020-07-05T16:38:37 LocalDateTime dt3 = LocalDateTime.of(d2, t2); // 2020-07-05T16:38:37 //对日期进行格式化a DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); System.out.println(dtf.format(LocalDateTime.now())); // 2020-07-05 16:45:08 //将字符串解析成日期 LocalDateTime parse = LocalDateTime.parse("2020-07-05 16:45:08", dtf); System.out.println(parse); // 2020-07-05T16:45:08
新的DateTimeFormatter是不可变的。默认情况下,LocalDate,LocalTime,LocalDateTime按照ISO标准格式化和解析:yyyy-MM-dd
HH:mm:ss
HH:mm:ss:SSS
yyyy-MM-dd'T'HH:mm:ss
yyyy-MM-dd'T'HH:mm:ss:SSS
重大更新:对日期和时间进行加减plusDays() 在现在的日期加上具体天数
minusHous() 在现在的时间减去具体的小时
plusWeeks() 在现在的日期时间加上具体的周数
// +5 天 LocalDate today = LocalDate.now(); LocalDate after5Days = today.plusDays(5); System.out.println(after5Days); //2020-07-10 // -2小时 LocalTime now = LocalTime.now(); LocalTime before2Hours = now.minusHours(2); System.out.println(before2Hours); // 14:59:22.526 // +1月-2周 LocalDate date = today.plusMonths(1).minusWeeks(2); System.out.println(date); // 2020-07-22
对日期和时间进行调整:withDayOfMonth()
withMonth()
withHour()
...
with()
//本月第一天 LocalDate firstDay = LocalDate.now().withDayOfMonth(1); System.out.println(firstDay); // 2020-07-01 //把秒和纳秒调整为0 LocalTime at = LocalTime.now().withSecond(0).withNano(0); System.out.println(at); // 17:08 //本月最后一天 LocalDate lastDay = LocalDate.now().with(TemporalAdjusters.lastDayOfMonth()); System.out.println(lastDay); // 2020-07-31 //本月第一个周日 LocalDate firstSunday = LocalDate.now().with(TemporalAdjusters.firstInMonth(DayOfWeek.SUNDAY)); System.out.println(firstSunday); //2020-07-05
判断日期和时间的先后:isBefore()
isAfter()
equals()
LocalDate d01 = LocalDate.of(2020,7,5); LocalDate d02 = LocalDate.of(2020,7,4); System.out.println(d01.isBefore(d02)); // false System.out.println(d01.isAfter(d02)); // true
计算日期的Period: 某一天到指定的某一天具体相差多久getYears()
getMonths()
getDays()
LocalDate d03 = LocalDate.of(2020,7,5); LocalDate d04 = LocalDate.of(2018,3,28); //通过until()方法获取Period对象,判断两个日期相差?年?月?天 Period period = d03.until(d04); System.out.println(period); // P-2Y-3M-8D 表示2020年7月5日到2018年3月28日中相差 2年3个月8天 //两个日期一共相差多少天? long day01 = LocalDate.of(2020, 7, 5).toEpochDay(); long day02 = LocalDate.of(2018,3,28).toEpochDay(); System.out.println(day01-day02); // 830
LocalDateTime无法与long进行转换因为LocalDateTime 没有时区,无法确定某一时刻
ZonedDateTime 有时区,可以与long进行转换
ZonedDateTime#
ZonedDateTime = LocalDateTime + ZoneIdZonedDateTime: 带时区的日期和时间
ZoneId: 新的时区对象(取代旧的java.util.TimeZone)
Instant: 时刻对象(epoch seconds)
ZonedDateTime zbj = ZonedDateTime.now(); // 当前时区的日期和时间 System.out.println(zbj); // 2020-07-05T17:32:40.415+08:00[Asia/Shanghai] ZonedDateTime zny = ZonedDateTime.now(ZoneId.of("America/New_York")); // 纽约时区的当前日期和时间 System.out.println(zny); // 2020-07-05T05:34:29.522-04:00[America/New_York]
ZonedDateTime可以从LocalDateTime转换: atZone()
//关联到当前默认时区 ZonedDateTime bj = ldt.atZone(ZoneId.systemDefault()); System.out.println(bj); // 2020-07-05T17:36:12+08:00[Asia/Shanghai] //关联到纽约时区 ZonedDateTime ny = ldt.atZone(ZoneId.of("America/New_York")); System.out.println(ny); // 2020-07-05T17:36:12-04:00[America/New_York]
转换时区:withZoneSamleInstant()
LocalDateTime ldt = LocalDateTime.of(2020, 7, 5, 17, 36, 12); //关联到当前默认时区 ZonedDateTime bj = ldt.atZone(ZoneId.systemDefault()); System.out.println(bj); // 2020-07-05T17:36:12+08:00[Asia/Shanghai] // 转换到纽约时区 ZonedDateTime zdt = bj.withZoneSameInstant(ZoneId.of("America/New_York")); System.out.println(zdt); // 2020-07-05T17:36:12-04:00[America/New_York]
Instant对象表示时刻:ZonedDateTime.toInstant()
Instant.getEpochSecond()
Instant ins = Instant.now(); Instant ins2 = ZonedDateTime.now().toInstant(); ZonedDateTime zdt = ins.atZone(ZoneId.of("Z")); //注意是秒 long epoch = ins.getEpochSecond();
如果项目中需要使用generatorConfig.xml配置文件生成java8中的日期类型LocalDate、LocalTime、LocalDateTime等等,需要在配置文件中的javaTypeResolver结点中加入属性useJSR310Types,当useJSR310Types为true时,就会jdbc对应的日期类型会转成java8中的LocateDateTime类型,如果useJSR310Types为false,则还是转成java.util.Date类型。
一旦mybatis中使用了java8中的日期类型,记得要在项目中引入mybatis-typehandlers-jsr310,这个主要是处理java8中的日期类型。
org.mybatismybatis-typehandlers-jsr3101.0.2
注意:用maven mybatis插件如果不在plugin里面添加依赖包的引用的话,会找不到相关得jar包,在plugin外部得jar包,他不会去找到并执行,所以要把plugin运行依赖得jar配置都放在里面,pom.xml配置 mybatis插件如下:
org.mybatis.generatormybatis-generator-maven-plugin1.3.7org.mybatismybatis3.4.6mysqlmysql-connector-java8.0.13
注意,在连接mysql8.0的时候要指明timeZone,即serverTimezone=UTC这个不能忘