内容简介:Date:用来表示时间点LocalDate:用来表示大家熟悉的日历表示法
Date:用来表示时间点
LocalDate:用来表示大家熟悉的日历表示法
LocalDate是带有年,月,日的日期。为了构建LocalDate对象,可以使用now或of静态方法栗子如下:
LocalDate today = LocalDate.now(); LocalDate birthday = LocalDate.of(1997, 8, 17);
看LocalDate源码中的注释发现
* @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基于时间值。
下面附上使用上述一些方法的代码栗子:
@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都不行,当然也有一个重载方法允许自己定义格式
// 取本月第1天:
LocalDate firstDayOfThisMonth = today.with(TemporalAdjusters.firstDayOfMonth());
// 取本月最后一天,再也不用计算是28,29,30还是31:
LocalDate lastDayOfThisMonth = today.with(TemporalAdjusters.lastDayOfMonth());
// 取2019年1月第一个周一
LocalDate firstMondayOf2019 = LocalDate.parse("2019-01-01").with(TemporalAdjusters.firstInMonth(DayOfWeek.MONDAY));
System.out.println("取本月第1天 ==> " + firstDayOfThisMonth);
System.out.println("取本月最后一天,再也不用计算是28,29,30还是31 ==> " + lastDayOfThisMonth);
System.out.println("取2019年1月第一个周一 ==> " + firstMondayOf2019);
}
console控制台输出:
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 取本月第1天 ==> 2019-01-01 取本月最后一天,再也不用计算是28,29,30还是31 ==> 2019-01-31 取2019年1月第一个周一 ==> 2019-01-07
这里说明下 2019-01-01
的 today.getDayOfWeek()
为什么返回的是 2
因为 getDayOfWeek()
返回的是个枚举类型需要用 getValue()
获取,和 2019-01-01
这天是星期二,在 DayOfWeek
的源码中可有说明,下面取源码片段:
public DayOfWeek getDayOfWeek() {
int dow0 = (int)Math.floorMod(toEpochDay() + 3, 7);
return DayOfWeek.of(dow0 + 1);
}
DayOfWeek
源码片段:
/**
* 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~
本文由Aquan 创作,采用 知识共享署名4.0 国际许可协议进行许可
本站文章除注明转载/出处外,均为本站原创或翻译,转载前请务必署名
最后编辑时间为: 六月 20,2019
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:- 迅速了解一下 ES10 中 Object.fromEntries 的用法
- 通过一个计数器来了解react-redux及其用法
- AWK 的用法
- AWK基础用法
- UniversalImageLoader的用法总结
- SQLAlchemy框架用法详解
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
深度解析淘宝运营
刘涛 / 电子工业出版社 / 2015-9-1 / 49.00元
淘宝运营,仅有知识是不够的,还需要有系统的运营思路。为帮助广大电商从业者以及众多中小卖家更好地运营店铺,《深度解析淘宝运营》全面阐述了整个店铺运营的重点环节,包括淘宝搜索规则、打造爆款、店铺规划、客户服务、直通车、钻石展位、数据分析等内容。具体操作步骤翔实,并且结合笔者的实际操作经验,将各个环节最本质的一面透彻展现给读者,结合理论与实战,尽可能向读者展示一个最真实的运营核心。《深度解析淘宝运营》没......一起来看看 《深度解析淘宝运营》 这本书的介绍吧!