java8 – Date/Time
关于为什么要学习java8的新的时间API,这里就不做详细说明:大概几点是以前的API大多不够系统,功能不够完善,并且日期类可变,因此不是线程安全的。
新特性
- 不变性:新的时间/日期API中,所有的类都是不可变的,因此是线程安全的。
- java.time :LocalDate LocalTime
API 学习
- java.time.LocalDate: 不可变,日期(yyyy-MM-dd)
- 获取当前日期 now(重载各种方式)
- 获取特定日期
- 解析(解析默认格式, 解析自定义格式)
- 格式化输出 (输出)
- 计算(几天前,几天后。。。)
package date;
import java.time.LocalDate;
import java.time.Month;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
/** * @author wangzhiping movingdaywzp@gmail.com * @date 2016/08/19 12:58 * @brief learning the new feature of time/date of java8 * immutable and thread-safe */
public class Date {
public static void main(String[] args) {
// today
LocalDate today = LocalDate.now();
System.out.println("today : " + today);
// special day
//LocalDate specialday = LocalDate.of(2016, 1, 22);
LocalDate specialday = LocalDate.of(2016, Month.JANUARY, 22);
System.out.println("speciaday : " + specialday);
LocalDate tenthDay2016 = LocalDate.ofYearDay(2016, 10);
System.out.println("10th day of 2016 : " + tenthDay2016);
// 解析
// default format
LocalDate parse = LocalDate.parse("2016-08-19");
System.out.println("default format of (yyyy-MM-dd) : " + parse);
// pattern format
LocalDate specialFormat = LocalDate.parse("2016/08/19",
DateTimeFormatter.ofPattern("yyyy/MM/dd"));
System.out.println("special format : " + specialFormat);
// 格式化输出
// default output
System.out.println("default output (yyyy-MM-dd) : " + LocalDate.now());
// pattern output
System.out.println("pattern output (yyyy/MM/dd) : " + LocalDate.now().
format(DateTimeFormatter.ofPattern("yyyy/MM/dd")));
// 计算
// 前几天, 后几天
System.out.println("5 days ago : " + today.minus(5, ChronoUnit.DAYS));
System.out.println("5 days ago : " + today.minusDays(5));
System.out.println("5 days later : " + today.plus(5, ChronoUnit.DAYS));
}
}
结果:
today : 2016-08-23
speciaday : 2016-01-22
10th day of 2016 : 2016-01-10
default format of (yyyy-MM-dd) : 2016-08-19
special format : 2016-08-19
default output (yyyy-MM-dd) : 2016-08-23
pattern output (yyyy/MM/dd) : 2016/08/23
5 days ago : 2016-08-18
5 days ago : 2016-08-18
5 days later : 2016-08-28
- java.time.LocalTime:不可变,时间(hh:mm:ss.zzz)
- 获取当前时间 now(重载各种方式)
- 获取特定时间
- 解析(解析默认格式, 解析自定义格式)
- 格式化输出 (输出)
- 计算(几个小时前, 几个小时后,。。。)
package date;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
/** * learning the new feature of Time of java8 * @author wangzhiping movingwzp@gmail.com * @date 2016/08/21 13:46 * */
public class Time {
public static void main(String[] args) {
// Now
LocalTime timenow = LocalTime.now();
LocalTime time = LocalTime.now(ZoneId.of("Asia/Kolkata"));
System.out.println("current time : " + timenow);
System.out.println("time of zone : " + time);
// Special Time( of)
LocalTime specialtime = LocalTime.of(11, 59);
System.out.println("specialTime : " + specialtime);
// 解析
// default format
LocalTime parseFromText = LocalTime.parse("11:59:59");
System.out.println("parse from (hh:mm:ss) : " + parseFromText);
//special format 注:易错点在于ofPattern的编写,要注意每个symbol的含义,比如把HH/mm/ss 写成 hh/mm/ss就会出错,可以F3进去查看具体含义
LocalTime parseFromFormatText = LocalTime.parse("11/59/59",
DateTimeFormatter.ofPattern("HH/mm/ss"));
System.out.println("parse from (HH/mm/ss) : " + parseFromFormatText);
// 格式化输出
// default output
System.out.println("default output : " + LocalTime.now());
// special output
System.out.println("special output : " + LocalTime.now().
format(DateTimeFormatter.ofPattern("HH--mm??ss")));
// 计算
// 3小时5分6秒后
System.out.println("time later : " + LocalTime.now().plusHours(3).
plusMinutes(5).plusMinutes(6)
.format(DateTimeFormatter.ofPattern("HH:mm:ss")));
}
}
结果:
current time : 14:46:40.091
time of zone : 12:16:40.092
specialTime : 11:59
parse from (hh:mm:ss) : 11:59:59
parse from (HH/mm/ss) : 11:59:59
default output : 14:46:40.109
special output : 14--46??40
time later : 17:57:40
- java.time.LocalDateTime: yyyy-MM-dd-HH-mm-ss.zzz 学习方式同上