首页  >  文章  >  Java  >  爪哇时期

爪哇时期

王林
王林原创
2024-08-30 15:52:511220浏览

java period 是一个用来测量年、月、日时间的类。 java中Period类的包是java.time.Period。 period 类对象指定时间段或用于确定年、月和日两个时间之间的差异。 period 对象是不可变的并且是线程安全的,就像Period 对象是不可变的一样,因此一旦创建它我们就无法更改它的值。但是,我们可以基于另一个 period 对象创建新的 period 对象。 period 类继承一个对象类(因为该对象是 java 中所有类的超类)并实现 ChronoPeriod 接口。

开始您的免费软件开发课程

网络开发、编程语言、软件测试及其他

语法:

java中Period类声明的语法如下:

public final class Period extends Object implements ChronoPeriod, Serializable
{
// variables and method of the class Period
}

解释Java时期的方法

Period 类方法列表在下面通过示例代码进行了解释;示例代码可以进一步用于类似的方法(对于未给出的每个方法示例代码):

  • long get(TemporalUnit unit):返回所请求单位的值。
  • 静态周期(LocalDate startInclusive, LocalDate endExclusive):返回Period对象是两个日期之间的年、月、日数的周期。

代码:

import java.time.Period;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
public class PeriodClassDemo {
public static void main(String[] args) {
Period p = Period.between(LocalDate.ofYearDay(2017, 20), LocalDate.ofYearDay(2017, 30) );
System.out.println(p);
System.out.println(p.get(ChronoUnit.DAYS));
System.out.println(p.get(ChronoUnit.MONTHS));
System.out.println(p.get(ChronoUnit.YEARS));
}
}

输出:

爪哇时期

  • Temporal addTo(Temporaltemporal):返回Period对象,即temporal与thisPeriod对象相加。

代码:

import java.time.Period;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.time.LocalDateTime;
public class PeriodClassDemo {
public static void main(String[] args) {
Period period = Period.of(1,1,1);
LocalDateTime d = LocalDateTime.now();
System.out.println(d);
d = (LocalDateTime)period.addTo(d);
System.out.println(d);
}
}

输出:

爪哇时期

  • boolean equals(Object otherPeriod):检查此Period与指定时间段并返回布尔值。
  • boolean isNegative():如果此周期为负数,则返回 True。
  • boolean isZero():如果此周期为负数,则返回 True。

代码:

import java.time.Period;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.time.LocalDateTime;
public class PeriodClassDemo {
public static void main(String[] args) {
Period p1 = Period.of(1,1,1);
Period p2 = Period.of(10,5,2);
Period p3 = Period.of(10,5,2);
System.out.println(p1.equals(p2));
System.out.println(p2.equals(p3));
System.out.println(p2.isNegative());
System.out.println(p2.isZero());
}
}

输出:

爪哇时期

  • static Duration from(TemporalAmount amount): 从时间量中获取周期的实例。

代码:

import java.time.Period;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.time.LocalDateTime;
public class PeriodClassDemo {
public static void main(String[] args) {
Period p = Period.from(Period.of(10, 5, 2) );
System.out.println(p);
}
}

输出:

爪哇时期

  • IsoChronology getChronology(): 返回该时期的年表,属于 ISO 日历系统。
  • int getDays(): 返回周期(以天为单位)。
  • int getMonths(): 以月为单位返回周期。
  • 列表 getUnits(): 返回此周期支持的单位集。
  • int hashCode(): 返回此期间的哈希码。

代码:

import java.time.Period;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.time.LocalDateTime;
public class PeriodClassDemo {
public static void main(String[] args) {
Period p = Period.from(Period.of(10, 5, 2) );
System.out.println(p.getChronology());
System.out.println(p.get(ChronoUnit.MONTHS));
System.out.println(p.getDays());
System.out.println(p.getMonths());
System.out.println(p.getYears());
System.out.println(p.getClass());
System.out.println(p.getUnits());
}
}

输出:

