In this article we will show you the solution of get year from date java, programmers frequently deal with dates, and being able to extract certain details like the year is crucial in a variety of contexts.
To assist you comprehend the procedure for obtaining the year from a given date using SimpleDateFormat, we will present a step-by-step instruction with code examples.
We will use the SimpleDateFormat class from the java.text package, which enables us to format and parse dates in a certain pattern, to achieve our purpose.
In order to parse a date string and determine the year from it, we will first create a SimpleDateFormat instance with the necessary date format.
Step By Step Guide On Get Year From Date Java :-
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class Year { public static int getYearFromDate(String dateString, String dateFormat) throws ParseException { SimpleDateFormat sdf = new SimpleDateFormat(dateFormat); Date date = sdf.parse(dateString); return date.getYear() + 1900; } public static void main(String[] args) { String inputDate = "2022-07-21"; String inputDateFormat = "yyyy-MM-dd"; try { int year = getYearFromDate(inputDate, inputDateFormat); System.out.println("Year: " + year); } catch (ParseException e) { System.out.println("Error occurred while parsing the date: " + e.getMessage()); } } }
- GetYearFromDate and main are two of the methods included in the Year class.
- A public static method called "getYearFromDate" requires the following two arguments: dateString: A string used to express dates in various formats. dateFormat: A string encoding the dateString's format.
- A SimpleDateFormat object sdf is generated using the given dateFormat inside the getYearFromDate function. Dates are parsed and formatted using the SimpleDateFormat class, which is a component of the Java Date-Time API.
- The dateString is parsed into a Date object using the sdf.parse(dateString) method. If the supplied date string does not adhere to the defined format, the parse method may throw a ParseException.
- The parsed date is represented by the Date object. The Date class's getYear() method, however, is no longer supported. Date objects may be used to retrieve the year by calling date.getYear(), which returns the proper year when multiplied by 1900. The real year is obtained by adding 1900 to the year returned by the getYear() function.
- The Java application's entry point is the main method.
- In the main procedure, the String variables inputDate and inputDateFormat are created. The format of the inputDate is represented by inputDateFormat, whereas inputDate stands for the date string that needs to be parsed.
- With inputDate and inputDateFormat as parameters, the getYearFromDate function can throw a ParseException.
- A try-catch block surrounds the method call to address any potential ParseExceptions that might arise when parsing the date.
- The retrieved year is kept in the year variable if date parsing is successful.
- System.out.println("Year:" + year); prints the outcome.
Conclusion :-
In this article, we learnt how to use Java's SimpleDateFormat to extract the year from a date.
The supplied date text was parsed into a Date object using the SimpleDateFormat class, and the year was then extracted.
It is essential for many applications, such as data analysis, event scheduling, and financial calculations, to be able to extract specific components from dates.
Being able to use SimpleDateFormat successfully and efficiently gives Java developers the ability to work with dates.
I hope this article on get year from date java helps you and the steps and method mentioned above are easy to follow and implement.