📌  相关文章
📜  Java中的 OffsetDateTime withMinute() 方法及示例

📅  最后修改于: 2022-05-13 01:55:27.201000             🧑  作者: Mango

Java中的 OffsetDateTime withMinute() 方法及示例

Java中 OffsetDateTime 类的withMinute()方法返回此 OffsetDateTime 的副本,其中一天中的小时按照参数中指定的方式更改。

句法:

public OffsetDateTime withMinute(int minute)

参数:此方法接受单个参数分钟,该参数指定要在结果中设置的分钟数,范围从 0 到 59。

返回值:它返回一个基于此日期的 OffsetDateTime 和请求的分钟,并且不为空。

异常:当小时的分钟值无效或年份无效时,程序会抛出DateTimeException

下面的程序说明了withMinute()方法:

方案一:

// 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

方案二:

// 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: Java/time/OffsetDateTime.html#withMinute(int)