. Advertisement .
..3..
. Advertisement .
..4..
There is a package of java.time contained in Java. This time package will provide API for the time covering dates, hours, instances, and durations. In detail, various classes are offered such as Clock, LocalDate Time, LocalDate, LocalTime, etc. When using these classes, you can get detailed information about dates and times simpler. In this post, we will introduce how to execute java functions to get the current year in accordance with java.time package classes.
You can apply various beneficial methods in java.time package. Some are listed in the following:
- The method
getYear()
has a return value as an integer that represents the year saved in the object of the current LocalDate. - The method
getMonth()
has a return value as an object within the class ofjava.timeMonth
represents the month of the object in the LocalDate. - The method
getDaYofMonth()
has a return value as an integer representing the day in the object of the LocalDate.
How to execute java functions to get the current year
1. Execute the java.Util.Date in Java Functions to Get the Current Year
In the previous JDK versions, the method getYear()
is contained by the class java.util.Date
can be executed for getting the current year. However, it is replaced by the method Calender.get(Calendar.YEAR)
now.
For example, by minus 1900 from the object of the current date, we used this method for returning the value. You can refer to the below code snippet.
import java.util.Date;
public class Java_Date_GetYear{
public static void main(String[] args) {
Date dt=new Date();
int year=dt.getYear();
System.out.println("Year for date object is : "+year);
int current_Year=year+1900;
System.out.println("Current year is : "+current_Year);
}
}
Output:
Year for date object is : 121
Current year is : 2021
As the example above, 121 is the date object we get but our purpose is to get the present year. Therefore, to achieve the last result, we must plus 1990 one more time.
2. Execute the Class LocalDate in Java Functions to Get the Current Year
With this class, the methods contained in it such as getMonth()
, getyear()
, etc will be used for getting the present month or year as required by users.
import java.time.LocalDate;
class Current_Year {
public static void main(String args[]) {
//Getting the current date value of the system
LocalDate current_date = LocalDate.now();
System.out.println("Current date: "+current_date);
//getting the current year from the current_date
int current_Year = current_date.getYear();
System.out.println("Current year: "+current_Year);
}
}
Output:
Current date: 2021-06-06
Current year: 2021
Following the code above, the current year, time, and date of the system where the code execution is happening is returned in the present year as output. This way, we used the method now()
of the class LocalDate()
.
3. Execute the Class java.util.Calendar in Java Functions to Get the Current Year
The class Calendar is known as an abstract class in Java that can support converting the fields of date and calendar such as hour, year, month, etc.
The static method getInstance()
can be used for initiating sub-classes as the class Calendar is an abstract one. Also, a constructor method cannot be used for instance creation. The current year can be provided by the method get(Calendar.Year)
within the system.
import java.util.Calendar;
public class Get_Current_Year {
public static void main(String args[])
{
Calendar cal = Calendar.getInstance();
System.out.println("The Current Year is:" + cal.get(Calendar.YEAR));
}
}
Output:
The Current Year is:2021
Also, we can use the class Calendar for printing the present year of a certain date. This way, a sub-class in Calendar must be used, which is called GregorianCalendar()
.
import java.util.*;
class Get_Current_Year2 {
public static void main(String args[])
{
// Creating a calendar object
Calendar c
= new GregorianCalendar(
2020, 02, 11);
// Getting the value of the year from calendar object
int year = c.get(Calendar.YEAR);
// Printing the year
System.out.println("Year: " + year);
}
}
Output:
Year: 2020
4. Execute the Package Joda Time in Java Functions to Get the Current Year
If you are in need of using too many objects of calendar and date, you should consider executing the package Joda Tume. The class java.util.Date is adjustable. The class java.util.calendar contains a few performance-related problems when updating its fields.
There are more efficient methods contained in Joda Time than the class contained in Java packages.
import org.joda.time.DateTime;
import org.joda.time.LocalDateTime;
public class Joda_Time {
public static void main(String[] args)
{
DateTime now = new DateTime();
System.out.println("Current Year: " + now.year().getAsText());
}
}
Output:
Current Year : 2021
As you can see, the Datetime()
object was created for getting the current date of the system. Since Joda Time is not installed available in IDE, we must download this package before using it.
5. Execute the Class java.time.YearMonth in Java Functions to Get the Current Year
Since the class java.time.YearMonth
offers the method getYear()
, we can use it among Java functions to get the current year. By using this method, you can follow the coding demonstration below:
import java.time.YearMonth;
public class Current_Year {
public static void main(String[] args) {
int year = YearMonth.now().getYear();
System.out.println("Current year: "+ year);
}
}
Output:
Current year: 2021
The Bottom Line
Through this article, we have brought you how to execute some Java functions to get the current year. Hope the information shared above can be beneficial to you. If you are looking for other code treatments in other programming languages, you can surf our website to explore more useful posts!
Leave a comment