当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java ZonedDateTime minusMinutes()用法及代码示例


ZonedDateTime类的minusMinutes()方法用于从此ZonedDateTime中减去分钟数,并在减去后返回ZonedDateTime的副本。此方法在即时时间轴上运行,因此减去一分钟将始终是一分钟。此实例是不可变的,不受此方法调用的影响。

用法:

public ZonedDateTime minusMinutes(long minutes)

参数:此方法接受一个参数分钟,表示要减去的分钟,它可以为负。


返回值:此方法基于此日期时间返回ZonedDateTime,并减去分钟,而不是null。

异常:如果结果超出支持的日期范围,则此方法将引发DateTimeException。

以下示例程序旨在说明minusMinutes()方法:
示例1:

// Java program to demonstrate 
// ZonedDateTime.minusMinutes() 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 minutes: "
                           + zoneddatetime); 
  
        // subtract 15 minutes 
        ZonedDateTime returnvalue 
            = zoneddatetime.minusMinutes(15); 
  
        // print result 
        System.out.println("ZonedDateTime after "
                           + " subtracting 15 minutes: "
                           + returnvalue); 
    } 
}
输出:

ZonedDateTime before subtracting minutes: 2018-12-06T19:21:12.123+05:30[Asia/Calcutta]
ZonedDateTime after subtracting 15 minutes: 2018-12-06T19:06:12.123+05:30[Asia/Calcutta]

示例2:

// Java program to demonstrate 
// ZonedDateTime.minusMinutes() 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 minutes: "
                           + zoneddatetime); 
  
        // subtract 20 minutes 
        ZonedDateTime returnvalue 
            = zoneddatetime.minusMinutes(20); 
  
        // print result 
        System.out.println("ZonedDateTime after "
                           + " subtracting 20 minutes: "
                           + returnvalue); 
    } 
}
输出:

ZonedDateTime before subtracting minutes: 2018-10-25T23:12:31.123+02:00[Europe/Paris]
ZonedDateTime after subtracting 20 minutes: 2018-10-25T22:52:31.123+02:00[Europe/Paris]

参考文献: https://docs.oracle.com/javase/10/docs/api/java/time/ZonedDateTime.html#minusMinutes(long)



相关用法


注:本文由纯净天空筛选整理自AmanSingh2210大神的英文原创作品 ZonedDateTime minusMinutes() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。