【Java8】date api(java.time)

        LocalDate yMd = LocalDate.of(2017, 4, 1);
        System.out.println(yMd);
        System.out.println(yMd.getYear());
        System.out.println(yMd.getMonth());
        System.out.println(yMd.getDayOfMonth());
        System.out.println(yMd.getDayOfYear());
        System.out.println(yMd.getDayOfWeek());
        System.out.println(yMd.isLeapYear());
        System.out.println(yMd.lengthOfMonth());
        System.out.println(yMd.lengthOfYear());

        System.out.println("==========================");

        LocalDate today = LocalDate.now();
        System.out.println(today);

        System.out.println("==========================");

        System.out.println(yMd.get(ChronoField.YEAR));
        System.out.println(yMd.get(ChronoField.MONTH_OF_YEAR));
        System.out.println(yMd.get(ChronoField.DAY_OF_MONTH));
        System.out.println(yMd.get(ChronoField.DAY_OF_WEEK));

        System.out.println("==========================");

        LocalTime hMs = LocalTime.of(17, 50, 10);
        System.out.println(hMs);
        System.out.println(hMs.getHour());
        System.out.println(hMs.getMinute());
        System.out.println(hMs.getSecond());

        System.out.println("==========================");

        LocalDate parseDate = LocalDate.parse("2017-04-05");
        LocalTime parseTime = LocalTime.parse("18:10:10");
        System.out.println(parseDate);
        System.out.println(parseTime);

        System.out.println("==========================");

        LocalDateTime yMdhMs = LocalDateTime.of(yMd, hMs);
        LocalDateTime athMs = yMd.atTime(hMs);
        LocalDateTime atyMd = hMs.atDate(yMd);
        System.out.println(athMs);
        System.out.println(atyMd);
        System.out.println(yMdhMs);

        System.out.println("==========================");

        LocalDate toDate = yMdhMs.toLocalDate();
        LocalTime toTime = yMdhMs.toLocalTime();
        System.out.println(toDate);
        System.out.println(toTime);

        System.out.println("==========================");

        System.out.println(Instant.ofEpochSecond(3));
        System.out.println(Instant.ofEpochSecond(2,1_000_000_000));

        System.out.println("==========================");

        Duration ofMin = Duration.ofMinutes(3);
        Duration of = Duration.of(3, ChronoUnit.MINUTES);

        System.out.println(ofMin);
        System.out.println(of);

        Period ofDay = Period.ofDays(10);
        Period ofWeek = Period.ofWeeks(3);
        Period yearMonthDay = Period.of(2, 6, 1);//2年6个月1天

        System.out.println(ofDay);
        System.out.println(ofWeek);
        System.out.println(yearMonthDay);

        System.out.println("==========================");

        LocalDate editDate = LocalDate.of(2017, 4, 10);

        System.out.println(editDate);
        System.out.println(editDate.withYear(2020));
        System.out.println(editDate.withMonth(7));
        System.out.println(editDate.with(ChronoField.DAY_OF_MONTH,7));

        System.out.println(editDate.plusWeeks(1));
        System.out.println(editDate.minusWeeks(3));
        System.out.println(editDate.plus(10,ChronoUnit.MONTHS));

        System.out.println("==========================");

        LocalDate tempDate = LocalDate.of(2017, 4, 6);

        System.out.println(tempDate.with(nextOrSame(DayOfWeek.SUNDAY)));
        System.out.println(tempDate.with(lastDayOfMonth()));

        LocalDate lambdaDate = tempDate.with(temporal -> {
            DayOfWeek dow = DayOfWeek.of(temporal.get(ChronoField.DAY_OF_WEEK));
            int dayToAdd = 1;
            if(dow == DayOfWeek.THURSDAY) dayToAdd = 3;
            else if(dow == DayOfWeek.WEDNESDAY) dayToAdd = 2;
            return temporal.plus(dayToAdd,ChronoUnit.DAYS);
        });
        System.out.println(lambdaDate);

        System.out.println("==========================");

        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:MM:ss");
        LocalDateTime date = LocalDateTime.of(yMd, hMs);
        String formatDate = dtf.format(date);
        System.out.println(formatDate);

        System.out.println("==========================");

        DateTimeFormatter formatter = 
                new DateTimeFormatterBuilder()
                .appendText(ChronoField.DAY_OF_MONTH)
                .appendLiteral(".")
                .appendText(ChronoField.MONTH_OF_YEAR)
                .appendLiteral(".")
                .appendText(ChronoField.YEAR)
                .parseCaseInsensitive()
                .toFormatter(Locale.ITALIAN);
        System.out.println(formatter.format(date));

        System.out.println("==========================");

        ZoneId zoneId = TimeZone.getDefault().toZoneId();
        System.out.println(zoneId);

2017-04-01
2017
APRIL
1
91
SATURDAY
false
30
365
==========================
2017-04-06
==========================
2017
4
1
6
==========================
17:50:10
17
50
10
==========================
2017-04-05
18:10:10
==========================
2017-04-01T17:50:10
2017-04-01T17:50:10
2017-04-01T17:50:10
==========================
2017-04-01
17:50:10
==========================
1970-01-01T00:00:03Z
1970-01-01T00:00:03Z
==========================
PT3M
PT3M
P10D
P21D
P2Y6M1D
==========================
2017-04-10
2020-04-10
2017-07-10
2017-04-07
2017-04-17
2017-03-20
2018-02-10
==========================
2017-04-09
2017-04-30
2017-04-09
==========================
2017-04-01 05:04:10
==========================
1.aprile.2017
==========================
Asia/Shanghai
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值