📌  相关文章
📜  Java中的 SimpleDateFormat set2DigitYearStart() 方法及示例

📅  最后修改于: 2022-05-13 01:54:42.980000             🧑  作者: Mango

Java中的 SimpleDateFormat set2DigitYearStart() 方法及示例

SimpleDateFormat 类set2DigitYearStart()方法用于将 100 年周期设置为 2 位数年份,并将其解释为在用户特定日期开始。该方法解析日期并将日期设置在从starting_Date(starting_Date + 100)年的范围内。
句法:

public void set2DigitYearStart(Date starting_Date)

参数:该方法有一个Date类型的参数starting_date ,该参数指的是方法中的开始日期,范围可达(starting_date + 100)年。
返回值:该方法返回一个 void 类型。
下面的程序说明了 SimpleDateFormat 的 set2DigitYearStart() 方法的工作:
示例 1:

Java
// 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 except) {
            except.printStackTrace();
        }
    }
}


Java
// 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 except) {
            except.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

// 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 except) {
            except.printStackTrace();
        }
    }
}
输出:
The Starting Time: Mon Jan 28 00:00:00 UTC 2019
The New Time: Sat May 12 00:00:00 UTC 1917