Java time

LocalDate currentDate = LocalDate.now();
LocalDate beforeDate = currentDate.minusMonths(1);
LocalDate lastDayOfMonth = beforeDate.with(TemporalAdjusters.lastDayOfMonth());
String sunday = "SUNDAY";
String saturday = "SATURDAY";
if (lastDayOfMonth.getDayOfWeek().toString().equals(sunday)) {
    // 如果是周日,向前推两天
    lastDayOfMonth = lastDayOfMonth.minusDays(2);
} else if (lastDayOfMonth.getDayOfWeek().toString().equals(saturday)){
    // 如果是周六,向前推一天
    lastDayOfMonth = lastDayOfMonth.minusDays(1);
}
// 最后一个工作日日期
String lastDay = lastDayOfMonth.format(DateTimeFormatter.ofPattern("yyyyMMdd"));
// 当前日期
String currnetDay = currentDate.format(DateTimeFormatter.ofPattern("yyyyMMdd"));
LocalDate afterDate = LocalDate.of(2018, 1, 16);
LocalDate today = LocalDate.now();
System.out.println(afterDate);
System.out.println(today);
System.out.println(afterDate.isAfter(today));
LocalDate currentDate = LocalDate.now();
System.out.println(currentDate.minusDays(1));
System.out.println("-------------------------");
LocalDate beforeDate = currentDate.minusMonths(1);
LocalDate lastDayOfMonth = beforeDate.with(TemporalAdjusters.lastDayOfMonth());
String dayOfWeek = lastDayOfMonth.getDayOfWeek().toString();
System.out.println("当前时间:" + currentDate);
System.out.println("上个月的今天:" + beforeDate);
System.out.println("上个月的最后一天:" + lastDayOfMonth);
System.out.println("上个月的最后一天是星期几:" + dayOfWeek);
System.out.println("上个月的最后一天的前一天:" + lastDayOfMonth.minusDays(1));
System.out.println("上个月的最后一天的前两天:" + lastDayOfMonth.minusDays(2));
System.out.println("格式化后的日期:" + lastDayOfMonth.format(DateTimeFormatter.ofPattern("yyyyMMdd")));
LocalDate localDate = LocalDate.parse("20180202", DateTimeFormatter.BASIC_ISO_DATE);
String ld = localDate.plusMonths(3).format(DateTimeFormatter.BASIC_ISO_DATE);
System.out.println(ld);
String ld1 = localDate.minusMonths(3).format(DateTimeFormatter.BASIC_ISO_DATE);
System.out.println(ld1);
System.out.println("-----------");
LocalDate st = LocalDate.parse("20180101", DateTimeFormatter.BASIC_ISO_DATE);
LocalDate ed = LocalDate.parse("20180221", DateTimeFormatter.BASIC_ISO_DATE);
LocalDate af = st.plusMonths(3);
System.out.println(af);
System.out.println(st.compareTo(af));
System.out.println(af.compareTo(ed));

https://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html

转载于:https://my.oschina.net/huangchp/blog/1828112

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值