jdk8获取当前时间|时间加减|java8时间格式化|时间处理工具|时间比较|线程安全的时间处理方法
目錄
前言
一、jdk8與jdk7以及之前的日期和時(shí)間處理類的不同:
二、Java 8日期/時(shí)間類
三:日期和時(shí)間主要類的關(guān)系(待更新)
四:日期操作和處理
獲取當(dāng)前日期(只能精確到年月日)
獲取當(dāng)前時(shí)間(可以精確到毫秒)
獲取上周周一的日期
獲取具體年、月、日、小時(shí)、分鐘、秒
指定日期、時(shí)間
判斷兩個(gè)日期是否相等
計(jì)算幾年后(前)、幾月后(前)、幾天后(前)等的日期
判斷指定月份有多少天
計(jì)算兩個(gè)日期之間相差月數(shù)、天數(shù)、分鐘數(shù)
前言
在很久之前,我總結(jié)了一些jdk7版本之前的關(guān)于時(shí)間處理的一些公共方法,日期轉(zhuǎn)換成字符串、指定時(shí)間加上指定天數(shù)后的日期、獲取上周周一時(shí)間等等;具體的可以戳鏈接查看完整的:https://blog.csdn.net/qq_27471405/article/details/79523556
但是這些是非線程安全的,不建議采用,舉個(gè)例子
在一個(gè)類中,有以下代碼:
private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public String getDate(Date date){
return sdf.format(date);
}
上面這串代碼在并發(fā)的時(shí)候,是線程不安全的,具體的如何不安全,大家可以搜一下,這里不多講了
那么今天給大家分享的是jdk8之后的一些時(shí)間處理的公共方法,是線程安全的,理應(yīng)大家以后應(yīng)該用下面這些方法
一、jdk8與jdk7以及之前的日期和時(shí)間處理類的不同:
1. Java的java.util.Date和java.util.Calendar類易用性差,不支持時(shí)區(qū),并且是可變的,也就意味著他們都不是線程安全的;
2. 用于格式化日期的類DateFormat被放在java.text包中,它是一個(gè)抽象類,所以我們需要實(shí)例化一個(gè)SimpleDateFormat對(duì)象來(lái)處理日期格式化,并且DateFormat也是非線程安全,這意味著如果你在多線程程序中調(diào)用同一個(gè)DateFormat對(duì)象,會(huì)得到意想不到的結(jié)果。
3. 對(duì)日期的計(jì)算方式繁瑣,而且容易出錯(cuò),因?yàn)樵路菔菑?開(kāi)始的,這意味著從Calendar中獲取的月份需要加一才能表示當(dāng)前月份
由于以上這些問(wèn)題,出現(xiàn)了一些三方的日期處理框架,例如Joda-Time,data4j等開(kāi)源項(xiàng)目
二、Java 8日期/時(shí)間類
Java 8的日期和時(shí)間類包含LocalDate、LocalTime、Instant、Duration以及Period,這些類都包含在java.time包中。
Instant:瞬時(shí)實(shí)例。
LocalDate:本地日期,不包含具體時(shí)間 例如:2014-01-14 可以用來(lái)記錄生日、紀(jì)念日、加盟日等。
LocalTime:本地時(shí)間,不包含日期。
LocalDateTime:組合了日期和時(shí)間,但不包含時(shí)差和時(shí)區(qū)信息。
ZonedDateTime:最完整的日期時(shí)間,包含時(shí)區(qū)和相對(duì)UTC或格林威治的時(shí)差。
新API還引入了 ZoneOffSet 和 ZoneId 類,使得解決時(shí)區(qū)問(wèn)題更為簡(jiǎn)便。解析、格式化時(shí)間的 DateTimeFormatter 類也全部重新設(shè)計(jì)。
三:日期和時(shí)間主要類的關(guān)系(待更新)
1 、LocalDate的關(guān)系圖:
2、 LocalTime:
3 、LocalDateTime:
4 、OffsetTime:
5 、OffsetDateTime:
6、 ZonedDateTime:
7 、Instant:
四:日期操作和處理
獲取當(dāng)前日期(只能精確到年月日)
/**
* 獲取當(dāng)前日期(只能精確到年月日)
* @param formatStr
*/
public static void getNowDate(String formatStr){
if (StringUtils.isBlank(formatStr)){
formatStr = "yyyy-MM-dd";
}
LocalDate now = LocalDate.now();
System.out.println("當(dāng)前日期: " + now + " " + now.getDayOfWeek());
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(formatStr); // * 原文章鏈接:https://blog.csdn.net/qq_27471405/article/details/106824023 * 其他均為盜版,公眾號(hào):靈兒的筆記(zygxsq)
String nowFormat = now.format(dateTimeFormatter);
System.out.println("格式化后的當(dāng)前日期:"+nowFormat);
}
如果傳格式化到天小時(shí)秒的話,會(huì)報(bào)異常:Exception in thread "main" java.time.temporal.UnsupportedTemporalTypeException: Unsupported field: HourOfDay
獲取當(dāng)前時(shí)間(可以精確到毫秒)
/**
* 獲取當(dāng)前時(shí)間(可以精確到毫秒)
* 原文章鏈接:https://blog.csdn.net/qq_27471405/article/details/106824023
* 其他均為盜版,公眾號(hào):靈兒的筆記(zygxsq)
* @param formatStr
*/
public static void getNowTime(String formatStr){
if (StringUtils.isBlank(formatStr)){
formatStr = "yyyy-MM-dd";
}
LocalDateTime now = LocalDateTime.now();
System.out.println("當(dāng)前日期: " + now + " " + now.getDayOfWeek());
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(formatStr); // * 原文章鏈接:https://blog.csdn.net/qq_27471405/article/details/106824023 * 其他均為盜版,公眾號(hào):靈兒的筆記(zygxsq)
String nowFormat = now.format(dateTimeFormatter);
System.out.println("格式化后的當(dāng)前日期:"+nowFormat);
}
獲取上周周一的日期
/**
* 獲取上周周一的日期
* 原文章鏈接:https://blog.csdn.net/qq_27471405/article/details/106824023
* 其他均為盜版,公眾號(hào):靈兒的筆記(zygxsq)
*/
public static void getLastMonday(){
LocalDate now = LocalDate.now();
System.out.println("當(dāng)前日期: " + now + " " + now.getDayOfWeek());
LocalDate todayOfLastWeek = now.minusDays(7);
LocalDate last_monday = todayOfLastWeek.with(TemporalAdjusters.previous(DayOfWeek.SUNDAY)).plusDays(1); // * 原文章鏈接:https://blog.csdn.net/qq_27471405/article/details/106824023 * 其他均為盜版,公眾號(hào):靈兒的筆記(zygxsq)
System.out.println("上周周一日期:"+last_monday);
}
獲取具體年、月、日、小時(shí)、分鐘、秒
/**
* 獲取具體年、月、日、小時(shí)、分鐘、秒
* @param formatStr
*/
public static void getDetailTime(String formatStr){
LocalDateTime now = LocalDateTime.now();
System.out.println("當(dāng)前日期: " + now + " " + now.getDayOfWeek());
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(formatStr);
String nowFormat = now.format(dateTimeFormatter);
System.out.println("格式化后的當(dāng)前日期:"+nowFormat);
int year = now.getYear();
int month = now.getMonthValue();
int day = now.getDayOfMonth();
int hour = now.getHour();
int minute = now.getMinute();
int second = now.getSecond();
int nano = now.getNano();
System.out.printf("年 : %d 月 : %d 日 : %d 小時(shí):%d 分鐘:%d 秒:%d 毫秒:%d %n", year, month, day,hour,minute,second,nano); // * 原文章鏈接:https://blog.csdn.net/qq_27471405/article/details/106824023 * 其他均為盜版,公眾號(hào):靈兒的筆記(zygxsq)
}
指定日期、時(shí)間
/**
* 指定日期、時(shí)間
* 原文章鏈接:https://blog.csdn.net/qq_27471405/article/details/106824023
* 其他均為盜版,公眾號(hào):靈兒的筆記(zygxsq)
* @param formatStr
*/
public static void createTime(String formatStr){
LocalDate date = LocalDate.of(2020, 04, 27);
System.out.println("指定日期: " + date);
LocalDateTime time = LocalDateTime.of(2020, 04, 27,06,10,50);
System.out.println("指定時(shí)間: " + time);
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(formatStr);
String nowFormat = time.format(dateTimeFormatter);
System.out.println("格式化后的指定時(shí)間:"+nowFormat);
}
判斷兩個(gè)日期是否相等
/**
* 判斷兩個(gè)日期是否相等、之前、之后
* 原文章鏈接:https://blog.csdn.net/qq_27471405/article/details/106824023
* 其他均為盜版,公眾號(hào):靈兒的筆記(zygxsq)
*/
public static void compareDate(){
LocalDate now = LocalDate.now();
System.out.println("當(dāng)前時(shí)間: " + now + " " + now.getDayOfWeek());
LocalDate date1 = LocalDate.of(2020, 04, 27);
LocalDate date2 = LocalDate.of(2020, 04, 27);
LocalDate date3 = LocalDate.of(2020, 04, 28);
boolean equal = now.isEqual(date1);
System.out.printf("是否是同一時(shí)間:%s ", date1.equals(now));
System.out.printf("是否是同一時(shí)間:%s ", now.isEqual(date1));
System.out.println();
System.out.printf("是否是同一時(shí)間:%s ", date1.equals(date2));
System.out.printf("是否是同一時(shí)間:%s ", date1.isEqual(date2));
System.out.println();
System.out.println("data2(2020.4.27)是否比data3(2020.4.28)小: "+date2.isBefore(date3)); * 原文章鏈接:https://blog.csdn.net/qq_27471405/article/details/106824023 * 其他均為盜版,公眾號(hào):靈兒的筆記(zygxsq)
System.out.println("data2(2020.4.27)是否比data3(2020.4.28)大: "+date2.isAfter(date3));
}
計(jì)算幾年后(前)、幾月后(前)、幾天后(前)等的日期
/**
* 計(jì)算幾年后(前)、幾月后(前)、幾天后(前)等的日期
* 原文章鏈接:https://blog.csdn.net/qq_27471405/article/details/106824023
* 其他均為盜版,公眾號(hào):靈兒的筆記(zygxsq)
* @param formatStr
*/
public static void calculateTime(String formatStr){
LocalDateTime now = LocalDateTime.now();
LocalDateTime newTime = now.plusHours(6);
System.out.println("當(dāng)前時(shí)間: " + now + " " + now.getDayOfWeek());
System.out.println("6小時(shí)后的時(shí)間: " + newTime);
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(formatStr);
String nowFormat = now.format(dateTimeFormatter);
String newFormat = newTime.format(dateTimeFormatter);
System.out.println("格式化后的當(dāng)前時(shí)間:"+nowFormat);
System.out.println("格式化后的6小時(shí)后的時(shí)間:"+newFormat);
LocalDateTime twoYearsLater = now.plusYears(2);
String twoYearsFormat = twoYearsLater.format(dateTimeFormatter);
System.out.println("2年后的時(shí)間:"+twoYearsFormat);
LocalDateTime twoMonthsLater = now.plusMonths(2);
String twoMonthsFormat = twoMonthsLater.format(dateTimeFormatter);
System.out.println("2個(gè)月后的時(shí)間:"+twoMonthsFormat);
LocalDateTime twoWeeksLater = now.plusWeeks(2);
String twoWeeksFormat = twoWeeksLater.format(dateTimeFormatter);
System.out.println("2周后的時(shí)間:"+twoWeeksFormat);
LocalDateTime twoDaysLater = now.plusDays(2);
String twoDaysFormat = twoDaysLater.format(dateTimeFormatter);
System.out.println("2天后的時(shí)間:"+twoDaysFormat);
LocalDateTime twoMinutesLater = now.plusMinutes(2);
String twoMinutesFormat = twoMinutesLater.format(dateTimeFormatter);
System.out.println("2分鐘后的時(shí)間:"+twoMinutesFormat);
LocalDateTime twoMinutesBefore = now.plusMinutes(-2);
String twoMinutesBeforeFormat = twoMinutesBefore.format(dateTimeFormatter);
System.out.println("2分鐘前的時(shí)間:"+twoMinutesBeforeFormat);
//原文章鏈接:https://blog.csdn.net/qq_27471405/article/details/106824023
//其他均為盜版,公眾號(hào):靈兒的筆記(zygxsq)
//還可以直接通過(guò)plus方法計(jì)算 幾年(月周天)后
LocalDateTime twoYearsPlusLater = now.plus(2, ChronoUnit.YEARS);
String twoYearsPlusLaterFormat = twoYearsPlusLater.format(dateTimeFormatter);
System.out.println("2年后的時(shí)間:"+twoYearsPlusLaterFormat);
//負(fù)號(hào)表示 之前
LocalDateTime twoDaysPlusBefore = now.plus(-2, ChronoUnit.DAYS);
String twoDaysPlusBeforeFormat = twoDaysPlusBefore.format(dateTimeFormatter);
System.out.println("2天前的時(shí)間:"+twoDaysPlusBeforeFormat);
//也可以用minus,也表示之前
LocalDateTime twoDaysMinusBefore = now.minus(2, ChronoUnit.DAYS);
String twoDaysMinusBeforeFormat = twoDaysMinusBefore.format(dateTimeFormatter);
System.out.println("2天前的時(shí)間:"+twoDaysMinusBeforeFormat);
}
判斷指定月份有多少天
/**
* 判斷指定月份有多少天
*/
public static void getMonthDays(){
YearMonth currentYearMonth = YearMonth.now();
System.out.println("當(dāng)前時(shí)間:"+currentYearMonth);
System.out.println("當(dāng)前月份有多少天:"+currentYearMonth.lengthOfMonth());
YearMonth february = YearMonth.of(2020, Month.FEBRUARY);
System.out.println("指定時(shí)間的月份2月:"+february);
System.out.println("指定時(shí)間的月份2月有多少天:"+february.lengthOfMonth());
}
計(jì)算兩個(gè)日期之間相差月數(shù)、天數(shù)、分鐘數(shù)
/**
* 計(jì)算兩個(gè)日期之間相差月數(shù)、天數(shù)、分鐘數(shù)
* 原文章鏈接:https://blog.csdn.net/qq_27471405/article/details/106824023
* 其他均為盜版,公眾號(hào):靈兒的筆記(zygxsq)
*/
public static void getDaysBetweenTwoDate(){
LocalDate startDate = LocalDate.of(2020, 04, 27);
LocalDate endDate = LocalDate.of(2020, 07, 2);
long months = startDate.until(endDate, ChronoUnit.MONTHS);
long days = startDate.until(endDate, ChronoUnit.DAYS);
System.out.println("startDate(2020.04.27)和endDate(2020.07.02)相差月數(shù):"+months);
System.out.println("startDate(2020.04.27)和endDate(2020.07.02)相差天數(shù):"+days);
LocalDateTime startTime = LocalDateTime.of(2020, 04, 27,18,20,10);
LocalDateTime endTime = LocalDateTime.of(2020, 04, 27,18,30,12);
long minutes = startTime.until(endTime, ChronoUnit.MINUTES);
System.out.println("startTime(2020.04.27 18:20:10)和endTime(2020.04.27 18:30:20)相差分鐘數(shù):"+minutes); // * 原文章鏈接https://blog.csdn.net/qq_27471405/article/details/106824023 * 其他均為盜版,公眾號(hào):靈兒的筆記(zygxsq)
}
參考文章
https://blog.csdn.net/u012091080/article/details/79901830
https://blog.csdn.net/chenleixing/article/details/44408875
https://blog.csdn.net/feicongcong/article/details/78224494
感謝原作者的分享,讓技術(shù)人能夠更快的解決問(wèn)題
我的博客即將同步至騰訊云+社區(qū),邀請(qǐng)大家一同入駐:https://cloud.tencent.com/developer/support-plan?invite_code=lnlh8qa6e7an
總結(jié)
以上是生活随笔為你收集整理的jdk8获取当前时间|时间加减|java8时间格式化|时间处理工具|时间比较|线程安全的时间处理方法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: linux终端中使用ctrl+c和ctr
- 下一篇: Linux建立虚拟ip的方法