Wednesday, July 3, 2024
HomeLanguagesJavaSimpleDateFormat set2DigitYearStart() Method in Java with Examples

SimpleDateFormat set2DigitYearStart() Method in Java with Examples

The set2DigitYearStart() Method of SimpleDateFormat class is used to set the 100-year period 2-digit years and interpret the same as being in to begin on a user-specific date. The method parses the date and set the date in the range from starting_Date to (starting_Date + 100) years.
Syntax: 
 

public void set2DigitYearStart(Date starting_Date)

Parameters: The method takes one parameter starting_date of Date type which refers to the starting date in the method and can range upto (starting_date + 100) years.
Return Value: The method returns a void type.
Below programs illustrate the working of set2DigitYearStart() Method of SimpleDateFormat: 
Example 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();
        }
    }
}


Output: 

The Starting Time: Thu Oct 27 00:00:00 UTC 2016
The New Time: Mon Jun 12 00:00:00 UTC 1916

 

Example 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();
        }
    }
}


Output: 

The Starting Time: Mon Jan 28 00:00:00 UTC 2019
The New Time: Sat May 12 00:00:00 UTC 1917

 

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments