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


Java SimpleDateFormat get2DigitYearStart()用法及代碼示例


SimpleDateFormat類的get2DigitYearStart()方法用於返回在解析或set2DigitYearStart()方法期間設置的100年期限的開始。它返回100年期限的開始日期(兩位數年份),這些日期被解釋為在“日期”中設置的。

用法:

public Date get2DigitYearStart()

參數:該方法不帶任何參數。


返回值:該方法返回100年期間的開始日期2位數字的年份,這些年份被解釋為在Date中設置

以下示例程序旨在說明SimpleDateFormat的get2DigitYearStart()方法的用法:

示例1:

// Java code to illustrate 
// get2DigitYearStart() method 
  
import java.text.*; 
import java.util.Calendar; 
  
public class SimpleDateFormat_Demo { 
    public static void main(String[] args) 
        throws InterruptedException 
    { 
  
        SimpleDateFormat dt 
            = new SimpleDateFormat("MM/ dd/ yy"); 
  
        try { 
            Calendar cal = Calendar.getInstance(); 
            cal.setTime(dt.parse("01/ 28/ 19")); 
            System.out.println("The Starting Time: "
                               + cal.getTime()); 
  
            // Setting 1916 instead of 2016 
            // Using set2DigitYearStart() method 
            dt.set2DigitYearStart( 
                dt.parse("01/ 01/ 1900")); 
            cal.setTime(dt.parse("05/ 12/ 17")); 
            System.out.println("The New Time: "
                               + cal.getTime()); 
  
            // Use of get2DigitYearStart() method 
            // to check start year 
            cal.setTime(dt.get2DigitYearStart()); 
            System.out.println("The start Year: "
                               + cal.get(Calendar.YEAR)); 
        } 
  
        catch (ParseException excpt) { 
            excpt.printStackTrace(); 
        } 
    } 
}
輸出:
The Starting Time: Mon Jan 28 00:00:00 UTC 2019
The New Time: Sat May 12 00:00:00 UTC 1917
The start Year: 1900

示例2:

// Java code to illustrate 
// get2DigitYearStart() method 
  
import java.text.*; 
import java.util.Calendar; 
  
public class SimpleDateFormat_Demo { 
    public static void main(String[] args) 
        throws InterruptedException 
    { 
  
        SimpleDateFormat dt 
            = new SimpleDateFormat("MM/ dd/ yy"); 
  
        try { 
            Calendar cal = Calendar.getInstance(); 
            cal.setTime(dt.parse("01/ 28/ 19")); 
            System.out.println("The Starting Time: "
                               + cal.getTime()); 
  
            // Setting 1916 instead of 2016 
            // Using set2DigitYearStart() method 
            dt.set2DigitYearStart( 
                dt.parse("01/ 01/ 2000")); 
            cal.setTime(dt.parse("05/ 12/ 17")); 
            System.out.println("The New Time: "
                               + cal.getTime()); 
  
            // Use of get2DigitYearStart() method 
            // to check start year 
            cal.setTime(dt.get2DigitYearStart()); 
            System.out.println("The start Year: "
                               + cal.get(Calendar.YEAR)); 
        } 
  
        catch (ParseException excpt) { 
            excpt.printStackTrace(); 
        } 
    } 
}
輸出:
The Starting Time: Mon Jan 28 00:00:00 UTC 2019
The New Time: Fri May 12 00:00:00 UTC 2017
The start Year: 2000


相關用法


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