在实践中学习java(1)--JAVA8的LocalDateTi

2020-11-12  本文已影响0人  爱吃饭的小芒果

一、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点前执行和12点后执行,返回的时间不一样,差12个小时。

12点前执行,返回:1577764800248(2019/12/31 12:0:0)、 1609343999999(2020/12/30 23:59:59)

12点后执行,返回: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方法内部不能调用非静态方法,反过来是可以的。而且可以在没有创建任何对象的前提下,仅仅通过类本身来调用static方法。这实际上正是static方法的主要用途。”

这篇文章分析的特别好,可仔细阅读一遍:https://www.cnblogs.com/dolphin0520/p/3799052.html

上一篇下一篇

猜你喜欢

热点阅读