Friday, September 5, 2025
HomeLanguagesJavaSimpleDateFormat get2DigitYearStart() Method in Java with Examples

SimpleDateFormat get2DigitYearStart() Method in Java with Examples

The get2DigitYearStart() Method of SimpleDateFormat class is used to return the start of 100 year period that was set during parsing or set2DigitYearStart() method. It returns the beginning date of the 100-year period 2-digit years those are interpreted as being set within the Date.
Syntax: 
 

public Date get2DigitYearStart()

Parameters: The method does not take any parameters.
Return Value: The method returns the beginning date of the 100-year period 2-digit years those are interpreted as being set within the Date
Below programs illustrate the working of get2DigitYearStart() Method of SimpleDateFormat:
Example 1:
 

Java




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


Output: 

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

 

Example 2:
 

Java




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


Output: 

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

 

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32264 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6634 POSTS0 COMMENTS
Nicole Veronica
11801 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11863 POSTS0 COMMENTS
Shaida Kate Naidoo
6750 POSTS0 COMMENTS
Ted Musemwa
7025 POSTS0 COMMENTS
Thapelo Manthata
6701 POSTS0 COMMENTS
Umr Jansen
6718 POSTS0 COMMENTS