Java中OffsetDateTime類的withMinute()方法返回此OffsetDateTime的副本,其中一天中的小時數按照參數中的指定進行了更改。
用法:
public OffsetDateTime withMinute(int minute)
參數:此方法接受單個參數分鍾,該分鍾指定要在結果中設置的minute-of-hour,範圍為0到59。
返回值:它基於此日期返回OffsetDateTime,其中包含請求的小時數,而不是null。
異常:當小時的分鍾值無效或day-of-year無效時,程序將引發DateTimeException。
以下示例程序旨在說明withMinute()方法:
示例1:
// Java program to demonstrate the withMinute() method
import java.time.OffsetDateTime;
import java.time.ZonedDateTime;
public class GFG {
public static void main(String[] args)
{
// Parses the date1
OffsetDateTime date1
= OffsetDateTime
.parse(
"2018-12-12T13:30:30+05:00");
// Prints dates
System.out.println("Date1: " + date1);
// Changes the minute of day
System.out.println("Date1 after altering minute of day: "
+ date1.withMinute(20));
}
}
輸出:
Date1: 2018-12-12T13:30:30+05:00 Date1 after altering minute of day: 2018-12-12T13:20:30+05:00
示例2:
// Java program to demonstrate the withMinute() method
import java.time.OffsetDateTime;
public class GFG {
public static void main(String[] args)
{
try {
// Parses the date1
OffsetDateTime date1
= OffsetDateTime
.parse(
"2018-12-12T13:30:30+05:00");
// Prints dates
System.out.println("Date1: " + date1);
// Changes the minute of day
System.out.println("Date1 after altering minute of day: "
+ date1.withMinute(27));
}
catch (Exception e) {
System.out.println("Exception: " + e);
}
}
}
輸出:
Date1: 2018-12-12T13:30:30+05:00 Date1 after altering minute of day: 2018-12-12T13:27:30+05:00
參考: https://docs.oracle.com/javase/10/docs/api/java/time/OffsetDateTime.html#withMinute(int)
相關用法
- Java LocalDateTime withMinute()用法及代碼示例
- Java ZonedDateTime withMinute()用法及代碼示例
- Java LocalTime withMinute()用法及代碼示例
- Java OffsetDateTime until()用法及代碼示例
- Java OffsetDateTime get()用法及代碼示例
- Java OffsetDateTime from()用法及代碼示例
- Java OffsetDateTime with()用法及代碼示例
- Java OffsetDateTime getDayOfMonth()用法及代碼示例
- Java OffsetDateTime getDayOfYear()用法及代碼示例
- Java OffsetDateTime getDayOfWeek()用法及代碼示例
- Java OffsetDateTime getMinute()用法及代碼示例
- Java OffsetDateTime getHour()用法及代碼示例
- Java OffsetDateTime getLong()用法及代碼示例
- Java OffsetDateTime getMonth()用法及代碼示例
- Java OffsetDateTime getSecond()用法及代碼示例
注:本文由純淨天空篩選整理自gopaldave大神的英文原創作品 OffsetDateTime withMinute() method in Java with examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。