Java 8时间和日期API

表示时间和日期

Instant instant = LocalDate.now().atStartOfDay()
			.atZone(ZoneId.systemDefault()).toInstant();
DataPointId pointId = new DataPointId(accountName, Date.from(instant));
Instant now = Instant.now();//获取当前时刻
Instant now = Instant.ofEpochMilli(System.currentTimeMillis());

//转换Date为Instant
public static Instant toInstant(Date date){
	return Instant.ofEpochMilli(date.getTime());
}
//转换Instant为Date
public static Date toDate(Instant instant){
	reurn new Date(instant.toEpochMilli());
}

LocalDateTime ldt = LocalDateTime.now();//与时区无关的日期和时间,获取系统默认的时区
LocalDateTime ldt = LocalDateTime.of(2017, 7, 11, 20, 45, 5);//构建LocalDateTime
ldt.toLocalDate();
ldt.toLocalTime();
LocalDate.of(2017, 7, 11);//.atTime(20, 45, 5)  日期加上了时间
LocalDate.now();
LocalTime.of(20, 45, 5);//.atDate(LocalDate.of(2017, 7, 11))
LocalTime.now();

//LocalDateTime不能注解转为时刻Instant,转换需要一个参数ZoneOffset
public static Instant toBeijingInstant(LocalDateTime ldt){
	return ldt.toInstant(ZoneOffset.of("+08:00"));
}

//北京时区
ZoneId bjZone = ZoneId.of("GMT+08:00");

//表示特定时区的日期和时间
ZoneDateTime zdt = ZonedDateTime.now();

//根据Instant和时区构建ZonedDateTime
public static ZonedDateTime ofInstant(Instant instant,ZoneId zone);
//根据LocalDate/LocalTime和ZoneId构造
public static ZonedDateTime of(LocalDate date,LocalTime time,ZoneId zone);

//ZoneDateTime可以直接转换为Instant
ZonedDateTime ldt = ZonedDateTime.now();
Instant now = ldt.toInstant();

//调整时间3点20分
LocalDateTime ldt = LocalDateTime.now();
ldt = ldt.withHour(15).withMinute(20).withSecond(0).withNano(0);

ldt = toLocalDate().atTime(15,20);

//3小时5分钟后
ldt = ldt.plusHours(3).plusMinutes(5);
//今天0点 ChronoField是一个枚举类 MILLI_OF_DAY表示一天的毫秒数
LocalDateTime ldt = LocalDateTime.now();
ldt = ldt.with(ChronoField.MILLI_OF_DAY, 0);

LocalDateTime ldt = LocalDateTime.of(LocalDate.now(),LocalTime.MIN);
LocalDateTime ldt = LocalDate.now().atTime(0,0);

//下周二上午10点整
ldt = ldt.plusWeeks(1).with(ChronoField.DAY_OF_WEEK,2).with(ChronoField.MILLI_OF_DAY,0).withHour(10);

Temporal接口

专门的TemporalAdjusters类提供了TemporalAdjuster的实现

LocalDate ld = LocalDate.now();
LocalDateTime ldt = ld.with(TemporalAdjusters.next( DayOfWeek.TUESDAY)).atTime(10, 0);

public static TemporalAdjuster next(DayOfWeek dayOfWeek){
	int dowValue = dayOfWeek.getValue();
	return(temporal)->{
		int calDow = temporal.get(DAY_OF_WEEK);
		int daysDiff = calDow - dowValue;
		return temporal.plus(daysDiff >= 0?7 - daysDiff:-daysDiff,DAYS);
	};
}
//明天最后一刻
LocalDateTime ldt = LocalDateTime.of(LocalDate.now().plusDays(1),LocalTime.MAX);

LocalDateTime ldt = LocalTime.MAX.atDate(LocalDate.now().plusDays());

//本月最后一天的最后一刻
LocalDateTime ldt = LocalDate.now().with(TemporalAdjusters.lastDayOfMonth()).atTime(LocalTime.MAX);

public static TemporalAdjuster lastDayOfMonth(){
	return (temporal) -> temporal.with(DAY_OF_MONTH,temporal.range(DAY_OF_NONTH).getMaximum());
}
//使用range方法,从返回值获取对应日历单位的最大值最小值
long maxDayOfMonth = LocalDate.now().range(ChronoField.DAY_OF_MONTH).getMaximum();
LocalDateTime ldt = LocalDate.now().withDayOfMonth((int)maxDayOfMonth).atTime(LocalTime.MAX);

//下个月第一个周一的下午5点整
LocalDateTime ldt = LocalDate.now().plusMonths()
	.with(TemporalAdjusters.firstInMonth(DayOfWeek.MONDAY)).atTime(17,0);

格式化

DateTimeFormatter formatter = DDateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime ldt = LocalDateTime.of(2016,8,18,14,20,45);
System.out.println(formatter.format(ldt));

//将字符串转换为日期和时间对象
DateTimeFormatter formatter = DateTimeFormatter.ofParrern("yyyy-MM-dd HH:mm:ss");
String str = "2016-08-18 14:20:15";
LocalDateTime ldt = LocalDateTime.parse(str,formatter);

时间段的计算

  • Period 表示年月日时间差
  • Duration 表示时分秒的时间差
LocalDate ld1 = LocalDate.of(2016,3,24);
LocalDate ld2 = LocalDate.of(2017,7,12);
Period period = Period.between(ld1,ld2);
System.out.println(period.getYeals() + "年" +period.getMonths()+"月"+period.getDays()+"天");

//根据生日计算年龄
LocalDate born = LocalDate.of(1990,06,20);
int year = Period.between(born,LocalDate.now()).getYears();

//计算迟到分钟数
long lateMinutes = Duration.between(LocalTime.of(9,0),LocalTime.now()).toMinutes();

与Date/Calendar对象的转换

//将LocalDateTime按默认时区转换为Date
public static Date toDate(LocalDateTime ldt){
	return new Date(ldt.atZone(ZoneId.systemDefault())).toInstant().toEpochMilli();
}

//将ZonedDateTime转换为Calendar
public static Calendar toCalendar(ZonedDateTime zdt){
	TimeZone tz = TimeZone.gettTimeZone(zdt.getZone());
	Calendar calendar = Calendar.getInstance(tz);
	calendar.setTimeInMillis(zdt.toInstant().toEpochMilli());
	return calendar;
}

//将Date按默认时区转换为LocalDateTime
pubic static LocalDateTime toLocalDateTime(Date date){
	return LocalDateTime.ofInstant(Instant.ofEpochMilli(date.getTime()),ZoneId.systemDefault());
}

//将Calendar转换为ZonedDateTime
public static ZonedDateTime toZoneDateTime(Calendar calendar){
	
	ZonedDateTime zdt = ZoneDateTime.ofInstant(Instant.ofEpochMilli(calendar.getTimeInMillis()),calendar.getTimeZone().toZoneId());
	return zdt;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值