Java 星期几
Java DayOfWeek 类由包含 DayOfWeekenum 和 API 的包组成,支持不同时区以及根据逻辑或代码实现的要求获取特定日期或时间的各种规则。此类可用于使用各种字段、值、单位、日期和时间日历访问不同的日期和时间,这些字段、值、单位、日期和时间日历可以为作为实现逻辑一部分所需的程序员提供精确的计时相关问题,或提供用于检索状态的有用数据。系统。
语法:
开始您的免费软件开发课程
网络开发、编程语言、软件测试及其他
public class <class_name><method_name><arguments> public DayOfWeek Temporal adjustInto (Temporal temporal)
Java 中 DayOfWeek 的方法
Java DayOfWeek 包含很多方法,如下:
1.颞部
语法:
public Temporal adjustInto(Temporal temporal)
此方法用于调整特定的时间对象以具有本周的特定日期。它返回一个与给定输入具有相同行为的对象,该对象与运行时给定的值相同。
示例:本程序用于演示时态类的调整方法。
代码:
import java.util.*; import java.time.*; public class Adjust_Into { public static void main(String[] args) { ZonedDateTimedt = ZonedDateTime.now(); System.out.println(dt); LocalDate dt2 = LocalDate.parse("2020-05-01"); dt = (ZonedDateTime)dt2.adjustInto(dt); System.out.println(dt); } }
输出:
2.静态星期几
语法:
public static DayOfWeekfrom(TemporalAccessor temporal)
此方法用于获取与 DayOfWeek 类相关的实例或对象,并将参数作为 TemporalAccessor 传递。
示例:此程序用于演示 DayOfWeekfrom() 方法作为静态的一部分。
代码:
import java.time.DayOfWeek; import java.time.LocalDate; import java.time.Month; public class D_Of_Wk { public static void main(String[] args) { LocalDatelcldt = LocalDate.of(2000, Month.APRIL, 23); DayOfWeekdofwk = DayOfWeek.from(lcldt); System.out.println("Current_day_of_the_week " + lcldt + " ->> " + dofwk.name()); } }
输出:
3.整数
语法:
public intget(TemporalField field)
此方法用于从星期几获取字段的指定值,当作为整数值的参数传递时,该值是 int 值。如果没有从该字段获取任何值,或者该值超出范围,则该字段将抛出 DateTimeException。
示例:此程序演示了 public int get(TemporalField field) 方法。
代码:
import java.time.LocalDate; import java.time.temporal.ChronoField; public class Date_Get { public static void main(String[] args) { LocalDate dt2 = LocalDate.parse("2020-06-18"); System.out.println(dt2.get(ChronoField.MONTH_OF_YEAR)); } }
输出:
4.字符串
语法:
public String getDisplayName(TextStyle style,Locale locale)
顾名思义,该方法主要用于显示或文本表示,例如“星期一”或“星期五”,并返回用于识别的文本名称,基本上由程序员作为输入提供,以方便表示为了获得所需的文本表示。如果没有完成语言环境或文本表示,它很可能会返回一个数值。
示例:此程序演示了 String getDisplayName(TextStyle style, Locale locale)
代码:
import java.time.Month; import java.time.format.TextStyle; import java.util.Locale; public class Mnth_en { public static void main(String[] args) { Month mnth = Month.of(5); System.out.println(mnth.getDisplayName(TextStyle.SHORT, Locale.ENGLISH)); } }
输出:
5.长
语法:
public long getLong(TemporalField field)
该方法用于获取传入参数后与星期几一样长的值。这会查询指定值的值,如果不支持则返回一个值,然后返回并抛出异常。
示例:此程序演示了 public long getLong(TemporalField field)。
代码:
import java.time.LocalTime; import java.time.temporal.ChronoField; public class Gt_Dt_Time_Ex { public static void main(String[] args) { LocalTimetm =LocalTime.parse("12:28:39.20"); long scnd_vl = tm.getLong(ChronoField.MILLI_OF_SECOND); System.out.println("Mli_Sec_Field: " + scnd_vl); } }
输出:
6.布尔
语法:
public booleanisSupported(TemporalField field)
它会检查并验证该字段是否受支持,或者只是返回一个空值,以防它不受支持。支持并检查 true 或 false 的布尔值。
示例:此程序演示公共布尔值Supported(TemporalField 字段)。
代码:
import java.time.YearMonth; import java.time.temporal.ChronoField; public class Dt_It_pl { public static void main(String[] args) { YearMonthtdYrMn = YearMonth.of(2020, 8); System.out.println("YearMonth :" + tdYrMn); booleanvl = tdYrMn.isSupported(ChronoField.EPOCH_DAY); System.out.println("EPOCH_DAY is considered as one of the fields of YearMonthClass. " + vl); } }
输出
7.星期几
语法:
public DayOfWeekminus(long days)
此方法返回在实际日期之前指定的星期几,然后返回 DayOfWeek 实际日期计划中存在的天数。
示例:此程序演示公共 DayOfWeek 减去(长天)。
代码:
import java.time.DayOfWeek; public class Day_of_wk_Minus { public static void main(String[] args) { DayOfWeekdy_Of_wk = DayOfWeek.of(4); System.out.println("Day_Week : "+ dy_Of_wk.name() + " - "+ dy_Of_wk.getValue()); long av = 10; dy_Of_wk = dy_Of_wk.minus(av); System.out.println("Day_Week_Earlier " + av + " sm_days: " + dy_Of_wk.name() + " - " + dy_Of_wk.getValue()); } }
输出
8.静态星期几
语法:
public static DayOfWeekof(intdayOfWeek)
It is used to display the 7 days of the week for an enum representation and then is used to obtain an instance of DayOfWeek from an int value.
Example: This program demonstrates the public static DayOfWeek of(intdayOfWeek).
Code:
import java.time.DayOfWeek; public class Dy_Of_Wk_Ex { public static void main(String[] args) { DayOfWeekdyOfWk = DayOfWeek.of(7); System.out.println("Day-Of_the_Week - "+ dyOfWk.name()); System.out.println("Int Value of " + dyOfWk.name() + " - " + dyOfWk.getValue()); } }
Output:
9. DayOfWeek
Syntax:
public DayOfWeekplus(long days)
In this method, the calculation structure revolves around getting the days and the values specified by a number of days after the specified value.
Example: This program demonstrates the public DayOfWeek plus(long days).
Code:
import java.time.DayOfWeek; import java.time.LocalDate; import java.time.Month; public class Dy_Of_Wk_Pls { public static void main(String[] args) { LocalDatelcldt = LocalDate.of(2020, Month.JANUARY, 22); DayOfWeekdyOfWk = DayOfWeek.from(lcldt); System.out.println(dyOfWk.getValue()); dyOfWk = dyOfWk.plus(3); System.out.println(dyOfWk.getValue()); } }
Output:
10. R
Syntax:
public <R> R query(TemporalQuery<R> query)
The specified strategy object is used for querying a query using the specific query for the queried object, and it can also return a null value throws an exception in case of an undefined or not proper query.
Example: This program demonstrates the public <R> R query(TemporalQuery<R> query).
Code:
import java.time.LocalDate; import java.time.temporal.TemporalQueries; public class Locl_Test_Av { public static void main(String[] args) { LocalDate dt1 = LocalDate.parse("2020-12-04"); System.out.printf("Value of Locl_date", dt1.query(TemporalQueries.precision())); } }
Output:
11. ValueRange
Syntax:
public ValueRangerange(TemporalField field)
This ValueRange method gets the range of some valid values for the specified field, which consists of range objects, and then the range of valid value is not null that also needs to keep a check on.
Example: This program demonstrates the public ValueRange range(TemporalField field).
Code:
import java.time.LocalTime; import java.time.temporal.ChronoField; import java.time.temporal.ValueRange; public class GField_For_Umk { public static void main(String[] args) { LocalTime lk_1 = LocalTime.parse("10:22:06"); ValueRange result = lk_1.range(ChronoField.CLOCK_HOUR_OF_AMPM); System.out.println("CLOCK_HOUR_OF_AMPM: " + result); } }
Output:
12. static DayOfWeek
Syntax:
public static DayOfWeekof(intdayOfWeek)
This method returns specific enum type constants with specified names used for matching the identifiers with the declared enums and values as designated to the method.
Example: This program demonstrates the public static DayOfWeek of(intdayOfWeek).
Code:
import java.time.DayOfWeek; public class Dy_Of_Week_Ging { public static void main(String[] args) { DayOfWeekdy_Of_wk = DayOfWeek.of(4); System.out.println("Dy_Of_the_week -" + dy_Of_wk.name()); System.out.println("int_val_of" + dy_Of_wk.name() + " - " + dy_Of_wk.getValue()); } }
Output:
13. static DayOfWeek[]
Syntax:
public static DayOfWeekof(intdayOfWeek)
This method is specifically used to obtain an instance which is the DayOfWeek instance, from an int value and returns a singleton day-of-week which is not null. It throws an exception if the day-week-of is invalid.
Example: This program demonstrates the public static DayOfWeek of(intdayOfWeek)
Code:
import java.time.DayOfWeek; public class Dy_Of_Week_Int { public static void main(String[] args) { DayOfWeekdy_Of_wk = DayOfWeek.of(6); System.out.println("Day_Of_The_Wk - "+ dy_Of_wk.name()); System.out.println("Int_Vl_Of " + dy_Of_wk.name() + " - "+ dy_Of_wk.getValue()); } }
Output:
Conclusion
This class is very useful in terms of programming where there are huge requirements and something related to the time-bound components or determine which timing constraints are needed for the implementation, which indicates any system or project with timings. Date of the week or month any module is fully associated with Java and DayOfWeek.
以上是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)

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

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

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

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。
