Java LocalDate的常用操作及计算
在JDK1.8中新增两个处理日期和时间的类 ,个人常用方法的用法记录如下:
/**
* LocalDate 常用操作
*
* @param args args
*/
public static void main(String[] args) {
// 当前日期
LocalDate start = LocalDate.now();
// 加一天
LocalDate end = start.plusDays(1);
// start.plusYears(1)
// 减一天
// start.minusDays(1)
//当前月最后一天
LocalDate mEnd = start.with(TemporalAdjusters.lastDayOfMonth());
//当前年最后一天
LocalDate yWith = start.with(TemporalAdjusters.lastDayOfYear());
// 时间差多少天、月、年
Period between = Period.between(mEnd, yWith);
int days = between.getDays();
int months = between.getMonths();
int years = between.getYears();
// 获取日期的周一是那一天(可能跨月)
LocalDate monday = start.with(DayOfWeek.MONDAY);
System.err.println("当前日期的周一为:"+monday);
System.out.println(end);
System.out.println(mEnd);
System.out.println(yWith);
System.out.println("相差多少天:" + days + ",相差多少月:" + months + ",相差多少年:" + years);
}
待后面继续更新经常使用方法。。。