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


Java SimpleDateFormat set2DigitYearStart()用法及代码示例


SimpleDateFormat类的set2DigitYearStart()方法用于设置100年的2位数字年份,并将其解释为以user-specific日期开始。该方法解析日期,并将日期设置在starting_Date到(starting_Date + 100)年的范围内。

用法:

public void set2DigitYearStart(Date starting_Date)

参数:该方法采用日期类型的一个参数starting_date,该参数表示方法中的开始日期,范围可以长达(starting_date + 100)年。


返回值:该方法返回一个空类型。

以下示例程序旨在说明SimpleDateFormat的set2DigitYearStart()方法的用法:
示例1:

// Java code to illustrate set2DigitYearStart() 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("10/ 27/ 16")); 
            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("06/ 12/ 16")); 
            System.out.println("The New Time: "
                               + cal.getTime()); 
        } 
  
        catch (ParseException excpt) { 
            excpt.printStackTrace(); 
        } 
    } 
}
输出:
The Starting Time: Thu Oct 27 00:00:00 UTC 2016
The New Time: Mon Jun 12 00:00:00 UTC 1916

示例2:

// Java code to illustrate set2DigitYearStart() 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()); 
        } 
  
        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


相关用法


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