Java Program to Extract Last two Digits of a Given Year
Last Updated :
02 Nov, 2020
As the name suggests where there is an execution to be operated required to deal with digits of the number, the modulo operator plays a vital role. Here the goal is to extract the last digits of a number. So making the problem easier to think about what if the goal is to extract the last digit from a number. Here in this case number is representing the year. The mathematical concept involved in extracting the last is to divide the given number by such a number that the remainder should be the same as that of the last digit. In order to extract the last digit that number with which the given number has to be divided must be 10.
Modulo is represented by '%' and is used to get the remainder
Example: Extracting Last One Digit From A Number
Random Number = 1997
Last Digit = 7
Goal: 1997 % (x) = 7 // Goal is Divide 1997 by x such that remainder is 7
if x = 10 // 1997 % 10 = 7 that is the last digit
Now, in order to extract the last two digits, we will be treating the number with the same approach just the given number will be modulo by 100 as per the mathematics unit standard.
Example: Extracting the Last Two-Digit From A Number
Input : year = 2020
Output : 20
Input : year = 1983
Output : 83
Input : year = 2000
Output : 00
We can implement this using 2 different methods:
A. Using Modulo Arithmetic Operator: We can use the modulo operator (%) to extract the last two digits by taking the modulo of the year by 100.
Internal Working Of Mathematics Unit System
Let us consider 1983, we can write it as
1983 = 1*1000 + 9*100 + 8*10 + 3
So when we take modulo by 100, we will just have the last two digits as remainder.
1983 % 100 = 1*1000 % 100 + 9*100 % 100 + 8*10 % 100 + 3 % 100 = 0 + 0 + 8*10 + 3 = 83
The implementation of the above approach is described below
Java
// Java code to extract last two digits of a year
// Importing Classes/Files
import java.util.*;
public class GFG {
// Main Driver Code
public static void main(String args[])
{
// Initializing year as String
int year = 1983;
// Printing last two digits of a number
// by modulo with 100
System.out.print(year % 100);
}
}
B. Using String substring() method: This method returns a new string that is a substring of the given string. Thus, to extract the last two digits we need to get the substring after index 2.
Java
// Java code to extract last two digits of a year
public class GFG {
// Function to extract last to digits of a year
static int extractLastTwo(String year)
{
// using substring() to extract the last two digit
// as substring
String lastTwoDigits = year.substring(2);
return Integer.parseInt(
lastTwoDigits); // Returning last two digits as
// an integer
}
public static void main(String args[])
{
// Initializing year as String
String year = "1983";
System.out.print(extractLastTwo(year));
}
}
Similar Reads
Java Program to Get Year From Date Java is the most powerful programming language, by which we can perform many tasks and Java is an industry preferable language. So it is filled with a huge amount of features. Here we are going to discuss one of the best features of Java, which is how to get a year from date using Java. Methods: The
4 min read
Java Program to Display Name of Months of Calendar Year in Short Format As we know that in a calendar year, there are a total of 12 Months. In order to convert the name of months to a shorter format, there are basically two ways, ie we can either make use of DateFormatSymbols class or SimpleDateFormat class. These classes have methods that are used to convert the names
3 min read
Java Program to Display Dates of a Calendar Year in Different Format As different countries do opt for different formats. So here the goal is simply to print dates of calendar in different years. The generic symbolic notation opted across the globe is: Symbolic NotationDepictsyyearMmonth in year dday in month Eday of week Concept: Whenever it comes down to the date a
4 min read
Extract all integers from the given String in Java In Java, we can extract integers from a given string using several approaches such as regular expressions, character iteration, or the split() method. In this article, we will learn how to extract all integers from the given String in Java.Example:We will use regular expressions to extract all integ
3 min read
How to Convert a String to a LocalDate in Java? Converting a String to a LocalDate in Java is a common operation when you are dealing with date inputs from users. Java provides the LocalDate class in the java.time package for representing a date without time information. LocalDate class is part of the java.time package introduced in Java 8. Strin
2 min read
How to Calculate the Week Number of the Year for a Given Date in Java? In Java, we have the Calendar class and the newer LocalDate class from java.time package. We can use these classes to find which week number of the year for a given date. The java.time package offers better functionality than the older date and time classes such as Calendar class. In this article, w
3 min read