java8 之java.time
Java 8 之 java.time 包
出处:http://coderbee.net
包概述
java.time 包是在JDK8新引入的,提供了用于日期、时间、实例和周期的主要API。
java.time包定义的类表示了日期-时间概念的规则,包括instants, durations, dates, times, time-zones and periods。这些都是基于ISO日历系统,它又是遵循 Gregorian规则的。
所有类都是不可变的、线程安全的。
一些类的介绍
LocalDateTime:存储了日期和时间,如:
2013-10-15T14:43:14.539
。LocalDate:存储了日期,如:
2013-10-15
。LocalTime:存储了时间,如:
14:43:14.539
。
上面的类可以由下面的类组合来:
类之间转换的示例:
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println("localDateTime :" + localDateTime);
LocalDate localDate = LocalDate.now();
System.out.println("localDate :" + localDate);
LocalTime localtime = LocalTime.now();
System.out.println("localtime :" + localtime);
// 获取当前年份
Year year = Year.now();
System.out.println("year :" + year);
// 从Year获取LocalDate
LocalDate localDate1 = year.atDay(59);
System.out.println("localDate1 :" + localDate1);
// 把LocalTime关联到一个LocalDate得到一个LocalDateTime
LocalDateTime localDateTime1 = localtime.atDate(localDate1);
System.out.println("localDateTime1 :" + localDateTime1);
// 用指定的年获取一个Year
Year year1 = Year.of(2012);
System.out.println("year1 :" + year1);
// 从Year获取YearMonth
YearMonth yearMonth = year1.atMonth(2);
System.out.println("yearMonth :" + yearMonth);
// YearMonth指定日得到 LocalDate
LocalDate localDate2 = yearMonth.atDay(29);
System.out.println("localDate2 :" + localDate2);
// 判断是否是闰年
System.out.println("isLeapYear :" + localDate2.isLeapYear());
//自动处理闰年的2月日期
//创建一个 MonthDay
MonthDay monthDay = MonthDay.of(2, 29);
LocalDate leapYear = monthDay.atYear(2012);
System.out.println("leapYear :" + leapYear);
//同一个 MonthDay 关联到另一个年份上
LocalDate nonLeapYear = monthDay.atYear(2011);
System.out.println("nonLeapYear :" + nonLeapYear);
上面代码的输出结果为:
localDateTime :2013-10-15T15:11:57.489
localDate :2013-10-15
localtime :15:11:57.489
year :2013
localDate1 :2013-02-28
localDateTime1 :2013-02-28T15:11:57.489
year1 :2012
yearMonth :2012-02
localDate2 :2012-02-29
isLeapYear :true
leapYear :2012-02-29
nonLeapYear :2011-02-28
格式化与时间计算
DateTimeFormatter:在日期对象与字符串之间进行转换。
ChronoUnit:计算出两个时间点之间的时间距离,可按多种时间单位计算。
TemporalAdjuster:各种日期计算功能。
续前面的代码:
DayOfWeek dayOfWeek = DayOfWeek.of(1);
System.out.println("dayOfWeek :" + dayOfWeek);
//计算两个日期之间的天数,还可以按其他时间单位计算两个时间点之间的间隔。
long between = ChronoUnit.DAYS.between(localDate, leapYear);
System.out.println("between :" + between);
// 线程安全的格式化类,不用每次都new个SimpleDateFormat
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("uuuu MM dd");
// 把日期时间转换为字符串标识
System.out.println("date formatter :" + dateTimeFormatter.format(nonLeapYear));
// 解析字符串形式的日期时间
TemporalAccessor temporalAccessor = dateTimeFormatter.parse("2013 01 15");
System.out.println("temporalAccessor :" + LocalDate.from(temporalAccessor));
Instant instant = Instant.now(); // 时间戳
System.out.println("instant :" + instant);
//计算某月的第一天的日期
LocalDate with = nonLeapYear.with(TemporalAdjuster.firstDayOfMonth());
System.out.println("with :" + with);
// 计算某月的第一个星期一的日期
TemporalAdjuster temporalAdjuster = TemporalAdjuster.firstInMonth(DayOfWeek.MONDAY);
LocalDate with1 = localDate.with(temporalAdjuster);
System.out.println("with1 :" + with1);
// 计算localDate的下一个星期一的日期
LocalDate with2 = localDate.with(TemporalAdjuster.next(DayOfWeek.MONDAY));
System.out.println("with2 :" + with2);
输出:
dayOfWeek :MONDAY
between :-594
date formatter :2011 02 28
temporalAccessor :2013-01-15
instant :2013-10-15T07:55:30.964Z
with :2011-02-01
with1 :2013-10-07
with2 :2013-10-21
java.util.Date到新库类的转换
转换可通过下面的方法进行。
Date.toInstant()
Date.from(Instant)
Calendar.toInstant()
方法概览
该包的API提供了大量相关的方法,这些方法一般有一致的方法前缀:
- of:静态工厂方法。
- parse:静态工厂方法,关注于解析。
- get:获取某些东西的值。
- is:检查某些东西的是否是true。
- with:不可变的setter等价物。
- plus:加一些量到某个对象。
- minus:从某个对象减去一些量。
- to:转换到另一个类型。
- at:把这个对象与另一个对象组合起来,例如:
date.atTime(time)
。
想起以前做报表都是在存储过程里处理日期的,因为Java的日期操作确实太弱了。有了Java 8,妈妈再也不用担心我处理日期了。
相关 [java java.time] 推荐:
Java 8 之 java.time 包
- - 码蜂笔记
Java中的锁(Locks in Java)
- - 并发编程网 - ifeve.com
Java PaaS 对决
- 呆瓜 - IBM developerWorks 中国 : 文档库
Java浮点数
- d0ngd0ng - 译言-电脑/网络/数码科技
Qt——转战Java?
- - 博客 - 伯乐在线
java 验证码
- - ITeye博客
java面试题
- - Java - 编程语言 - ITeye博客
Java使用memcached
- - 互联网 - ITeye博客
Java线程池
- - 企业架构 - ITeye博客
Java异常
- - CSDN博客推荐文章
java8 之java.time的更多相关文章
- JAVA8的java.util.function包 @FunctionalInterface
1 函数式接口java.util.function https://www.cnblogs.com/CobwebSong/p/9593313.html 2 JAVA8的java.util.functi ...
- Function接口 – Java8中java.util.function包下的函数式接口
Introduction to Functional Interfaces – A concept recreated in Java 8 Any java developer around the ...
- Java8——jdk——java.time包
public class TestLocalDateTime { //6.ZonedDate.ZonedTime.ZonedDateTime : 带时区的时间或日期 @Test public void ...
- JAVA8的java.util.function包
一 概述 name type description Consumer Consumer< T > 接收T对象,不返回值 Predicate Predicate< T > 接收 ...
- 《Java学习笔记(第8版)》学习指导
<Java学习笔记(第8版)>学习指导 目录 图书简况 学习指导 第一章 Java平台概论 第二章 从JDK到IDE 第三章 基础语法 第四章 认识对象 第五章 对象封装 第六章 继承与多 ...
- java8--类加载机制与反射(java疯狂讲义3复习笔记)
本章重点介绍java.lang.reflect包下的接口和类 当程序使用某个类时,如果该类还没有被加载到内存中,那么系统会通过加载,连接,初始化三个步骤来对该类进行初始化. 类的加载时指将类的clas ...
- Java8函数式编程
在Java8的 java.util.function中包含以下几个接口 1.Function,先上源码 /* * Copyright (c) 2010, 2013, Oracle and/or its ...
- 【转】Java 8十个lambda表达式案例
1. 实现Runnable线程案例 使用() -> {} 替代匿名类: //Before Java 8: new Thread(new Runnable() { @Override public ...
- 使用Java Service Wrapper在Linux下配置Tomcat应用
前言 Java Service Wrapper是Tanuki Software的一个产品,可以将Java应用注册成Windows或Linux服务,使其可以随系统开机启动,同时可以监控Java应用的状态 ...
随机推荐
- POJ 1947-Rebuilding Roads(树形背包)
题意: 一个树求得到一个节点数为p的子树,最小需要删除的边数. 分析:父节点到儿子这条边,删或不删,背包问题. #include <map> #include <set> #i ...
- codeforces 672C - Recycling Bottles 贪心水题
感觉很简单,就是讨论一下 #include <stdio.h> #include <string.h> #include <algorithm> #include ...
- lightoj 1020 (博弈)
思路:很简单的博弈,找出每个人先拿的必胜态进行状态转移即可. #include<cstdio> #include<string> #include<cstring> ...
- mexopencv问题:Invalid MEX file GLIBCXX_3.4.15 error
参考:http://blog.sina.com.cn/s/blog_74112f030101cmxt.html root@debian-yuliyang:/opt/matlab/sys/os/glnx ...
- ios开发中,A valid provisioning profile for this executable was not found,的解决方法
手头上的一个ios项目在上架后,再进行时出现了以上的这个错误,这是上架后忘了对一些配置进行复原 我的项目解决方法是: 是上面的这一块出现了问题,图片上的配置是正常的情况,但是上架的时候对其进行了修改, ...
- leetcode—Best Time to Buy and Sell stocks III
1.题目描述 Say you have an array for which the ith element is the price of a given stock on day i. Des ...
- leetcode@ [241] Different Ways to Add Parentheses (Divide and Conquer)
https://leetcode.com/problems/different-ways-to-add-parentheses/ Given a string of numbers and opera ...
- leetcode@ [84/85] Largest Rectangle in Histogram & Maximal Rectangle
https://leetcode.com/problems/largest-rectangle-in-histogram/ https://leetcode.com/problems/maximal- ...
- Base64 报错 的解决办法 (Base-64 字符数组或字符串的长度无效。, 输入的不是有效的 Base-64 字符串,因为它包含非 Base-64 字符、两个以上的填充字符,或者填充字符间包含非法字符。)
Base64 报错 的解决办法, 报错如下:1. FormatException: The input is not a valid Base-64 string as it contains a n ...
- A Tour of Go Mutating Maps
Insert or update an element in map m: m[key] = elem Retrieve an element: elem = m[key] Delete an ele ...