仅动态列出特定日期的两个日期之间的日期

时间:2016-05-22 05:19:24

标签: java date datetime time

我有一个presentation表,其中包含以下值:

ID     DAY         START       END      STARTDATE       ENDDATE
622   Monday     12:00:00    02:00:00   01-05-2016     04-06-2016 
623   Tuesday    12:00:00    02:00:00   01-05-2016     04-06-2016 
624   Wednesday  08:00:00    10:00:00   01-05-2016     04-06-2016 
625   Thursday   10:00:00    12:00:00   01-05-2016     04-06-2016 

所以基本上,STARTDATE是2016年1月5日,直到ENDDATE 04-06-2016

我想如何在这两个日期之间列出日期:

Calendar cal = Calendar.getInstance();
                cal.setTime(presentationDateStart);
                while (cal.getTime().before(presentationDateEnd)) {
                    cal.add(Calendar.DATE,1);
                    System.out.println("poop "+cal.getTime());

                }

但是,这将以此格式列出所有可能的日期:Sun May 08 00:00:00 SGT 2016

如何动态地根据DAY表格中的presentation获取特定日期? presentation表的值始终是动态的。

更新:

我只想要在presentation表格中仅包含DAY的日期列表。格式对我来说并不重要。

预期结果:

Mon May 02 00:00:00 SGT 2016
Tue May 03 00:00:00 SGT 2016
Wed May 04 00:00:00 SGT 2016
Thu May 05 00:00:00 SGT 2016
Mon May 09 00:00:00 SGT 2016
Tue May 10 00:00:00 SGT 2016
Wed May 11 00:00:00 SGT 2016
Thu May 12 00:00:00 SGT 2016 so on until end of the date

更新

这是我的JAVA代码:

            String presentationID = null;
            String presentationDay = null;
            Date presentationStart = null;
            Date presentationEnd = null;
            Date presentationDateStart = null;
            Date presentationDateEnd = null;
            String presentationFreeID = null;

            List list1 = new ArrayList() ;
            list1 = GenerateScheduleDAO.getPresentation();
            PresentationBean[] presentation = new PresentationBean[list1.size()];
            for(int c = 0 ; c < list1.size() ; c++){

                presentation[c] = (PresentationBean) list1.get(c);

                presentationID = presentation[c].getPresentationID();
                presentationDay = presentation[c].getPresentationDay();
                presentationStart = presentation[c].getPresentationStart();
                presentationEnd = presentation[c].getPresentationEnd();
                presentationDateStart = presentation[c].getPresentationDateStart();
                presentationDateEnd = presentation[c].getPresentationDateEnd();
                presentationFreeID = presentation[c].getFreeID();

                System.out.println(" pFID11: "+ presentationFreeID +" pID: "+ presentationID +" pDay: "+ presentationDay+ " PStart: "+ presentationStart+" pEnd: "+ presentationEnd+" DStart: "+presentationDateStart+" EDate: "+ presentationDateEnd);

                Calendar cal = Calendar.getInstance();
                cal.setTime(presentationDateStart);
                while (cal.getTime().before(presentationDateEnd)) {
                    cal.add(Calendar.DATE,1);
                    System.out.println("poop "+cal.getTime());

                }

            }    

在db中,DAY存储在VARCHAR2

1 个答案:

答案 0 :(得分:2)

您可以使用Calendar.DAY_OF_WEEK获取日期索引,并仅打印(包括)您需要的日期

Calendar cal = Calendar.getInstance();
cal.setTime(presentationDateStart);
while (cal.getTime().before(presentationDateEnd)) {
    cal.add(Calendar.DATE,1);
    if(cal.get(Calendar.DAY_OF_WEEK) == 2 || cal.get(Calendar.DAY_OF_WEEK) == 3 || cal.get(Calendar.DAY_OF_WEEK) == 4 || cal.get(Calendar.DAY_OF_WEEK) == 5){
        //2, 3 ,4 ,5 are the index of days mon-Thu
        //constants available too : Calendar.MONDAY ... 
        System.out.println("poop "+cal.getTime());
    }
}//date is before endDate

如果你有day-index的动态源,你可以在这个列表中得到一个比较DAY_OF_WEEK的值(数组或arraylist)?

了解有关DAY_OF_WEEK check docs

的更多信息

修改 我已经提到过你可以使用动态日索引数组 这是我的意思的样本:

ArrayList<Integer> dayIndex = getDayIndex();// could be from DB, API... it contains day index (1,4,6) the days you want to include
if(dayIndex .contains(cal.get(Calendar.DAY_OF_WEEK)){
    System.out.println("poop "+cal.getTime());
}