Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(JAVA programming) write a program named ConvertDate that converts a date entere

ID: 3688329 • Letter: #

Question

(JAVA programming) write a program named ConvertDate that converts a date entered by the user into Another form. The user's input will have the form month day, year The month will be one of the words January, February,… The letters in the month may be lowercase, uppercase, or any mixture of the two. The day is a one- or two-digit number. The year is any number of digits. There may be any number of spaces (l) before the month, (2) between the month and the day, (3) between the day and the year, and (4) after the year. You may assume that there is at least one Space between the month and day and between the day and year. You may also assume that there is a comma after the day. The converted date must have the form day month year with one space between the day and month and between the month and year. The first letter of the month must be uppercase, and the remaining letters must be lowercase. The following example shows what the user will see on the screen: Enterdate to be converted: april 28, 2003 Converted date: 28 April 2003 Hint: Use the indexOf and last IndexOf methods to locate the month. day, and year within the string entered by the user. and then use the substring method to extract them.

Explanation / Answer

/**The java program DateConversion that prompts user to enter date value in the format of month name day, year with
* extra white spaces. The the program prints th day removing extraces in the order of month name in first letter in capital , day and year to console.
* */
//DateConversion.java
import java.util.Scanner;
public class DateConversion
{
   public static void main(String[] args)
   {
       Scanner scanner=new Scanner(System.in);
       // Inputs the month, day, and year
       System.out.println("Enter date to be converted: ");
       String date = scanner.nextLine();
      
       // call trim on string date to clear extra white spaces
       date = date.trim();
       // Get first index of space in the date
       int monthIndex = date.indexOf(" ");
       //Get the last index of the comma in the date
       int dayIndex = date.lastIndexOf(",");
      
       //Starting letter of the month
       String monthFirstLetter = date.substring(0,1);
       //Get rest of characters from the date start from index=1
       String otherMonthLetter = date.substring(1);
      
       //Convert first character to upper case and remaining character to lower case      
       String upperCaseMonth = monthFirstLetter.trim().toUpperCase()
               + otherMonthLetter.trim().toLowerCase();
      
       //Get the month name that is upt to monthIndex and trim for remove extra space
       String month = upperCaseMonth.trim().substring(0, monthIndex).trim();
       //Get the day that is upt to dayIndex and trim for remove extra space
       String day = date.trim().substring(monthIndex + 1, dayIndex).trim();
       //Get the year value that is dayIndex+1 and trim for remove extra space
       String year = date.trim().substring(dayIndex + 1).trim();
      
       //print the day,month and year to console
       System.out.println("Formatted Date: " + day + " " + month + " " + year);
   }
}

----------------------------------------------------------------------------------------------------------------------------------------------------------------

Sample output:

Enter date to be converted:
april 28,2003
Formatted Date: 28 April 2003

sample run2:

Enter date to be converted:
april            28,              2003
Formatted Date: 28 April 2003