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


Java Clock withZone()用法及代码示例


java.time.Clock.withZone(ZoneId zone)方法是Clock类的一种方法,该方法返回应用了该方法的时钟对象的时钟副本,并带有不同的时区。如果有时钟,并且需要更改时钟区域,但没有其他属性,则使用withZone()方法。此方法将zone作为参数,这是需要更改的时区。它返回带区域的时钟,与参数中传递的区域相同。

用法:

public abstract Clock withZone(ZoneId zone)

参数:此方法采用ZoneId类型的强制性参数区域,在其中需要更改时区。


返回:此方法返回应用了此方法的时钟对象的时钟副本,并以不同的时区作为参数传递。

例:

Code:
//Clock with default zone
Clock clock1=Clock.systemUTC();
ZoneId zone = ZoneId.of("Asia/Calcutta");
Clock clock2 = clock1.withZone(zone);
System.out.println(clock2.toString());

Output::
SystemClock[Asia/Calcutta]

Explanation::
when withZone() is called for Clock object clock1 with zoneId "Asia/Calcutta",
then the withZone() method will return a Clock whose Zone is "Asia/Calcutta".

下面的程序演示了java.time.Clock类的withZone()方法:

程序1:在withZone()的帮助下,创建具有与“第一时钟”相似的属性但zoneId等于“Asia/Calcutta”的时钟。

// Java program to demonstrate 
// withZone() method of Clock class 
  
import java.time.*; 
  
// create class 
public class withZoneMethodDemo { 
  
    // Main method 
    public static void main(String[] args) 
    { 
        // create a base Clock with systemUTC() method 
        Clock baseclock = Clock.systemUTC(); 
  
        // get ZonedDateTime object from baseclock 
        // clock instant to get date time 
        ZonedDateTime baseTime = baseclock 
                                     .instant() 
                                     .atZone(baseclock.getZone()); 
  
        // print details of ZonedDateTime 
        System.out.println("ZonedDateTime of baseclock "
                           + baseTime.toString()); 
  
        // create ZoneId object with Zone Asia/Calcutta 
        ZoneId zone = ZoneId.of("Asia/Calcutta"); 
  
        // apply withZone() to change zone from utc to Asia/Calcutta 
        Clock clockWithOtherZone = baseclock.withZone(zone); 
  
        // get ZonedDateTime object from clockWithOtherZone 
        // clock instant to get date time 
        ZonedDateTime calcuttaTime = clockWithOtherZone 
                                         .instant() 
                                         .atZone(clockWithOtherZone.getZone()); 
  
        // print details of ZonedDateTime 
        System.out.println("ZonedDateTime of clockWithOtherZone "
                           + calcuttaTime.toString()); 
    } 
}
输出:
ZonedDateTime of baseclock 2018-08-24T08:09:17.354Z
ZonedDateTime of clockWithOtherZone 2018-08-24T13:39:17.539+05:30[Asia/Calcutta]

程序2:使用getZone()为withZone()创建的时钟打印zoneId。

// Java program to demonstrate 
// withZone() method of Clock class 
  
import java.time.*; 
  
// create class 
public class withZoneMethodDemo { 
  
    // Main method 
    public static void main(String[] args) 
    { 
        // create a base Clock with systemDefaultZone() method 
        Clock baseclock = Clock.systemDefaultZone(); 
  
        // print details of ZonedDateTime 
        System.out.println("baseclock Zone:"
                           + baseclock.getZone()); 
  
        // create ZoneId object with Zone Asia/Calcutta 
        ZoneId zone = ZoneId.of("Asia/Calcutta"); 
  
        // apply withZone() to change zone 
        // of baseclock to Asia/Calcutta 
        Clock clockWithOtherZone = baseclock.withZone(zone); 
  
        // print details of ZonedDateTime 
        System.out.println("clockWithOtherZone Zone:"
                           + clockWithOtherZone.getZone()); 
    } 
}
输出:
baseclock Zone:Etc/UTC
clockWithOtherZone Zone:Asia/Calcutta

参考:
https://docs.oracle.com/javase/8/docs/api/java/time/Clock.html#withZone-java.time.ZoneId-



相关用法


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