Show Year In Two Digits Format In Java Date
In Java, extracting the last two digits of the current year is a common requirement in date-related operations. This can be useful for formatting dates, generating year-based codes, or working with legacy systems. There are multiple ways to achieve this, using different Java date and time APIs. Let us delve into understanding how Java Date can be used to show the year in two digits.
1. Utilizing Calendar with the Modulo Operator
The traditional Calendar class provides methods to retrieve date and time values. It was introduced in Java 1.1 and is still widely used in older applications. However, it is considered outdated and has been replaced by the java.time
package in Java 8.
1.1 Code Example
01 02 03 04 05 06 07 08 09 10 11 12 | import java.util.Calendar; public class LastTwoDigitsYear { public static void main(String[] args) { Calendar calendar = Calendar.getInstance(); int year = calendar.get(Calendar.YEAR); int lastTwoDigits = year % 100 ; System.out.println( "Current Year: " + year); System.out.println( "Last Two Digits of the Year: " + lastTwoDigits); } } |
1.1.1 Code Explanation and Output
The Java program extracts the last two digits of the current year using the Calendar
class. It first gets an instance of the calendar using Calendar.getInstance()
, retrieves the full four-digit year using calendar.get(Calendar.YEAR)
, and then extracts the last two digits using year % 100
. Finally, it prints the current year and its last two digits.
When the code is executed, the following output appears on the IDE console.
1 2 | Current Year: 2025 Last Two Digits of the Year: 25 |
2. Leveraging SimpleDateFormat
The SimpleDateFormat class provides an easy way to format and parse dates. SimpleDateFormat
offers several features:
- It allows formatting and parsing of dates into strings.
- The format pattern
"yy"
is used to get the last two digits of the year. - This class is thread-unsafe, meaning multiple threads should not use the same instance.
2.1 Code Example
01 02 03 04 05 06 07 08 09 10 11 | import java.text.SimpleDateFormat; import java.util.Date; public class LastTwoDigitsYearFormat { public static void main(String[] args) { SimpleDateFormat sdf = new SimpleDateFormat( "yy" ); String lastTwoDigits = sdf.format( new Date()); System.out.println( "Last Two Digits of the Year: " + lastTwoDigits); } } |
2.1.1 Code Explanation and Output
The LastTwoDigitsYearFormat
Java class prints the last two digits of the current year using the SimpleDateFormat
class. Inside the main
method, an instance of SimpleDateFormat
is created with the format "yy"
, which extracts only the last two digits of the year from the current date. The format
method is then used on a new Date
object to get the formatted year as a string. Finally, the result is printed to the console using System.out.println
.
When the code is executed, the following output appears on the IDE console.
1 | Last Two Digits of the Year: 25 |
3. Utilizing the java.time API
Since Java 8, the java.time package provides a modern approach to handling dates and times. java.time
API offers the following advantages:
- Immutable and thread-safe.
- More efficient than
Calendar
andSimpleDateFormat
. - Recommended for all new Java applications.
3.1 Code Example
01 02 03 04 05 06 07 08 09 10 11 12 13 14 | import java.time.LocalDate; import java.time.Year; public class LastTwoDigitsJavaTime { public static void main(String[] args) { int lastTwoDigits = Year.now().getValue() % 100 ; System.out.println( "Last Two Digits of the Year: " + lastTwoDigits); // Alternative approach using LocalDate int lastTwoDigitsAlternative = LocalDate.now().getYear() % 100 ; System.out.println( "Alternative: Last Two Digits of the Year: " + lastTwoDigitsAlternative); } } |
3.1.1 Code Explanation and Output
The LastTwoDigitsJavaTime
Java class extracts and prints the last two digits of the current year using Java’s Year
and LocalDate
classes from the java.time
package. In the main
method, Year.now().getValue() % 100
calculates the last two digits by retrieving the current year and applying the modulus operator. The result is printed using System.out.println
. Additionally, an alternative approach is demonstrated using LocalDate.now().getYear() % 100
, which achieves the same outcome by obtaining the full year from LocalDate
and extracting its last two digits before printing the result.
When the code is executed, the following output appears on the IDE console.
1 2 | Last Two Digits of the Year: 25 Alternative: Last Two Digits of the Year: 25 |
4. Conclusion
There are several ways to extract the last two digits of the current year in Java, depending on the Java version and project requirements. The Calendar
class is suitable for legacy Java versions, while SimpleDateFormat
is useful for formatting needs. For modern Java applications, the java.time
API is the preferred choice due to its cleaner and more efficient design.