當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Java MonthDay now(ZoneId)用法及代碼示例

Java中MonthDay類的now(ZoneId zone)方法用於從指定時區的係統時鍾中獲取當前的month-day。

用法:

public static MonthDay now(ZoneId zone)

參數:此方法接受ZoneId作為參數。



返回值:此方法使用係統時鍾返回當前的month-day。

以下示例程序旨在說明Java中MonthDay的now(ZoneId zone)方法:

程序1:

// Java program to demonstrate 
// MonthDay.now(ZoneId zone) method 
  
import java.time.*; 
import java.time.temporal.*; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // apply now(ZoneId zone) method 
        // of MonthDay class 
        MonthDay result = MonthDay.now( 
            ZoneId.systemDefault()); 
  
        // print both month and day 
        System.out.println("MonthDay:"
                           + result); 
    } 
}
輸出:
MonthDay:--05-09

程序2:

// Java program to demonstrate 
// MonthDay.now(ZoneId zone) method 
  
import java.time.*; 
import java.time.temporal.*; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // apply now(ZoneId zone) method 
        // of MonthDay class 
        MonthDay result = MonthDay.now( 
            ZoneId.systemDefault()); 
  
        // print only month 
        System.out.println("Month:"
                           + result.getMonth()); 
    } 
}
輸出:
Month:MAY

參考文獻: https://docs.oracle.com/javase/10/docs/api/java/time/MonthDay.html#now(java.time.ZoneId)




相關用法


注:本文由純淨天空篩選整理自pp_pankaj大神的英文原創作品 MonthDay now(ZoneId) method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。