I have to create a program which prints out a list of dates (year, month, days) until a users selected date (end_date
which is later converted to end_cal
).
For example, if today is 2017-09-30 Saturday
and user inputs date 2017-10-30
, program must print out these dates:
2017-09-30, 2017-10-07, 2017-10-14, 2017-10-21, 2017-10-28.
Problems:
- Adding calendar type elements into a list
- Printing the list.
- Formatting dates during printing of addition to the list
When I try to print it, output is just a bunch of duplicates of same date.
public class Weekdays {
static Scanner input = new Scanner(System.in);
static Calendar temp_cal = Calendar.getInstance(); //temporary calendar object. it's value is being chaged in the process
static Calendar start_cal = Calendar.getInstance(); // current day when the program is executed
static Calendar end_cal = Calendar.getInstance(); //end date that the user inputs
static SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
public static boolean date_validation(String date){ //partial validation: whether the input date is in a correct format
Date test_date;
try {
test_date = format.parse(date);
}catch (ParseException e){
return false;
}
return true;
}
//created list of dates that are of the same day of the week (for example all Sundays)
static private List<Calendar> getListOfDates(){
List<Calendar> dates = new ArrayList<>();
while (!((temp_cal.get(Calendar.YEAR)>= end_cal.get(Calendar.YEAR))&&(temp_cal.get(Calendar.MONTH) >= end_cal.get(Calendar.MONTH))&&(temp_cal.get(Calendar.DAY_OF_YEAR) >= end_cal.get(Calendar.DAY_OF_YEAR))))
{
temp_cal.add(Calendar.DATE, 7);
dates.add(temp_cal); }
return dates;
}
static private void printListOfDates(List<Calendar> dates){
System.out.println(Arrays.toString(dates.toArray()));
}
public static void main(String[] str) throws ParseException{
String end_date = input.next();
while(!(date_validation(end_date))){
end_date = input.next();
}
end_cal.setTime(format.parse(end_date));
printListOfDates(getListOfDates());
}
Input: 2018/01/01
Output (copied only one example, whole output is just several duplicates of this):
java.util.GregorianCalendar[time=1515233525518,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Europe/Helsinki",offset=7200000,dstSavings=3600000,useDaylight=true,transitions=118,lastRule=java.util.SimpleTimeZone[id=Europe/Helsinki,offset=7200000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=2,startMonth=2,startDay=-1,startDayOfWeek=1,startTime=3600000,startTimeMode=2,endMode=2,endMonth=9,endDay=-1,endDayOfWeek=1,endTime=3600000,endTimeMode=2]],firstDayOfWeek=2,minimalDaysInFirstWeek=4,ERA=1,YEAR=2018,MONTH=0,WEEK_OF_YEAR=1,WEEK_OF_MONTH=1,DAY_OF_MONTH=6,DAY_OF_YEAR=6,DAY_OF_WEEK=7,DAY_OF_WEEK_IN_MONTH=1,AM_PM=1,HOUR=0,HOUR_OF_DAY=12,MINUTE=12,SECOND=5,MILLISECOND=518,ZONE_OFFSET=7200000,DST_OFFSET=0]]
java.util.Calendar
class. You should instead find the appropriate class for your use case in thejava.time
package. – Joe CCalendar
has the methodsbefore
andafter
to make comparisons – user7605325java.time
package implementComparable
. – Joe CisBefore
andisAfter
too (more readable IMHO). – Ole V.V.