java.text.SimpleDateFormat get2DigitYearStart()

Description :

This java tutorial shows how to use the get2DigitYearStart()  method of SimpleDateFormat class of java.text package. This method returns the beginning date of the 100-year period 2-digit years are interpreted as being within.

Method Syntax :

public Date get2DigitYearStart()

Parameter Input :

 

DataType Parameter Description
N/A N/A N/A

 

Method Returns :

This method returns the start of the 100-year period into which two digit years are parsed.

Compatibility Version :

Requires Java 1.2 and up

Exception :

N/A

Discussion :

As we have discussed on the method set2DigitYearStart(Date startDate), this method is to fix the issue on the parsing of date input with two digit year.

Now on get2DigitYearStart() method, this return what has been set on the set2DigitYearStart(Date startDate).

Java Code Example :

This java example source code demonstrates the use of get2DigitYearStart()  method of SimpledDateFormat class. At first  we have set the baseline date by calling set2DigitYearStart(Date startDate). As you would have noticed, we have set 05/10/1825 as the baseline year. We intended to get the interpretation of year 14, now we have the predicament if the baseline year would be 1814 or 1914. From the description of set2DigitYearStart(), this method sets 100-year period 2-digit years will be interpreted as being in to begin on the date the user specifies. 1814 is less than 1825 thus the parsing of date would be 1914.

package com.javatutorialhq.java.examples;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

/*
 * This example source code demonstrates the use of  
 * get2DigitYearStart() method 
 * of SimpleDateFormat class
 */

public class Get2DigitYearStartExample {

	public static void main(String[] args) throws InterruptedException {

		SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yy");
		try {
			Calendar cal = Calendar.getInstance();
			cal.setTime(sdf.parse("10/25/14"));		
			System.out.println("Time 1:"+cal.getTime());
			
			// we want 1914 instead of 2014		
			sdf.set2DigitYearStart(sdf.parse("05/10/1825"));
			
			// set the value of calendar again 
			// to check if we now have the correct year
			cal.setTime(sdf.parse("10/25/14"));
			System.out.println("Time 2:"+cal.getTime());
			
			// check the baseline year
			cal.setTime(sdf.get2DigitYearStart());
			System.out.println("Baseline Year:"+cal.get(Calendar.YEAR));
			
		} catch (ParseException e) {			
			e.printStackTrace();
		}
		
		
	}

}

Sample Output :

Running the get2DigitYearStart() method example source code of Calendar class will give you the following output:

SimpleDateFormat get2DigitYearStart() method example

SimpleDateFormat get2DigitYearStart() method example

Exception Scenario :

N/A

Similar Method :

  • N/A

Suggested Reading List :

References :