一举畸、JAVA8的LocalDateTime
最近寫自動化用例遇到一個時間問題。獲取當年開始時間和結束時間姑原。
從上網搜索了使用Calendar方式獲攘胙:
public StringgetYearStart(){//當年開始時間戳,毫秒
? ? Calendar calendar = Calendar.getInstance();// 獲取當前日期
? ? calendar.add(Calendar.YEAR, 0);
? ? calendar.add(Calendar.MONTH, 0);
? ? calendar.add(Calendar.DATE, 0);
? ? calendar.set(Calendar.DAY_OF_YEAR, 1);
? ? calendar.set(Calendar.HOUR, -12);
? ? calendar.set(Calendar.MINUTE, 0);
? ? calendar.set(Calendar.SECOND, 0);
? ? Long time = calendar.getTimeInMillis();
? ? return String.valueOf(time);
}
//-------------------------------------------------------
public StringgetYearEnd(){//當年結束時間戳零截,毫秒
? ? Calendar calendar = Calendar.getInstance();// 獲取當前日期
? ? calendar.add(Calendar.YEAR, 1);
? ? calendar.add(Calendar.DATE, 0);
? ? calendar.add(Calendar.MONTH, 0);
? ? calendar.set(Calendar.DAY_OF_YEAR, -1);
? ? calendar.set(Calendar.HOUR, 23);
? ? calendar.set(Calendar.MINUTE, 59);
? ? calendar.set(Calendar.SECOND, 59);
? ? calendar.set(Calendar.MILLISECOND, 999);
? ? Long time = calendar.getTimeInMillis();
? ? return String.valueOf(time);
}
但這個方法有個問題麸塞,中午12點前執(zhí)行和12點后執(zhí)行,返回的時間不一樣涧衙,差12個小時哪工。
12點前執(zhí)行,返回:1577764800248(2019/12/31 12:0:0)弧哎、 1609343999999(2020/12/30 23:59:59)
12點后執(zhí)行雁比,返回:1577808000000(2020/1/1 0:0:0)、 1609430399999(2020/12/31 23:59:59)
解決辦法:使用java8的LocalDate和LocalDateTime方式:
/**
* @Description:當天的開始時間
* @Param: [today, isFirst: true 表示開始時間傻铣,false表示結束時間]
*/
public static StringgetStartOrEndDayOfDay(LocalDate today, Boolean isFirst){
LocalDate resDate = LocalDate.now();
? ? if (today ==null) {
today = resDate;
? ? }
if(isFirst){
return String.valueOf(LocalDateTime.of(today, LocalTime.MIN).toInstant(ZoneOffset.ofHours(8)).toEpochMilli());
? ? }else{
return String.valueOf(LocalDateTime.of(today, LocalTime.MAX).toInstant(ZoneOffset.ofHours(8)).toEpochMilli());
? ? }
}
/**
* @Description:本周的開始時間
* @Param: [today, isFirst: true 表示開始時間章贞,false表示結束時間]
*/
public static StringgetStartOrEndDayOfWeek(LocalDate today, Boolean isFirst){
String time =MinTime;
? ? LocalDate resDate = LocalDate.now();
? ? if (today ==null) {
today = resDate;
? ? }
DayOfWeek week = today.getDayOfWeek();
? ? int value = week.getValue();
? ? if (isFirst) {
resDate = today.minusDays(value -1);
? ? }else {
resDate = today.plusDays(7 - value);
? ? ? ? time =MaxTime;
? ? }
LocalDateTime localDateTime = LocalDateTime.parse(resDate.toString() + time);
? ? return String.valueOf(localDateTime.toInstant(ZoneOffset.ofHours(8)).toEpochMilli());
}
/**
* @Description:本月的開始時間
* @Param: [today, isFirst: true 表示開始時間,false表示結束時間]
*/
public static StringgetStartOrEndDayOfMonth(LocalDate today, Boolean isFirst){
String time =MinTime;
? ? LocalDate resDate = LocalDate.now();
? ? if (today ==null) {
today = resDate;
? ? }
Month month = today.getMonth();
? ? int length = month.length(today.isLeapYear());
? ? if (isFirst) {
resDate = LocalDate.of(today.getYear(), month, 1);
? ? }else {
resDate = LocalDate.of(today.getYear(), month, length);
? ? ? ? time =MaxTime;
? ? }
LocalDateTime localDateTime = LocalDateTime.parse(resDate.toString() + time);
? ? return String.valueOf(localDateTime.toInstant(ZoneOffset.ofHours(8)).toEpochMilli());
}
/**
* @Description:本季度的開始時間
* @Param: [today, isFirst: true 表示開始時間非洲,false表示結束時間]
*/
public static StringgetStartOrEndDayOfQuarter(LocalDate today, Boolean isFirst){
String time =MinTime;
? ? LocalDate resDate = LocalDate.now();
? ? if (today ==null) {
today = resDate;
? ? }
Month month = today.getMonth();
? ? Month firstMonthOfQuarter = month.firstMonthOfQuarter();
? ? Month endMonthOfQuarter = Month.of(firstMonthOfQuarter.getValue() +2);
? ? if (isFirst) {
resDate = LocalDate.of(today.getYear(), firstMonthOfQuarter, 1);
? ? }else {
resDate = LocalDate.of(today.getYear(), endMonthOfQuarter, endMonthOfQuarter.length(today.isLeapYear()));
? ? ? ? time =MaxTime;
? ? }
LocalDateTime localDateTime = LocalDateTime.parse(resDate.toString() + time);
? ? return String.valueOf(localDateTime.toInstant(ZoneOffset.ofHours(8)).toEpochMilli());
}
/**
* @Description:本年度的開始時間
* @Param: [today, isFirst: true 表示開始時間鸭限,false表示結束時間]
*/
public static StringgetStartOrEndDayOfYear(LocalDate today, Boolean isFirst){
String time =MinTime;
? ? LocalDate resDate = LocalDate.now();
? ? if (today ==null) {
today = resDate;
? ? }
if (isFirst) {
resDate = LocalDate.of(today.getYear(), Month.JANUARY, 1);
? ? }else {
resDate = LocalDate.of(today.getYear(), Month.DECEMBER, Month.DECEMBER.length(today.isLeapYear()));
? ? ? ? time =MaxTime;
? ? }
LocalDateTime localDateTime = LocalDateTime.parse(resDate.toString() + time);
? ? return String.valueOf(localDateTime.toInstant(ZoneOffset.ofHours(8)).toEpochMilli());
}
/**
* @Description:上個月的開始時間
* @Param: [today, isFirst: true 表示開始時間,false表示結束時間]
*/
public static StringgetStartOrEndDayOfLastMonth(LocalDate today, Boolean isFirst){
String time =MinTime;
? ? LocalDate resDate = LocalDate.now();
? ? if (today ==null) {
today = resDate;
? ? }
today = today.minusMonths(1);
? ? Month month = today.getMonth();
? ? int length = month.length(today.isLeapYear());
? ? if (isFirst) {
resDate = LocalDate.of(today.getYear(), month, 1);
? ? }else {
resDate = LocalDate.of(today.getYear(), month, length);
? ? ? ? time =MaxTime;
? ? }
LocalDateTime localDateTime = LocalDateTime.parse(resDate.toString() + time);
? ? return String.valueOf(localDateTime.toInstant(ZoneOffset.ofHours(8)).toEpochMilli());
}
/**
* @Description:當天往前30天的開始和結束時間
* @Param: [today, isFirst: true 表示開始時間两踏,false表示結束時間]
*/
public static StringgetBefore30Days(LocalDate today, Boolean isFirst){
LocalDate resDate = LocalDate.now();
? ? if (today ==null) {
today = resDate;
? ? }
today = today.minusDays(30);
? ? if(isFirst){
return String.valueOf(LocalDateTime.of(today, LocalTime.MIN).toInstant(ZoneOffset.ofHours(8)).toEpochMilli());
? ? }else{
return String.valueOf(LocalDateTime.of(today, LocalTime.MAX).toInstant(ZoneOffset.ofHours(8)).toEpochMilli());
? ? }
}
/**
* @Description:當前往前12個月的開始時間-1號
* @Param: [today, isFirst: true 表示開始時間败京,false表示結束時間]
*/
public static StringgetBefore12Months(LocalDate today, Boolean isFirst){
String time =MinTime;
? ? LocalDate resDate = LocalDate.now();
? ? if (today ==null) {
today = resDate;
? ? }
today = today.minusMonths(12);
? ? Month month = today.getMonth();
? ? int length = month.length(today.isLeapYear());
? ? if (isFirst) {
resDate = LocalDate.of(today.getYear(), month, 1);
? ? }else {
resDate = LocalDate.of(today.getYear(), month, length);
? ? ? ? time =MaxTime;
? ? }
LocalDateTime localDateTime = LocalDateTime.parse(resDate.toString() + time);
? ? return String.valueOf(localDateTime.toInstant(ZoneOffset.ofHours(8)).toEpochMilli());
}
/**
* @Description:當前往前6年的開始時間
* @Param: [today, isFirst: true 表示開始時間,false表示結束時間]
*/
public static StringgetBefore6years(LocalDate today, Boolean isFirst){
String time =MinTime;
? ? LocalDate resDate = LocalDate.now();
? ? if (today ==null) {
today = resDate;
? ? }
today = today.minusYears(6);
? ? if (isFirst) {
resDate = LocalDate.of(today.getYear(), Month.JANUARY, 1);
? ? }else {
resDate = LocalDate.of(today.getYear(), Month.DECEMBER, Month.DECEMBER.length(today.isLeapYear()));
? ? ? ? time =MaxTime;
? ? }
LocalDateTime localDateTime = LocalDateTime.parse(resDate.toString() + time);
? ? return String.valueOf(localDateTime.toInstant(ZoneOffset.ofHours(8)).toEpochMilli());
}
二梦染、static關鍵字
在《Java編程思想》P86頁有這樣一段話:
“static方法就是沒有this的方法赡麦。在static方法內部不能調用非靜態(tài)方法朴皆,反過來是可以的。而且可以在沒有創(chuàng)建任何對象的前提下泛粹,僅僅通過類本身來調用static方法遂铡。這實際上正是static方法的主要用途【фⅲ”
這篇文章分析的特別好扒接,可仔細閱讀一遍:https://www.cnblogs.com/dolphin0520/p/3799052.html