爪哇时期

  • Period minus(TemporalAmount amountToSubtract): 返回此周期减去指定周期所得到的对象。
  • Period minusDays(long daysToSubtract): 返回指定天数减去的对象。
  • Period Mindsmonths(长月):返回对象,该周期与指定月份相减的结果。
  • Period minusYears(longyears):返回对象,它是指定年份减去这个周期的结果。
  • Period multipliedBy(long multiplicand): 返回周期对象乘以标量。
  • Period neated(): 返回一个周期对象,该对象是由该周期的长度取反而产生的。
  • Period normalized(): 返回一个周期对象,该对象是由该周期产生的,并以年和月进行标准化。
  • 静态周期(int 年、int 月、int 天): 返回表示几年、几个月和几天的 period 对象。
  • 静态Period ofDays(int days):返回几天的Period对象。
  • 静态Period ofMonths(int Months):返回几个月的Period对象。
  • 静态Period ofWeeks(int week):返回几周的Period对象。
  • static period of Years(intyears):返回几周的Period对象。

代码:

import java.time.Period;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.time.LocalDateTime;
public class PeriodClassDemo {
public static void main(String[] args) {
Period p1 = Period.of(10,5,2);
Period p2 = Period.of(20,5,2);
System.out.println(p1.getDays());
System.out.println(p2.getDays());
System.out.println(p1.minus(p2).getDays());
System.out.println(p1.minusDays(2).getDays());
System.out.println(p1.minusMonths(1).getDays());
System.out.println(p1.minusYears(1).getDays());
System.out.println(p1.multipliedBy(2).getDays());
System.out.println(p1.negated().getDays());
System.out.println(p1.normalized().getDays());
Period p3 = Period.ofDays(1);
System.out.println(p3.getDays( ));
Period p4 = Period.ofMonths(2);
System.out.println(p4.getMonths());
}
}

输出:

爪哇时期

  • static Period parse(CharSequence text): Return Period object from a text, for example, PnYnMnD.
  • Period plus(TemporalAmount amountToAdd): Return the period object of this period with added the specified period.
  • Period plusDays(long daysToAdd ): Return a period object of this period with added the specified days.
  • Period plusMonths(long monthsToAdd): Return a period object of this period with added the specified months.
  • Period plusYears(long yearsToAdd): Return a period object of this period with added the specified years.

Code:

import java.time.Period;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.time.LocalDateTime;
public class PeriodClassDemo {
public static void main(String[] args) {
Period p1 = Period.of(10,5,2);
Period p2 = Period.parse("P1Y2M3D");
System.out.println(p2.getMonths());
Period p3 = p1.plus(Period.ofDays(5));
System.out.println(p3);
}
}

Output:

爪哇时期

  • Temporal subtractFrom(Temporal temporal): Return Subtraction of this period from a temporal object.
  • String toString(): Return this period in string representation, such as PT8H6M12.345S.
  • long toTotalMonths(): Return the total number of months in this period.
  • Period withDays(int days): Return a period object of this period with the specified amount of days.
  • Period withMonths(int months): Return a period object of this period with the specified amount of months.
  • Period withYears(int years): Return a period object of this period with the specified amount of Years.

Code:

import java.time.Period;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.time.LocalDateTime;
public class PeriodClassDemo {
public static void main(String[] args) {
Period p1 = Period.of(10,5,2);
System.out.println(p1);
LocalDateTime d = LocalDateTime.now();
System.out.println(d);
d = (LocalDateTime)p1.subtractFrom(d);
System.out.println(d);
System.out.println(p1.toString());
System.out.println(p1.toTotalMonths());
System.out.println(p1.withDays(2));
System.out.println(p1.toString());
System.out.println(p1.withMonths(1));
System.out.println(p1.toString());
}
}

Output:

爪哇时期

Conclusion

The Period class is one of the built-in class in java, which is used to measure time in years, months, and days and add, subtract, and convert the period, or in simple words, the period class allows one to operate on day or month, or year period. The period class is available in java.time.Period package of java.

以上是爪哇时期的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
上一篇:Java DayOfWeek下一篇:Timer in Java