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

Date Format Conversion : Create a simple data file like the example shown below

ID: 3777624 • Letter: D

Question

Date Format Conversion: Create a simple data file like the example shown below containing the 4 dates below plus 10 or more additional dates. The file should include 1 date per line and each date should have the form:    Month DayOfTheMonth, Year.   All dates should be in this century. No error checking for invalid dates is necessary.

February 19, 2017

July 4, 2017

November 4, 2018

September 30, 2019

         .

         .

         Write a program that will read the dates in the input date file and create an output data file with the form MonthNumber-DayOfTheMonth-Last2DigitsOfTheYear with no extra spaces. Example:

2-19-17

7-4-17

11-4-18

9-30-19

         .

         .

        

Turn in printouts of the program, the input data file, and the output data file.

Explanation / Answer

DateTest.java

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;

public class DateTest {

  
   public static void main(String[] args) throws FileNotFoundException {
       File file = new File("D:\dates.txt");
       PrintWriter pw = new PrintWriter(new File("D:\dateoutput.txt"));
       if(file.exists()){
           Scanner scan = new Scanner(file);
           while(scan.hasNextLine()){
               String dateStr = "";
               String s = scan.nextLine();
               String str[] = s.replaceAll("^[,\s]+", "").split("[,\s]+");
               String month = str[0].toUpperCase().trim();
               if(month.equals("JANUARY")){
                   dateStr = "1";  
               }
               else if(month.equals("FEBRUARY")){
                   dateStr = "2";  
               }
               else if(month.equals("MARCH")){
                   dateStr = "3";  
               }
               else if(month.equals("APRIL")){
                   dateStr = "4";  
               }
               else if(month.equals("MAY")){
                   dateStr = "5";  
               }
               else if(month.equals("JUNE")){
                   dateStr = "6";  
               }
               else if(month.equals("JULY")){
                   dateStr = "7";  
               }
               else if(month.equals("AUGUST")){
                   dateStr = "8";  
               }
               else if(month.equals("SEPTEMBER")){
                   dateStr = "9";  
               }
               else if(month.equals("OCTOBER")){
                   dateStr = "10";  
               }
               else if(month.equals("NOVEMBER")){
                   dateStr = "11";  
               }
               else if(month.equals("DECEMBER")){
                   dateStr = "12";
               }
              
               dateStr = dateStr+"-";
               dateStr = dateStr + str[1].trim();
               dateStr = dateStr + "-";
               dateStr = dateStr + str[2].trim().substring(2);

               pw.println(dateStr);
           }
           pw.flush();
           pw.close();
           pw.close();
           scan.close();
           System.out.println("File has been generated");
          
       }
       else{
           System.out.println("File does not exist");
       }
   }

}

Output:

File has been generated

dateoutput.txt

2-19-17
7-4-17
11-4-18
9-30-19

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote