Java8 时间格式化新方案

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoField;
import java.time.temporal.ChronoUnit;
import java.util.Formatter;

import static java.time.temporal.TemporalAdjusters.lastDayOfMonth;
import static java.time.temporal.TemporalAdjusters.nextOrSame;

public class TimeDemo {
    public static void main(String[] args) {
        LocalDate date = LocalDate.of(2014,3,18);
        int year = date.getYear();
        Month month = date.getMonth();
        int day = date.getDayOfMonth();
        DayOfWeek dow = date.getDayOfWeek();
        int len = date.lengthOfMonth();
        boolean leap = date.isLeapYear();

        LocalDate today = LocalDate.now();
        LocalDateTime time = LocalDateTime.of(2018,4,12,12,12);
        System.out.println(time);
        System.out.println(date);
        System.out.println(year);
        System.out.println(month);
        System.out.println(day);
        System.out.println(dow);
        System.out.println(len);
        System.out.println(leap);
        System.out.println(today);


        //获取时长
        Period tenDays = Period.between(LocalDate.of(2014, 3, 8),
                LocalDate.of(2014, 3, 18));
        System.out.println(tenDays.getDays());
        System.out.println(tenDays.getMonths());

        //Duration  Period对象
        Duration threeMinutes = Duration.ofMinutes(3);
        Duration threeMinutes2 = Duration.of(3, ChronoUnit.MINUTES);
        Period tenDays2 = Period.ofDays(10);
        Period threeWeeks = Period.ofWeeks(3);
        Period twoYearsSixMonthsOneDay = Period.of(2, 6, 1);
        System.out.println(threeMinutes.getSeconds());
        System.out.println(threeMinutes2.getSeconds());
        System.out.println(tenDays2.getDays());
        System.out.println(threeWeeks.getDays());
        System.out.println(twoYearsSixMonthsOneDay.getDays());


        //操纵、解析、格式化日期
        LocalDate date11 = LocalDate.of(2014, 3, 18);
        LocalDate date21 = date11.withYear(2011);
        LocalDate date31 = date21.withDayOfMonth(25);
        LocalDate date41 = date31.with(ChronoField.MONTH_OF_YEAR, 9);

        System.out.println(date11);
        System.out.println(date21);
        System.out.println(date31);
        System.out.println("---"+date41);

        //以相对方式修改LocalDate对象的属性
        LocalDate date12 = LocalDate.of(2014, 3, 18);
        LocalDate date22 = date12.plusWeeks(1);
        LocalDate date32 = date22.minusYears(3);
        LocalDate date42 = date32.plus(6, ChronoUnit.MONTHS);

        System.out.println(date12);
        System.out.println(date22);
        System.out.println(date32);
        System.out.println(date42);

        //使用 TemporalAdjuster
        LocalDate date13 = LocalDate.of(2014, 3, 18);
        LocalDate date23 = date13.with(nextOrSame(DayOfWeek.SUNDAY));
        LocalDate date33 = date13.with(lastDayOfMonth());
        System.out.println("----------");
        System.out.println(date13);
        System.out.println(date23);
        System.out.println(date33);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值