Write a program called ‘DatePrinter’ which includes a method ‘void printDate()’. There is no return value and no parameters. The method will use a scanner (from java.util.Scanner) to get a string input from the user using the console of format “MM/DD/YYYY” where MM is the month, DD is the day, and YYYY is the year. Each one of these fields will always have two characters by adding leading zeros to single digit numbers, for example if I want to use January for the month, I will input 01 instead of just 1 for MM, so a full example for January 5th, 2010 would be “01/05/2010” as the input string. After taking this entire string as format “MM/DD/YYYY” print out the month, the day, and the year in this format:
The day is: DD
The month is: MM
The year is: YYYY
Note: Could you plz go through this code and let me
know if u need any changes in this.Thank You
_________________
// DatePrinter.java
import java.util.Scanner;
public class DatePrinter {
void printDate()
{
String day,month,year;
/*
* Creating an Scanner class object
which is used to get the inputs
* entered by the user
*/
Scanner sc = new
Scanner(System.in);
String date;
System.out.print("Enter Date (in
MM/DD/YYYY format) :");
date=sc.next();
int indx1=date.indexOf("/");
month=date.substring(0,indx1);
int
indx2=date.lastIndexOf("/");
day=date.substring(indx1+1,indx2);
year=date.substring(indx2+1);
System.out.println("The day is
:"+day);
System.out.println("The month is
:"+month);
System.out.println("The year is
:"+year);
}
}
________________________
// Test.java
public class Test {
public static void main(String[] args) {
DatePrinter d=new
DatePrinter();
d.printDate();
}
}
___________________________
Output:
Enter Date (in MM/DD/YYYY format) :01/10/2019
The day is :10
The month is :01
The year is :2019
______________________________Thank You
Get Answers For Free
Most questions answered within 1 hours.