Java中的 OffsetDateTime minusMinutes() 方法及示例
Java中 OffsetDateTime 类的minusMinutes()方法返回此 OffsetDateTime 的副本,其中从解析的日期和时间中减去指定的分钟数。
句法:
public OffsetDateTime minusMinutes(long minutes)
参数:此方法接受单个参数分钟,它指定要从解析日期中减去的分钟数。它也可以是负数,在这种情况下,它会增加分钟数。
返回值:它基于此日期时间返回一个 OffsetDateTime,减去分钟数且不为空。
异常:当超出支持的数据和时间范围时,程序会抛出DateTimeException 。
下面的程序说明了minusMinutes()方法:
方案一:
Java
// Java program to demonstrate the minusMinutes() 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);
// Subtracts the number of minutes
System.out.println("Date1 after subtracting minutes: "
+ date1.minusMinutes(-120));
}
}
Java
// Java program to demonstrate the minusMinutes() 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);
// Subtracts the number of minutes
System.out.println("Date1 after subtracting minutes: "
+ date1.minusMinutes(140));
}
}
输出:
Date1: 2018-12-12T13:30:30+05:00
Date1 after subtracting minutes: 2018-12-12T15:30:30+05:00
方案二:
Java
// Java program to demonstrate the minusMinutes() 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);
// Subtracts the number of minutes
System.out.println("Date1 after subtracting minutes: "
+ date1.minusMinutes(140));
}
}
输出:
Date1: 2018-12-12T13:30:30+05:00
Date1 after subtracting minutes: 2018-12-12T11:10:30+05:00
参考:https: Java/time/OffsetDateTime.html#minusMinutes(long)