String date = "July 2 , 2013";
String year = ""+ date.charAt(date.length()-2) + date.charAt(date.length()-1);
System.out.println(year);
int year2 = Integer.parseInt(year);
System.out.println(year2);
How to get last two digit from a year which a user inputs
-
14-07-2023 - |
Question
I Have been playing with this for while, and I can not seem to get my mind around how I can get this task accomplished.
My task :
I need get the last two digits of a year that a user has entered.
Example :
A user enters the July 2 , 2014; I need to get the last two digits of year 2014 which is "14" and divide it by 4. This will give me 3.5; however I will disregard the ".5" and just keep the 3.
Research :
I have been reading my Textbook, and seen one approach that I may be able to use which includes the string builder class. However my book has a very brief description and shows no example which I can constructively use to accomplish me task.
Progress:
This is what I have so far, it is basically a template of how I want my program; I just need some help getting the last 2 digits of a year.
public class DateCalc
{
public static void main (String[] args)
{
String month;
int day;
int year;
Scanner keyboard = new Scanner(System.in);
// receiving input
System.out.print( "Please Enter The Month Of The Date : ");
month = keyboard.nextLine();
// receiving input
System.out.print( "Please Enter The Day Of The Date : ");
day = keyboard.nextInt();
// receiving input
System.out.print( "Please Enter The Year OF The Date : ");
year = keyboard.nextInt();
switch(month)
{
// keys are numbered indexes that I need for this, please disregard
case "January" :
int janKey = 1;
break;
case "February":
int febKey = 4;
break;
case "March":
int marKey = 4;
break;
case "April":
int aprKey = 0;
break;
case "May":
int maykey = 2;
break;
case "June":
int junKey = 5;
break;
case "July":
int julKey = 0;
break;
case "August":
int augKey = 3;
break;
case "September":
int septKey = 6;
break;
case "October":
int octKey = 1;
break;
case "November":
int novKey = 4;
break;
case "December":
int decKey = 4;
break;
// IN MY DEFUALT CASE " inputValidation " WILL BE EXECUTED
default:
JOptionPane.showMessageDialog(null," Invalid Entry Please Try Again " );
}
}
}
La solution 4
Autres conseils
You could do something like String yearString = Integer.toString(year).substring(2)
, which will first convert the integer to a string, then get the substring consisting of everything after the second character. Then to turn it back into an integer, try int yearInt = Integer.parseInt(yearString)
IMHO the cleaner option would be to convert the day,month and year into a Calendar. And then get the two digit year from the calendar.
Something like this:
Calendar cal = Calendar.getInstance();
cal.set(year, month, day);
SimpleDateFormat sdf = new SimpleDateFormat("yy"); // Just the year, with 2 digits
String formattedDate = sdf.format(Calendar.getInstance().getTime());
System.out.println(formattedDate);
One thing you can do is mod the year by 100 to get the last 2 digits. Then you can do whatever math you want with it.
This can be extended to get an arbitrary number of digits; modding a number by 10^n
yields the last n
digits of the number.
Edit because I was stupid and thought year
was a String