让你快速了解LocalDate类的基本用法

2019-10-16 11:35:40 浏览数 (2)

Date:用来表示时间点

LocalDate:用来表示大家熟悉的日历表示法

LocalDate是带有年,月,日的日期。为了构建LocalDate对象,可以使用now或of静态方法栗子如下:

代码语言:javascript复制
LocalDate today = LocalDate.now();
LocalDate birthday = LocalDate.of(1997, 8, 17);

看LocalDate源码中的注释发现

代码语言:javascript复制
 * @implSpec
 * This class is immutable and thread-safe.
 //该类是不可变的,并且是线程安全的。

说明LocalDate类是线程安全的

下面介绍LocalDate对象的方法

方法

描述

now,of

这些静态方法会构建一个LocalDate,要么从当前时间构建,要么从给定的年月日构建

plusDays,plusWeeks,plusMonths,plusYears

在当前的LocalDate上加上一定量的天,星期,月或年

minusDays,minusWeeks,minusMonths,minusYears

在当前的LocalDate上减去一定量的天,星期,月或年

plus,minus

加上或减去一个Duration或Period

withDayOfMonth,withDayOfYear,withMonth,withYear

返回一个新的LocalDate,其月的日期,年的日期,月或年修改为给定的值

getDayOfMonth

获取月的日期(在1到31之间)

getDayOfYear

获取年的日期(在1到366之间)

getDayOfWeek

获取星期日期,返回DayOfWeek枚举值

getMonth,getMonthValue

获取月份的Month枚举,或者是1 ~ 12之间的数字

getYear

获取年份,在-999 999 999 到 999 999 999之间

until

获取Period,或者两个日期之间按照给定的ChronoUnits计算的数值;计算了两个日期之间的年、月和日的周期

isBefore,isAfter

将当前的LocalDate与另一个LocalDate进行比较

isLeapYear

如果当前是闰年,则返回true。即,该年份能够给4整除,但是不能给100整除,或者能够被400整除。

注:Duration类表示秒或纳秒时间间隔,适合处理较短的时间,需要更高的精确性。我们能使用between()方法比较两个瞬间的差;Period 类表示一段时间的年、月、日,开使用between()方法获取两个日期之间的差作为Period 对象返回;Period 和 Duration两个类看表示时间量或两个日期之间的差,两者之间的差异为:Period基于日期值,而Duration基于时间值。

下面附上使用上述一些方法的代码栗子:

代码语言:javascript复制
@Test
public void localDateTest() {
    // 获取今天的日期
    LocalDate today = LocalDate.of(2019, 1, 1);
    System.out.println("today ==> "   today.toString());
    System.out.println("程序员日每年的第256天 ^_^ ==> "   today.plusDays(255));//程序员日每年的第256天 ^_^
    System.out.println("withDayOfMonth修改当前的月的天数修改为6天 ==> "   today.withDayOfMonth(6));
    System.out.println("withDayOfMonth修改当前的月份修改为8月份 ==> "   today.withMonth(8));
    System.out.println("withDayOfYear修改当年中的天数为第5天 ==> "   today.withDayOfYear(5));
    System.out.println("withYear修改当前日期的年为指定的2020年 ==> "   today.withYear(2020));

    // 今天是几号
    int dayofMonth = today.getDayOfMonth();
    // 今天是周几(返回的是个枚举类型,需要再getValue())
    int dayofWeek = today.getDayOfWeek().getValue();
    // 今年是哪一年
    int dayofYear = today.getDayOfYear();
    System.out.println(dayofMonth   "|"   dayofWeek   "|"   dayofYear);
    // {@code 1}MONDAY
    // {@code 2}TUESDAY
    // {@code 3}WEDNESDAY
    // {@code 4}THURSDAY
    // {@code 5}FRIDAY
    // {@code 6}SATURDAY
    // {@code 7}SUNDAY


    LocalDate today2 = LocalDate.parse("2020-09-09");
    int years = today.until(today2).getYears();
    int months = today.until(today2).getMonths();
    int days = today.until(today2).getDays();
    System.out.println(today   " 和 "   today2   " 间隔 ==> "   years   " years, "   months   " months and "   days   " days");

    long year = today.until(today2, ChronoUnit.YEARS);
    long month = today.until(today2, ChronoUnit.MONTHS);
    long day = today.until(today2, ChronoUnit.DAYS);
    System.out.println(today   " 和 "   today2   " 间隔 ==> "   year   "年");
    System.out.println(today   " 和 "   today2   " 间隔 ==> "   month   "月");
    System.out.println(today   " 和 "   today2   " 间隔 ==> "   day   "天");

    System.out.println(today   " 在 "   today2   " 之前? ==> "   today.isBefore(today2));
    System.out.println(today   " 在 "   today2   " 之后? ==> "   today.isAfter(today2));

    System.out.println(today   " 是闰年吗? ==> "   today.isLeapYear());


    // 根据字符串取:
    LocalDate endOfFeb = LocalDate.parse("2019-10-24");
    System.out.println(endOfFeb.toString());
    // 严格按照yyyy-MM-dd验证,02写成2都不行,当然也有一个重载方法允许自己定义格式
}

console控制台输出:

代码语言:javascript复制
today ==> 2019-01-01
程序员日每年的第256天 ^_^ ==> 2019-09-13
withDayOfMonth修改当前的月的天数修改为6天 ==> 2019-01-06
withDayOfMonth修改当前的月份修改为8月份 ==> 2019-08-01
withDayOfYear修改当年中的天数为第5天 ==> 2019-01-05
withYear修改当前日期的年为指定的2020年 ==> 2020-01-01
1|2|1
2019-01-01 和 2020-09-09 间隔 ==> 1 years, 8 months and 8 days
2019-01-01 和 2020-09-09 间隔 ==> 1年
2019-01-01 和 2020-09-09 间隔 ==> 20月
2019-01-01 和 2020-09-09 间隔 ==> 617天
2019-01-01 在 2020-09-09 之前? ==> true
2019-01-01 在 2020-09-09 之后? ==> false
2019-01-01 是闰年吗? ==> false
2019-10-24

这里说明下2019-01-01today.getDayOfWeek()为什么返回的是2

因为getDayOfWeek()返回的是个枚举类型需要用getValue()获取,和2019-01-01这天是星期二,在DayOfWeek的源码中可有说明,下面取源码片段:

代码语言:javascript复制
public DayOfWeek getDayOfWeek() {
    int dow0 = (int)Math.floorMod(toEpochDay()   3, 7);
    return DayOfWeek.of(dow0   1);
}

DayOfWeek源码片段:

代码语言:javascript复制
/**
 * The singleton instance for the day-of-week of Monday.
 * This has the numeric value of {@code 1}.
 */
MONDAY,
/**
 * The singleton instance for the day-of-week of Tuesday.
 * This has the numeric value of {@code 2}.
 */
TUESDAY,
/**
 * The singleton instance for the day-of-week of Wednesday.
 * This has the numeric value of {@code 3}.
 */
WEDNESDAY,
/**
 * The singleton instance for the day-of-week of Thursday.
 * This has the numeric value of {@code 4}.
 */
THURSDAY,
/**
 * The singleton instance for the day-of-week of Friday.
 * This has the numeric value of {@code 5}.
 */
FRIDAY,
/**
 * The singleton instance for the day-of-week of Saturday.
 * This has the numeric value of {@code 6}.
 */
SATURDAY,
/**
 * The singleton instance for the day-of-week of Sunday.
 * This has the numeric value of {@code 7}.
 */
SUNDAY;

Okey,这就是今天对LocalDate的学习与分享Meow~

0 人点赞