我从2个日期之间的查询中检索更多数据。日期是该月第一周的第一周,以及该月最后一周的最后一天。 准确地说,当前月份(例如)的范围日期为(在区域设置上,星期一是一周的第一天):
from 2014-03-31 to 2014-05-04 (35 days)
问题是数据检索只是工作日(例如:一周5天),但我必须计算整个星期7天,因为每7天我在ArrayList中写入数据。
我正在构建一个代码来实现这一点,但不起作用,因为代码只计算33天,而不是35天,我不明白为什么......
这是我的代码:
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date firstdayoftheweek = new Date();
try
{
firstdayoftheweek = formatter.parse("2014-03-31");
}
catch(ParseException e){}
String dateTemp = null;
for ( String[] result : results) // cicle for every query's row
{
count_row++;
if (count_row == 1)
{
Date dtActualDayQuery = new Date();
try
{
dtWd = formatter.parse(result[12]);
}
catch(ParseException e){}
int diffDays = Days.daysBetween(new DateTime(firstdayoftheweek), new DateTime(dtActualDayQuery)).getDays();
if (diffDays > 1)
{
count_row = (count_row + diffDays) -1;
}
}
else
{
Date dtprevious = new Date();
Date dtActualDayQuery = new Date();
try
{
dtprevious = formatter.parse(dateTemp);
dtActualDayQuery = formatter.parse(result[12]);
}
catch(ParseException e){}
int diffDays = Days.daysBetween(new DateTime(dtprevious), new DateTime(dtActualDayQuery)).getDays();
if (diffDays > 1)
{
count_row = (count_row + diffDays) -1;
}
}
dateTemp = result[12]; // Save the date for next comparison
// Do something every day of the week
if (((count_row) % 7) == 0)
{
// Do something every 7 days
}
}
不正确,我知道......但我无法想出正确的解决方案......
谢谢!
编辑:
我写了一个示例代码:
package test;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.joda.time.DateTime;
import org.joda.time.Days;
public class Test
{
public static void main(String[] args)
{
int count = 0;
String[] month = {"2014-03-31", "2014-04-01", "2014-04-02", "2014-04-03", "2014-04-04",
"2014-04-07", "2014-04-08", "2014-04-09", "2014-04-10", "2014-04-11",
"2014-04-14", "2014-04-15", "2014-04-16", "2014-04-17", "2014-04-18",
"2014-04-21", "2014-04-22", "2014-04-23", "2014-04-24", "2014-04-25",
"2014-04-28", "2014-04-29", "2014-04-30", "2014-05-01", "2014-05-02"};
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date dt_1day_absolute = new Date();
try
{
dt_1day_absolute = formatter.parse("2014-03-31");
}
catch (ParseException e) {}
String dateTemp = null;
for (int i = 0; i < month.length; i++)
{
count++;
if (count == 1)
{
Date dtFromVector = new Date();
try
{
dtFromVector = formatter.parse(month[i]);
} catch (ParseException e){}
int diffDays = Days.daysBetween(new DateTime(dt_1day_absolute), new DateTime(dtFromVector)).getDays();
if (diffDays > 1)
{
count = (count + diffDays) - 1;
}
}
else
{
Date dtprev = new Date();
Date dtaft = new Date();
try
{
dtprev = formatter.parse(dateTemp);
dtaft = formatter.parse(month[i]);
}
catch (ParseException e) {}
int diffDays = Days.daysBetween(new DateTime(dtprev), new DateTime(dtaft)).getDays();
if (diffDays > 1)
{
count = (count + diffDays) - 1;
}
}
dateTemp = month[i];
System.out.println("Num Day: " + String.valueOf(count));
System.out.println("Date: " + month[i]);
if (((count) % 7) == 0)
{
System.out.println("---End week---");
System.out.println("");
}
}
}
}
我得到了这个结果:
Num Day: 1
Date: 2014-03-31
Num Day: 2
Date: 2014-04-01
Num Day: 3
Date: 2014-04-02
Num Day: 4
Date: 2014-04-03
Num Day: 5
Date: 2014-04-04
Num Day: 8
Date: 2014-04-07
Num Day: 9
Date: 2014-04-08
Num Day: 10
Date: 2014-04-09
Num Day: 11
Date: 2014-04-10
Num Day: 12
Date: 2014-04-11
Num Day: 15
Date: 2014-04-14
Num Day: 16
Date: 2014-04-15
Num Day: 17
Date: 2014-04-16
Num Day: 18
Date: 2014-04-17
Num Day: 19
Date: 2014-04-18
Num Day: 22
Date: 2014-04-21
Num Day: 23
Date: 2014-04-22
Num Day: 24
Date: 2014-04-23
Num Day: 25
Date: 2014-04-24
Num Day: 26
Date: 2014-04-25
Num Day: 29
Date: 2014-04-28
Num Day: 30
Date: 2014-04-29
Num Day: 31
Date: 2014-04-30
Num Day: 32
Date: 2014-05-01
Num Day: 33
Date: 2014-05-02
我期待这个结果:
Num Day: 1
Date: 2014-03-31
Num Day: 2
Date: 2014-04-01
Num Day: 3
Date: 2014-04-02
Num Day: 4
Date: 2014-04-03
Num Day: 5
Date: 2014-04-04
Num Day: 6
not exists
Num Day: 7
not exists
---End week---
Num Day: 8
Date: 2014-04-07
Num Day: 9
Date: 2014-04-08
Num Day: 10
Date: 2014-04-09
Num Day: 11
Date: 2014-04-10
Num Day: 12
Date: 2014-04-11
Num Day: 13
not exists
Num Day: 14
not exists
---End week---
Num Day: 15
Date: 2014-04-14
Num Day: 16
Date: 2014-04-15
Num Day: 17
Date: 2014-04-16
Num Day: 18
Date: 2014-04-17
Num Day: 19
Date: 2014-04-18
Num Day: 20
not exists
Num Day: 21
not exists
---End week---
Num Day: 22
Date: 2014-04-21
Num Day: 23
Date: 2014-04-22
Num Day: 24
Date: 2014-04-23
Num Day: 25
Date: 2014-04-24
Num Day: 26
Date: 2014-04-25
Num Day: 27
not exists
Num Day: 28
not exists
---End week---
Num Day: 29
Date: 2014-04-28
Num Day: 30
Date: 2014-04-29
Num Day: 31
Date: 2014-04-30
Num Day: 32
Date: 2014-05-01
Num Day: 33
Date: 2014-05-02
Num Day: 34
not exists
Num Day: 35
not exists
---End week---
答案 0 :(得分:1)
下一个解决方案应该会有所帮助:
final DateTimeFormatter dtf = DateTimeFormat.forPattern("yyyy-MM-dd"); // like 'SimleDateFormat'
final List<String> month = Arrays.asList("2014-03-31", "2014-04-01", "2014-04-02", "2014-04-03", "2014-04-04",
"2014-04-07", "2014-04-08", "2014-04-09", "2014-04-10", "2014-04-11",
"2014-04-14", "2014-04-15", "2014-04-16", "2014-04-17", "2014-04-18",
"2014-04-21", "2014-04-22", "2014-04-23", "2014-04-24", "2014-04-25",
"2014-04-28", "2014-04-29", "2014-04-30", "2014-05-01", "2014-05-02");
final DateTime lastDate = dtf.parseDateTime(month.get(month.size() - 1)); // last date in 'month' array
final MutableDateTime dateIterator = new MutableDateTime(dtf.parseDateTime(month.get(0))); // first date in 'month' array
int count = 0;
for (;;dateIterator.addDays(1)) // start iteration day by day
{
count++;
final String dateAsString = dtf.print(dateIterator); // convert iterator to String
if (month.contains(dateAsString)) // 'month' array contains current date
{
System.out.println("Date: " + dateAsString);
}
else
{
if (dateIterator.getDayOfWeek() == 1 // current date is first day of week but record is absent in db
&& dateIterator.isAfter(lastDate)) // and all records from db are processed
// so let's break for-loop
{
break; // break for-loop
}
System.out.println("not exists");
}
System.out.println("Num Day: " + String.valueOf(count));
}