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


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


ofStrict()用于创建ZonedDateTime实例的ZonedDateTime类的方法,严格验证本地日期时间,偏移量和区域ID的组合,其中所有三个Local date-tme,ZoneOffset和ZoneId均作为参数传递。如果偏移量无效,则引发异常。
用法:

public static ZonedDateTime ofStrict(LocalDateTime localDateTime,
                                     ZoneOffset offset,
                                     ZoneId zone)

参数:此方法接受三个参数localDateTime(是本地日期时间),offset(是区域偏移量)和zone(是时区)。

返回值:此方法返回分区的日期时间。


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

// Java program to demonstrate 
// ZonedDateTime.ofStrict() method 
  
import java.time.*; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // create local date time object 
        LocalDateTime ldt 
            = LocalDateTime 
                  .parse("2019-01-29T23:55:59.00"); 
  
        // create ZoneOffset 
        ZoneOffset zoneOffset 
            = ZoneOffset.ofHours(1); 
  
        // create a ZonID 
        ZoneId zone 
            = ZoneId.of("Europe/Paris"); 
  
        // apply ofStrict method 
        // of ZonedDateTime class 
        ZonedDateTime zt 
            = ZonedDateTime 
                  .ofStrict( 
                      ldt, zoneOffset, zone); 
  
        // print the result 
        System.out.println("ZonedDateTime is "
                           + zt); 
    } 
}
输出:
ZonedDateTime is 2019-01-29T23:55:59+01:00[Europe/Paris]

示例2:

// Java program to demonstrate 
// ZonedDateTime.ofStrict() method 
  
import java.time.*; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // create local date-time object 
        LocalDateTime ldt 
            = LocalDateTime 
                  .parse("2019-01-29T23:55:59.00"); 
  
        // create ZoneOffset 
        ZoneOffset zoneOffset 
            = ZoneOffset.ofHours(0); 
  
        // create a ZonID 
        ZoneId zone = ZoneId.of("UTC"); 
  
        // apply ofStrict method 
        // of ZonedDateTime class 
        ZonedDateTime zt 
            = ZonedDateTime 
                  .ofStrict( 
                      ldt, zoneOffset, zone); 
  
        // print the result 
        System.out.println("ZonedDateTime is "
                           + zt); 
    } 
}
输出:
ZonedDateTime is 2019-01-29T23:55:59Z[UTC]

参考文献:
https://docs.oracle.com/javase/10/docs/api/java/time/ZonedDateTime.html#ofStrict(java.time.LocalDateTime, java.time.ZoneOffset, java.time.ZoneId)



相关用法


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