Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

***Seeking help for JAVA date difference *** I am having troubles calculating the time difference - instructions and my code is below - thanks. Create

***Seeking help for JAVA date difference ***

I am having troubles calculating the time difference - instructions and my code is below - thanks.

Create a program that reads the files: date_diff.csv. The program should read all of the records and calculate the number of days between the two dates and print out the number of days, and then translate it into years, months and days. When the file is finished, list the number of dates that start in each month with the number.

*******

date_diff.csv file found here - https://github.com/tempUser00/temp

******

Input

8-May-93,Sep. 24, 2013

Feb. 8, 1989,Feb. 1, 1991

Feb. 27, 2006,Nov. 3, 2015

Output

Days . Years, Months, Days

7444 20, 4, 16

723 1, 11, 24

3536 9, 8, 7

===============

Apr 17

Aug 18

Dec 19

Main

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

package unit2collections; import java.text.DateFormat; import java.util.Date; import java.util.GregorianCalendar; import java.util.HashMap; import java.util.Map; public class Main { // List of IO methods (input/output) private final static fileInput dateDiff = new fileInput ("date_diff.csv"); private final static Map map = new HashMap(); private final static fileOuput output = new fileOuput("output.txt");/// also create the object first public static void main(String[] args){ String line; String[] words; Object wordFound; // Today's date Date today = new Date(); long diff = today.getTime() - dateDiff(); System.out.println((diff / (1000 * 60 * 60 * 24)) + " days old."); DateFormat formatter = DateFormat.getDateTimeInstance(DateFormat.LONG); String(long timeDifferenceMilliseconds) { long diffSeconds = timeDifferenceMilliseconds / 1000; long diffMinutes = timeDifferenceMilliseconds / (60 * 1000); long diffHours = timeDifferenceMilliseconds / (60 * 60 * 1000); long diffDays = timeDifferenceMilliseconds / (60 * 60 * 1000 * 24); long diffWeeks = timeDifferenceMilliseconds / (60 * 60 * 1000 * 24 * 7); long diffMonths = (long) (timeDifferenceMilliseconds / (60 * 60 * 1000 * 24 * 30.41666666)); long diffYears = timeDifferenceMilliseconds / ((long)60 * 60 * 1000 * 24 * 365); if (diffSeconds < 1) { return "less than a second"; } else if (diffMinutes < 1) { return diffSeconds + " seconds"; } else if (diffHours < 1) { return diffMinutes + " minutes"; } else if (diffDays < 1) { return diffHours + " hours"; } else if (diffWeeks < 1) { return diffDays + " days"; } else if (diffMonths < 1) { return diffWeeks + " weeks"; } else if (diffYears < 1) { return diffMonths + " months"; } else { return diffYears + " years"; } } System.out.println(formatter.format(dateDiff)); System.out.println("Days Years Months Days"); System.out.println("======== ======= ======= ======== "); for (Map.Entry entry : dateDiff.entrySet()) { System.out.println(entry.getKey() + "" + entry.getValue()); output.fileWrite(entry.getKey() + "" + entry.getValue()); } output.fileClose(); } } 

fileInput

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

package unit2collections; import java.text.DateFormat; import java.util.Date; import java.util.GregorianCalendar; import java.util.HashMap; import java.util.Map; public class Main { // List of IO methods (input/output) private final static fileInput dateDiff = new fileInput ("date_diff.csv"); private final static Map map = new HashMap(); private final static fileOuput output = new fileOuput("output.txt");/// also create the object first public static void main(String[] args){ String line; String[] words; Object wordFound; /** Today's date */  Date today = new Date(); long diff = today.getTime() - dateDiff(); System.out.println((diff / (1000 * 60 * 60 * 24)) + " days old."); DateFormat formatter = DateFormat.getDateTimeInstance(DateFormat.LONG); System.out.println(formatter.format(dateDiff)); // date_diff.csv file here (read data) while ((line = dateDiff.fileReadLine()) != null) { words = line.split(","); wordFound = map.get(words[0]); if (wordFound == null) { map.put(words[0], new locationStuff(1)); } else { locationStuff locationStuff = ((locationStuff) map.get(words[0])); locationStuff.incrementStuff(); map.put(words[0], locationStuff); } } System.out.println("Days Years Months Days"); System.out.println("======== ======= ======= ======== "); for (Map.Entry entry : dateDiff.entrySet()) { System.out.println(entry.getKey() + "" + entry.getValue()); output.fileWrite(entry.getKey() + "" + entry.getValue()); } output.fileClose(); } } 

fileOutput

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

package unit2collections; import java.io.*; //This class is used to write output to a file public class fileOuput { Writer out = null; private String fileName; public fileOuput(String filename) { this.fileName = filename; try { //FileOutputStream class is used to direct output to a file out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName))); } catch (FileNotFoundException e) { System.out.println("Error, cannot open file: " + fileName + " " + e); } } public void fileWrite(String line) { //Writes output to a file line by line try { out.write(line + " "); } catch (Exception e) { System.out.println("Error, cannot open file: " + fileName + " " + e); } } public void fileClose() { //To safely close the file if (out != null) { try { out.close(); } catch (IOException e) { e.printStackTrace(); } } } } 

locationStuff

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

package unit2collections; public class locationStuff { public int cities; public int stuff; public locationStuff(int city) { this.cities = city; } public int getCity() { return cities; } public void incrementCity() { cities++; } public int getStuff() { return stuff; } public void incrementStuff() { stuff++; } @Override public String toString(){ return String.valueOf("" + getCity() + " " + getStuff()); } } 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

JDBC Database Programming With J2ee

Authors: Art Taylor

1st Edition

0130453234, 978-0130453235

More Books

Students also viewed these Databases questions