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 }
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)); } }
输出:
代码:
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); } }
输出:
代码:
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()); } }
输出:
代码:
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); } }
输出:
代码:
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()); } }
输出:
代码:
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()); } }
输出:
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:
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:
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中文网其他相关文章!