Java 8 之 新日期时间API

Java 8 之 新日期时间API

1.系统时间

1.LocalDate date = LocalDate.now();
 System.out.println(date.getYear() + "/" + date.getMonthValue() + "/" + date.getDayOfMonth()); // 2020/3/13  

2.LocalTime time = LocalTime.now();  
System.out.println(time.getHour() + ":" + time.getMinute() + ":" + time.getSecond()); // 10:47:22  
// ***没有提供  getMillis() 方法  
System.out.println(time.get(ChronoField.MILLI_OF_SECOND)); // 859  
 
3.LocalDateTime dateTime = LocalDateTime.now();  
System.out.println(dateTime.getYear() + "/" + dateTime.getMonthValue() + "/" + dateTime.getDayOfMonth()+ " " + dateTime.getHour() + ":" + dateTime.getMinute() + ":" + dateTime.getSecond()); // 2020/3/13  10:47:22    
 
4.Clock clock = Clock.systemDefaultZone();  
System.out.println(clock.millis()); // 这个函数是用于获得时间戳的,从1970年到现在的毫秒数

2.接下来的时间格式转换就是围绕3个函数来转,LocalDate , LocalTime , 以及LocalDateTime,很简单容易记

package cn.ycl.test;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
public class test2 {
	public static void main(String[] args) {// 2020,3,13
		// TODO Auto-generated method stub
		// 将日期字符串手动转换为日期格式
		LocalDate myDate = LocalDate.of(2020, 3, 13);
		System.out.println(myDate);// 2020-03-13
		System.out.println(myDate.getYear() + "/" + myDate.getMonthValue() + "/" + myDate.getDayOfMonth()); // 2020/3/13
		
		//获得特定时间的星期
		LocalDate independenceDay = LocalDate.of(2020, 3, 13);
		DayOfWeek dayOfWeek = independenceDay.getDayOfWeek();
		System.out.println(dayOfWeek); // FRIDAY
        
		// 将时间字符串手动转换为时间格式
		LocalTime myTime = LocalTime.of(10, 30, 45);
		System.out.println(myTime.getHour() + ":" + myTime.getMinute() + ":" + myTime.getSecond()); // 10:30:45
		// 将时间日期字符串手动转换为时间日期格式	
		LocalDateTime myDateTime1 = LocalDateTime.of(2020, 3, 13, 10, 30, 45);
		System.out.println(myDateTime1);//2020-03-13T10:30:45,直接输出的话会多了一个T,很奇怪
		System.out.println(myDateTime1.getYear() + "/" + myDateTime1.getMonthValue() + "/" + myDateTime1.getDayOfMonth()
				+ " " + myDateTime1.getHour() + ":" + myDateTime1.getMinute() + ":" + myDateTime1.getSecond()); 
		// 2020/3/13 10:30:45
		
		LocalDateTime myDateTime2 = LocalDateTime.of(myDate, myTime);
		System.out.println(myDateTime2);//2020-03-13T10:30:45
		System.out.println(myDateTime2.getYear() + "/" + myDateTime2.getMonthValue() + "/" + myDateTime2.getDayOfMonth()
				+ " " + myDateTime2.getHour() + ":" + myDateTime2.getMinute() + ":" + myDateTime2.getSecond()); // 2020/3/13 10:30:45
	}
}

3.格式化(感觉这部分应该会用的挺多的)

Java代码

1.// Date -> String  
LocalDate formatDate1 = LocalDate.of(2013, 12, 4);  
String dateString = formatDate1.format(DateTimeFormatter.ofPattern("yyyy/MM/dd"));  
System.out.println(dateString); // 2013/12/04  
 
2.// String -> Date  //感觉这部分应该会用的挺多的
LocalDate formatDate2 = LocalDate.parse("2013/12/04", DateTimeFormatter.ofPattern("yyyy/MM/dd"));  
System.out.println(formatDate2); // 2013-12-04  

4.日期转换

Java代码
1.// LocalDate -> LocalDateTime

LocalDate changeDate1 = LocalDate.of(2013, 12, 4);  
LocalDateTime changeDateTime1 = changeDate1.atTime(10, 30, 45);  
System.out.println(changeDateTime1); // 2013-12-04T10:30:45  
   
2.// LocalTime -> LocalDateTime  
LocalTime time2 = LocalTime.of(10, 30, 45);  
LocalDateTime dateTime2 = time2.atDate(LocalDate.of(2013, 12, 4));  
System.out.println(dateTime2); // 2013-12-04T10:30:45  
   
3.// LocalDateTime -> LocalDate, LocalTime  
LocalDateTime dateTime3 = LocalDateTime.of(2013, 12, 4, 10, 30, 45);  
LocalDate date3 = dateTime3.toLocalDate();  
System.out.println(date3); // 2013-12-04  
LocalTime time3 = dateTime3.toLocalTime();  
System.out.println(time3); // 10:30:45  

5.日期加减(感觉这部分应该会用的挺多的)

  
LocalDate today = LocalDate.now();  

LocalDate twoDaysAfter = today.plusDays(2L); // 2天后  

LocalDate threeDaysBefore = today.minusDays(3L); // 3天前  

LocalDate oneYearsAfter = today.plus(1L, ChronoUnit.YEARS);// 1年后  

LocalDate twoWeeksBefore = today.minus(2L, ChronoUnit.WEEKS); // 2周前  

LocalDate newDate = LocalDate.now().plus(Period.of(3, 2, 1)); // 3年2月1天后  

6.计算间隔(感觉这部分应该会用的挺多的)

LocalDateTime d1 = LocalDateTime.of(2013, 11, 4, 10, 30, 45);  
LocalDateTime d2 = LocalDateTime.of(2013, 12, 4, 10, 30, 45);  
Duration duration = Duration.between(d1, d2);  
System.out.println(duration.toDays()); // 30  

7.日期比较(感觉这部分应该会用的挺多的)

LocalDate compDate1 = LocalDate.of(2013, 12, 4);  
LocalDate compDate2 = LocalDate.of(2013, 11, 4);  
System.out.println(compDate1.isBefore(compDate2)); // false  
System.out.println(compDate1.compareTo(compDate2)); // 1  

8.和java.util.Date的转换

1.// LocalDateTime -> Date  
LocalDateTime cDateTime = LocalDateTime.now();  
Instant instant = cDateTime.atZone(ZoneId.systemDefault()).toInstant();  
Date cDate = Date.from(instant);  
System.out.println(cDate); // Fri Mar 21 16:39:22 CST 2014  
2.// Date -> LocalDateTime  
Date date2 = new Date();  
Instant instant2 = date2.toInstant();  
LocalDateTime dateTimeFromDate = LocalDateTime.ofInstant(instant2, ZoneOffset.systemDefault());  
System.out.println(dateTimeFromDate); // 2014-03-21T16:39:22.890  
// Calendar -> LocalDateTime  
Calendar cal = Calendar.getInstance();  
LocalDateTime dateTimeFromCal = LocalDateTime.ofInstant(cal.toInstant(), ZoneOffset.systemDefault());  
System.out.println(dateTimeFromCal); // 2014-03-21T16:39:22.890  

此博客大部分是借鉴了:https://www.iteye.com/blog/rensanning-2034622

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

三七有脾气

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值