爪哇时期
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中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

Java 8引入了Stream API,提供了一种强大且表达力丰富的处理数据集合的方式。然而,使用Stream时,一个常见问题是:如何从forEach操作中中断或返回? 传统循环允许提前中断或返回,但Stream的forEach方法并不直接支持这种方式。本文将解释原因,并探讨在Stream处理系统中实现提前终止的替代方法。 延伸阅读: Java Stream API改进 理解Stream forEach forEach方法是一个终端操作,它对Stream中的每个元素执行一个操作。它的设计意图是处

PHP是一种广泛应用于服务器端的脚本语言,特别适合web开发。1.PHP可以嵌入HTML,处理HTTP请求和响应,支持多种数据库。2.PHP用于生成动态网页内容,处理表单数据,访问数据库等,具有强大的社区支持和开源资源。3.PHP是解释型语言,执行过程包括词法分析、语法分析、编译和执行。4.PHP可以与MySQL结合用于用户注册系统等高级应用。5.调试PHP时,可使用error_reporting()和var_dump()等函数。6.优化PHP代码可通过缓存机制、优化数据库查询和使用内置函数。7

PHP和Python各有优势,选择应基于项目需求。1.PHP适合web开发,语法简单,执行效率高。2.Python适用于数据科学和机器学习,语法简洁,库丰富。

PHP适合web开发,特别是在快速开发和处理动态内容方面表现出色,但不擅长数据科学和企业级应用。与Python相比,PHP在web开发中更具优势,但在数据科学领域不如Python;与Java相比,PHP在企业级应用中表现较差,但在web开发中更灵活;与JavaScript相比,PHP在后端开发中更简洁,但在前端开发中不如JavaScript。

PHP和Python各有优势,适合不同场景。1.PHP适用于web开发,提供内置web服务器和丰富函数库。2.Python适合数据科学和机器学习,语法简洁且有强大标准库。选择时应根据项目需求决定。

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP成为许多网站首选技术栈的原因包括其易用性、强大社区支持和广泛应用。1)易于学习和使用,适合初学者。2)拥有庞大的开发者社区,资源丰富。3)广泛应用于WordPress、Drupal等平台。4)与Web服务器紧密集成,简化开发部署。

PHP适用于Web开发和内容管理系统,Python适合数据科学、机器学习和自动化脚本。1.PHP在构建快速、可扩展的网站和应用程序方面表现出色,常用于WordPress等CMS。2.Python在数据科学和机器学习领域表现卓越,拥有丰富的库如NumPy和TensorFlow。
