calendar type elements in a list

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]]


    I'm not sure if this is the correct format for you (I will leave that as a task for you) but you could do something like this using LocalDate :

    import java.util.Scanner;
    import java.time.LocalDate;
    import java.time.format.DateTimeFormatter;
    
    class Main {
      public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
    
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
        LocalDate endDate;
    
        while(true){
          System.out.print("Enter the endDate in the format of yyyy/MM/dd:");
          String date = scanner.next();
          try {
            endDate = LocalDate.parse(date, formatter);
            break;
          } catch (Exception e) {
            System.err.println("ERROR: Please input the date in the correct format");
          }
        }
    
        System.out.println("Below are the days between the current day and " + endDate);
        printDaysBetween(endDate);
      }
    
      public static void printDaysBetween(LocalDate end){
        LocalDate start = LocalDate.now();
        while (!start.isAfter(end)) {
          System.out.println(start);
          start = start.plusDays(7);
        }
      }
    }
    

    Example Usage:

    Enter the endDate in the format of yyyy/MM/dd: 2017-10-30
    ERROR: Please input the date in the correct format
    Enter the endDate in the format of yyyy/MM/dd: 2017/10/30
    Below are the days between the current day and 2017-10-30
    2017-09-30
    2017-10-07
    2017-10-14
    2017-10-21
    2017-10-28
    

    The first issue is with the way you print out the result:

    System.out.println(Arrays.toString(dates.toArray()));
    

    If you want formatted dates, you will need to make use of a formatter.

    private static SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd");
    
    static private void printListOfDates(List<Calendar> dates){
        for (Calendar date : dates) {
            System.out.println(outputFormat.format(date));
        }
    }
    

    A second issue is that you seem to reuse the same temp_cal object in your loop in getListOfDates. This results in your list containing multiple copies of the same Calendar object (which is modified multiple times). You will need to create a new instance for every iteration in your loop.


    import java.util.Date;
    import java.time.*;
    import java.text.SimpleDateFormat;
    import java.util.*;
    public class days
    {
    public static void main (String args[]){
        Scanner input = new Scanner(System.in);
        System.out.println("Enter Dates (Start(yyyy/mm/dd)-End)");
        //example 2017 1 5 -> 5 jan 2017
        getDates(Integer.parseInt(input.next()),Integer.parseInt(input.next()),
                     Integer.parseInt(input.next()),Integer.parseInt(input.next()),
                        Integer.parseInt(input.next()),Integer.parseInt(input.next()));
        /*the input needed example
         * 2017
         * 1
         * 1      -Start date
         * 2017
         * 2
         * 10     -End date
         */
    
    }
    
    public static void getDates(int pYearStart, int pMonthStart, int pDayStart,
                                        int pYearEnd, int pMonthEnd, int pDayEnd){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy MMM dd");
        int year = pYearStart;
        int month = pMonthStart-1;
        int day = pDayStart;
        Calendar start = new GregorianCalendar(year,month,day);
        int yearEnd = pYearEnd;
        int monthEnd = pMonthEnd-1;
        int dayEnd = pDayEnd;
        Calendar end = new GregorianCalendar(yearEnd,monthEnd,dayEnd);
        System.out.print("Start Date:   ");
        System.out.println(sdf.format(start.getTime()));
    
        System.out.print("End Date:   ");
        System.out.println(sdf.format(end.getTime()));
    
        System.out.println("");
        System.out.println("All Dates:");
    
        boolean sameDate = false;
        int amount = 0;
        do{
    
            System.out.println(sdf.format(start.getTime()));
            start.add(Calendar.DAY_OF_MONTH, 7);
            amount++;
            if(start.get(Calendar.DAY_OF_MONTH) >= end.get(Calendar.DAY_OF_MONTH) && 
            start.get(Calendar.MONTH) == end.get(Calendar.MONTH)) {
                sameDate = true;}
        }while(sameDate != true);
        System.out.println("No of dates: "+amount);
        }
    }
    

    我认为这就是你想要的,我没有使用列表,只是改变它,只要你喜欢。

    链接地址: http://www.djcxy.com/p/402.html

    上一篇: 如何将HEAD移回到以前的位置? (分离头)

    下一篇: 日历类型元素在列表中