Java Period类使用示例教程

x33g5p2x  于2021-08-22 转载在 Java  
字(4.4k)|赞(0)|评价(0)|浏览(204)

本文是《Java日期时间API教程》系列。在这篇文章中,我们将讨论java.time package中的Java Period类的重要方法或API。

Period 类以年、月、日为单位表示一个时间量。Period使用基于日期的值(年、月、日)。这个类是不可变的,并且是线程安全的

Java Period类图

下面的类图显示了Period类所提供的方法/API的列表。

Java Period类的方法/接口与实例

让我们通过例子来演示几个重要的和常用的Period类方法的用法。在下面的PeriodMethodsExample类中,每个方法的名称都描述了Period类的方法及其用法。请参考https://docs.oracle.com/javase/8/docs/api/java/time/Period.html。 Java文档了解更多细节。

package net.javaguides.date;

import java.time.LocalDateTime;
import java.time.Period;

//*/*
/* Class demonstrates the usage of Period class methods with examples
/* 
/* @author Ramesh Fadatare
/*
/*/
public class PeriodMethodsExample {

    public static void main(String[] args) {
        PeriodMethodsExample example = new PeriodMethodsExample();
        example.withYearsMethodExample();

        example.withMonthsMethodExample();

        example.withDaysMethodExample();

        example.toTotalMonthsMethodExample();

        example.subtractFromMethodExample();

        example.plusYearsMethodExample();

        example.plusMonthsMethodExample();

        example.plusDaysMethodExample();

        example.plusMethodExample();

        example.parseMethodExample();

        example.ofYearsMethodExample();

        example.ofWeeksMethodExample();

        example.ofMonthsMethodExample();

        example.ofDaysMethodExample();

        example.ofMethodExample();

        example.minusYearsMethodExample();

        example.minusMonthsMethodExample();

        example.minusDaysMethodExample();
    }

    public void withYearsMethodExample() {
        Period period = Period.ofYears(2);
        System.out.println(period.toString());
        period = period.withYears(5);
        System.out.println(period.toString());
    }

    public void withMonthsMethodExample() {
        Period period = Period.ofMonths(2);
        System.out.println(period.toString());
        period = period.withMonths(5);
        System.out.println(period.toString());
    }

    public void withDaysMethodExample() {
        Period period = Period.ofDays(2);
        System.out.println(period.toString());
        period = period.withDays(5);
        System.out.println(period.toString());
    }

    public void toTotalMonthsMethodExample() {
        Period period = Period.ofYears(2);
        System.out.println(period.toTotalMonths());
    }

    public void subtractFromMethodExample() {
        Period period = Period.ofYears(2);

        LocalDateTime date = LocalDateTime.now();
        System.out.println(date);

        date = (LocalDateTime) period.subtractFrom(date);
        System.out.println(date);
    }

    public void plusYearsMethodExample() {
        Period period = Period.ofYears(2);
        Period period1 = period.plusYears(1);
        System.out.println(period1.getYears());
    }

    public void plusMonthsMethodExample() {
        Period period = Period.ofMonths(2);
        Period period1 = period.plusMonths(1);
        System.out.println(period1.getMonths());
    }

    public void plusDaysMethodExample() {
        Period period = Period.ofDays(2);
        Period period1 = period.plusDays(1);
        System.out.println(period1.getDays());
    }

    public void plusMethodExample() {
        Period period = Period.of(1, 5, 2);
        System.out.println(
            "Years: " + period.getYears() + ", Months: " + period.getMonths() + ", Days: " + period.getDays());
        Period period1 = period.plus(Period.ofDays(5));
        System.out.println(
            "Years: " + period1.getYears() + ", Months: " + period1.getMonths() + ", Days: " + period1.getDays());
    }

    public void parseMethodExample() {
        Period period = Period.parse("P1Y2M3D");
        System.out.println(
            "Years: " + period.getYears() + ", Months: " + period.getMonths() + ", Days: " + period.getDays());
    }

    public void ofYearsMethodExample() {
        Period period = Period.ofYears(2);
        System.out.println(period.getYears());
    }

    public void ofWeeksMethodExample() {
        Period period = Period.ofWeeks(2);
        System.out.println(period.getDays());
    }

    public void ofMonthsMethodExample() {
        Period period = Period.ofMonths(2);
        System.out.println(period.getMonths());
    }

    public void ofDaysMethodExample() {
        Period period = Period.ofDays(2);
        System.out.println(period.getDays());
    }

    public void ofMethodExample() {
        Period period = Period.of(1, 5, 2);
        System.out.println(
            "Years: " + period.getYears() + ", Months: " + period.getMonths() + ", Days: " + period.getDays());
    }

    public void minusYearsMethodExample() {
        Period period = Period.ofYears(5);
        System.out.println(period.getYears());
        Period period1 = period.minusYears(3);
        System.out.println(period1.getYears());
    }

    public void minusMonthsMethodExample() {
        Period period = Period.ofMonths(5);
        System.out.println(period.getMonths());
        Period period1 = period.minusMonths(3);
        System.out.println(period1.getMonths());
    }

    public void minusDaysMethodExample() {
        Period period = Period.ofDays(5);
        System.out.println(period.getDays());
        Period period1 = period.minusDays(3);
        System.out.println(period1.getDays());
    }
}

输出:

P2Y
P5Y
P2M
P5M
P2D
P5D
24
2018-12-22T13:27:23.928
2016-12-22T13:27:23.928
3
3
3
Years: 1, Months: 5, Days: 2
Years: 1, Months: 5, Days: 7
Years: 1, Months: 2, Days: 3
2
14
2
2
Years: 1, Months: 5, Days: 2
5
2
5
2
5
2

相关文章

热门文章

更多