Advanced Java Create a program that reads the files: date_diff.csv. The program
ID: 3722683 • Letter: A
Question
Advanced Java
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.
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
Explanation / Answer
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;
public class DateDiff {
public static void main(String[] args) throws ParseException {
// First We need to parse each line of the file and then we need to make two
// date objects for calculation
// As per given data there are 4 tokens in each line separted by comma
SimpleDateFormat format = new SimpleDateFormat("MMM/dd/yyyy");
ArrayList<String> lines = readFile();
// loop through lines
Date d2 = null;
Date d1 = null;
System.out.println("Days "+" "+"Years"+" "+"Months"+" "+" Days");
for (String string : lines) {
// get tokens
String[] tokens = string.split(",");
// first and second token is of date 1
// first token needs to be splitted by space
String[] dayMonth = tokens[0].split("\.");
d1 = format.parse(dayMonth[0] + "/" + dayMonth[1] + "/" + tokens[1]);
// for date 2
// first token needs to be splitted by space
dayMonth = tokens[2].split("\.");
d2 = format.parse(dayMonth[0] + "/" + dayMonth[1] + "/" + tokens[3]);
System.out.println(getDateDifference(d1, d2));
}
}
// Read the file
private static ArrayList<String> readFile() {
List<String> list = null;
try {
list = Files.readAllLines(new File("./date_diff.csv").toPath(), Charset.defaultCharset());
} catch (IOException e) {
e.printStackTrace();
}
return (ArrayList<String>) list;
}
public static String getDateDifference(Date from, Date to) {
long diffInMillies = Math.abs(to.getTime() - from.getTime());
long days = TimeUnit.DAYS.convert(diffInMillies, TimeUnit.MILLISECONDS);
Calendar fromDate=Calendar.getInstance();
Calendar toDate=Calendar.getInstance();
fromDate.setTime(from);
toDate.setTime(to);
int increment = 0;
int year,month,day;
if (fromDate.get(Calendar.DAY_OF_MONTH) > toDate.get(Calendar.DAY_OF_MONTH)) {
increment =fromDate.getActualMaximum(Calendar.DAY_OF_MONTH);
}
// DAY CALCULATION
if (increment != 0) {
day = (toDate.get(Calendar.DAY_OF_MONTH) + increment) - fromDate.get(Calendar.DAY_OF_MONTH);
increment = 1;
} else {
day = toDate.get(Calendar.DAY_OF_MONTH) - fromDate.get(Calendar.DAY_OF_MONTH);
}
if ((fromDate.get(Calendar.MONTH) + increment) > toDate.get(Calendar.MONTH)) {
month = (toDate.get(Calendar.MONTH) + 12) - (fromDate.get(Calendar.MONTH) + increment);
increment = 1;
} else {
month = (toDate.get(Calendar.MONTH)) - (fromDate.get(Calendar.MONTH) + increment);
increment = 0;
}
year = toDate.get(Calendar.YEAR) - (fromDate.get(Calendar.YEAR) + increment);
return days+" "+year+" "+month+" "+day;
}
}
output/;
Days Years Months Days
7444 20 4 16
723 1 11 21
3536 9 8 4
input file
May.8,1993,Sep. 24, 2013
Feb.8, 1989,Feb. 1, 1991
Feb.27, 2006,Nov. 3, 2015
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.