54.5k views
0 votes
Write a JAVA application program that will get a line of text containing a date in US form from the user, and then change this date to European form. Begin by asking the user to enter a date in the form of month/day/year. Store this date in a String variable. Next, use the appropriate String methods to swap the month and day parts of the date, and replace the slash marks with periods. Print the revised String to the screen. Please note that users are allowed to input the year in two digits or four digits, and input the month and day in one digit or two digits. Your program should be able to handle all possible cases.

1 Answer

5 votes

Answer:

The Java code is given below with appropriate comments as well as the sample output

Step-by-step explanation:

import java.util.Scanner;

public class Main

{

public static void main(String[] args) {

System.out.println("Enter a date in the form mon/day/year:");

Scanner scanner = new Scanner(System.in);

String inputDate = scanner. nextLine();

//Split the string string with / as the delimiter

String[] dateComponents = inputDate.split("/");

//Rearrange the date and month part and join with '.' as sepertor

String outputDate = String.join(".", dateComponents[1], dateComponents[0], dateComponents[2]);

System.out.println("Your date in European form is:\\" + outputDate);

}

}

Sample Output

Output 1:

Enter a date in the form mon/day/year:

01/22/2020

Your date in European form is:

22.01.2020

Output 2:

Enter a date in the form mon/day/year:

1/22/20

Your date in European form is:

22.1.20

User Llogari Casas
by
5.7k points