Task The task is to read a file, which consists of one date on each line. Each d
ID: 3886609 • Letter: T
Question
Task The task is to read a file, which consists of one date on each line. Each date should be stored in a Date objectIf the next date is later than the previous date, a DateRange object should be created from the two dates. All the Date and DateRange objects should be printed out. Input and output Consider the following input June 17 , 1997 July 23 , 1997 September 28, 1980 September 31, 1980 Mar. 2, 1980 Apr. 2, 1980 May 3, 1980 Nov 25, 1989 Dec 25, 1989 Jan 3, 1973 Note that the months might be written in different ways. Also, note that there is cre invalid date. This data should result in the following output Date : June 17 , 1997 Date : July 23 , 1997 DateRange : Date : June 17 , 1997 - Date: July 23, 1997 Date : September 28, 1980 Invalid Date Date : March 2 , 1980 Date : April 2, 1980 DateRange: Date : March 2 , 1980 - Date : April 2 , 1980 Date: May 3, 1980 DateRange : Date : April 2, 1980 - Date: May 3 , 1980 Date: November 25, 1989 DateRange : Date : May 3 , 1980 - Date: November 25 , 1989 Date: December 25, 1989 DateRange : Date : November 25, 1989 - Date: December 25, 1989 Date: January 3, 1973 Each of these lines should be the result of print statements like System.out.printin(date); system.out.println( daterange );Explanation / Answer
//to compare the date objects
public int compareTo(Date other) {
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy");
String value = this.getDateTmTrans();
String value1 =other.getDateTmTrans();
int retVal = 0;
try {
Date firstDate = dateFormat.parse(value);
Date secondDate = dateFormat.parse(value1);
retVal = firstDate.compareTo(secondDate);
} catch (ParseException e) {
e.printStackTrace();
}
return 0;
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.