Java中的 ZonedDateTime minusWeeks() 方法及示例
ZonedDateTime类的minusWeeks()方法,用于从此 ZonedDateTime 中减去周数,并在减法后返回 ZonedDateTime 的副本。此方法在本地时间线上运行,将周数减去本地日期时间,然后减去本地周数date-time 被转换回 ZonedDateTime,使用区域 ID 获取偏移量。转换回 ZonedDateTime 时,如果本地日期时间重叠,则尽可能保留偏移量,否则将使用较早的偏移量。此实例是不可变的,不受此方法调用的影响。
句法:
public ZonedDateTime minusWeeks(long weeks)
参数:此方法接受一个参数weeks ,表示要减去的周数,它可以是负数。
返回值:此方法返回基于此日期时间减去周数的ZonedDateTime 。
异常:如果结果超出支持的日期范围,此方法将引发DateTimeException 。
下面的程序说明了 minusWeeks() 方法:
方案一:
// Java program to demonstrate
// ZonedDateTime.minusWeeks() method
import java.time.*;
public class GFG {
public static void main(String[] args)
{
// create a ZonedDateTime object
ZonedDateTime zoneddatetime
= ZonedDateTime.parse(
"2018-12-06T19:21:12.123+05:30[Asia/Calcutta]");
// print instance
System.out.println("ZonedDateTime before"
+ " subtracting weeks: "
+ zoneddatetime);
// subtract 10 weeks
ZonedDateTime returnvalue
= zoneddatetime.minusWeeks(10);
// print result
System.out.println("ZonedDateTime after "
+ "subtracting 10 weeks: "
+ returnvalue);
}
}
ZonedDateTime before subtracting weeks: 2018-12-06T19:21:12.123+05:30[Asia/Calcutta]
ZonedDateTime after subtracting 10 weeks: 2018-09-27T19:21:12.123+05:30[Asia/Calcutta]
方案二:
// Java program to demonstrate
// ZonedDateTime.minusWeeks() method
import java.time.*;
public class GFG {
public static void main(String[] args)
{
// create a ZonedDateTime object
ZonedDateTime zoneddatetime
= ZonedDateTime.parse(
"2018-10-25T23:12:31.123+02:00[Europe/Paris]");
// print instance
System.out.println("ZonedDateTime before"
+ " subtracting weeks: "
+ zoneddatetime);
// subtract 2 weeks
ZonedDateTime returnvalue
= zoneddatetime.minusWeeks(2);
// print result
System.out.println("ZonedDateTime after "
+ "subtracting 2 weeks: "
+ returnvalue);
}
}
ZonedDateTime before subtracting weeks: 2018-10-25T23:12:31.123+02:00[Europe/Paris]
ZonedDateTime after subtracting 2 weeks: 2018-10-11T23:12:31.123+02:00[Europe/Paris]
参考资料: https: Java/time/ZonedDateTime.html#minusWeeks(long)