2021-09-22

Java8中LocalDate获取时间字符``

		//昨天
        String yesterdayStr = LocalDate.now().minusDays(1).atStartOfDay().atZone(ZoneId.systemDefault()).format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        System.out.println("yesterdayStr = " + yesterdayStr);
        //今天
        String todayStr = LocalDate.now().atStartOfDay().atZone(ZoneId.systemDefault()).format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        System.out.println("todayStr = " + todayStr);
        //明天
        String tomorrowStr = LocalDate.now().plusDays(1).atStartOfDay().atZone(ZoneId.systemDefault()).format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        System.out.println("tomorrowStr = " + tomorrowStr);
        //上个月
        String lastMonthStr = LocalDate.now().minusMonths(1).atStartOfDay().atZone(ZoneId.systemDefault()).format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        System.out.println("lastMonthStr = " + lastMonthStr);
        //下个月
        String nextMonthStr = LocalDate.now().plusMonths(1).atStartOfDay().atZone(ZoneId.systemDefault()).format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        System.out.println("nextMonthStr = " + nextMonthStr);
        //去年
        String lastYearStr = LocalDate.now().minusYears(1).atStartOfDay().atZone(ZoneId.systemDefault()).format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        System.out.println("lastYearStr = " + lastYearStr);
        //明年
        String nextYearStr = LocalDate.now().plusYears(1).atStartOfDay().atZone(ZoneId.systemDefault()).format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        System.out.println("nextYearStr = " + nextYearStr);
上一篇:DateUtil 日期转换


下一篇:输入某人生日,格式为“yyyy-MM-dd“,输出到现在为止经过了多少周。