DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Date d1;
Date d2;
Date d3;
Date d4;
try {
d1 = df.parse("2007-01-01");
d2 = df.parse("2007-04-01");
d3 = df.parse("2001-07-02");
d4 = df.parse("2001-09-02");
} catch(java.text.ParseException pe) {
JOptionPane.showMessageDialog(null, "0");
return;
}
String relation;
if (d1.before(d2))
relation = "Date is in the first Quarter";
else if (d1.after(d2))
relation = "Date is in the second Quarter";
else if (d2.before(d3))
relation = "Date is in the second Quarter";
else if (d2.after(d3))
relation = "Date is in the third Quarter";
else if (d3.before(d4))
relation = "Date is in the third Quarter";
else if (d3.after(d4))
relation = "Date is in the third Quarter";
else
relation = "Date is in the fourth quarter";
TxtOutput.setText(d1 + " is " + relation + ' ' + d2 );
}
The above code detects the date for each variable and tells the user which quarter it is in displaying it in a text field.
Is it possible to have some sort of TO option for example
Code:
d1 = df.parse("2007-01-01 *TO 2007-03-31*");
The idea is that i the end I will be able to compare todays date (the day in which the program is compiled i.e. not a a fixed day) with each quarter not just comparing quarters with each other, I also need help in this area too. Also is it possible to just select months instead of years bcause at present this program would only be compatible with this year instead of the "yyyy-MM-dd" format I could have a "MM-dd" format.
12-19-2007, 03:56 AM
Razee Marikar
You cannot do such a parsing. The DateFormat object's parse method returns a Date. And a Date object can contain only one date, no range. All its fields except year will become 0 unless set otherwise. Year will be epoch (1970). This means you cannot compare only months and dates (though you can enter only month and date, year will be epoch till you set it). To get the current date, you can use new Date().
12-19-2007, 11:19 AM
sjalle
Save yourself a lot of work by using the Calendar class. All the things you ask for here are already implemented in that class.