他全部,
我正在尝试计算周,月和年的第一天和最后一天。 对于月份和年份,它工作正常,但周错了:
public Date[] calcDateRange(Calendar c, int day) {
Date[] dr = new Date[2];
// setMin
c.set(day, c.getActualMinimum(day));
dr[0] = c.getTime();
// setMax
c.set(day, c.getActualMaximum(day));
dr[1] = c.getTime();
return dr;
}
public void print() {
SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
Calendar cStart = Calendar.getInstance(Locale.GERMANY);
cStart.setFirstDayOfWeek(Calendar.MONDAY);
System.out.println("startdate: " + sdf.format(cStart.getTime()));
Date[] minMaxD = calcDateRange(cStart, Calendar.DAY_OF_WEEK);
System.out.println("start_of_week:\t" + sdf.format(minMaxD[0]) + "\nend_of_week:\t" + sdf.format(minMaxD[1]));
minMaxD = calcDateRange(cStart, Calendar.DAY_OF_MONTH);
System.out.println("start_of_month:\t" + sdf.format(minMaxD[0]) + "\nend_of_month:\t" + sdf.format(minMaxD[1]));
minMaxD = calcDateRange(cStart, Calendar.DAY_OF_YEAR);
System.out.println("start_of_year:\t" + sdf.format(minMaxD[0]) + "\nend_of_year:\t" + sdf.format(minMaxD[1]));
}
有人可以帮助我找到我的错误吗? 计算日期的最佳方法是什么?
输出:
startdate: 20.03.2014
start_of_week: 23.03.2014 <--- wrong, should be '17.03.2014'
end_of_week: 22.03.2014 <--- wrong, should be '23.03.2014'
start_of_month: 01.03.2014
end_of_month: 31.03.2014
start_of_year: 01.01.2014
end_of_year: 31.12.2014
提前谢谢。
//修改
目前我使用以下方法,但我真的不满意它:
public Date[] calcDateRange(Calendar c, int day) {
int fdow = c.getFirstDayOfWeek();
c.setFirstDayOfWeek(Calendar.SUNDAY);
Date[] dr = new Date[2];
// setMin
c.set(day, c.getActualMinimum(day));
//..setSecondsMinutes
if (day == Calendar.DAY_OF_WEEK)
c.add(day, 1); // German Week correction
dr[0] = c.getTime();
// setMax
c.set(day, c.getActualMaximum(day));
//..setSecondsMinutes
if (day == Calendar.DAY_OF_WEEK)
c.add(day, 1); // German Week correction
dr[1] = c.getTime();
c.setFirstDayOfWeek(fdow);
return dr;
}
答案 0 :(得分:4)
Joda Time库对日期和时间周围的事物非常有用。由于我无法发表评论,我将其作为答案发布。我建议你研究一下。 http://www.joda.org/joda-time/
<强> //编辑:强>
我认为我发现错误在哪里。 一周的开始是不正确的,因为:
你打电话
Date[] minMaxD = calcDateRange(cStart, Calendar.DAY_OF_WEEK);
然后在calcDateRange函数
的行上c.set(day, c.getActualMinimum(day));
将发生以下情况:
在
上dr[0] = c.getTime();
行你将获得SUNDAY的时间,因为你设置了它,然后日期返回23.3。
本周末校长是相同的:
在线
c.set(day,c.getActualMaximum(day));
你设置&#34; DAY_OF_WEEK&#34;到星期六然后它将返回你的日期22.3。
<强> // EDIT2:强> 计算开始和结束的方法
public Date[] calcDateRangeWeek(Calendar c, int day) {
Date[] dr = new Date[2];
// setMin
c.set(day, Calendar.MONDAY);
dr[0] = c.getTime();
// setMax
c.set(day, Calendar.SUNDAY);
dr[1] = c.getTime();
return dr;
}
答案 1 :(得分:1)
如果您可以使用Java 8,我会使用java.time
中的TemporalAdjusters:
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import static java.time.DayOfWeek.*;
import static java.time.temporal.TemporalAdjusters.*;
public void print() {
DateTimeFormatter german = DateTimeFormatter.ofPattern("dd.MM.yyyy");
LocalDate start = LocalDate.now();
System.out.printf("startdate: %s%n", start.format(german));
System.out.printf("start_of_week:\t%s%nend_of_week:\t%s%n",
start.with(previousOrSame(MONDAY)).format(german),
start.with(nextOrSame(SUNDAY)).format(german));
System.out.printf("start_of_month:\t%s%nend_of_month:\t%s%n",
start.with(firstDayOfMonth()).format(german),
start.with(lastDayOfMonth()).format(german));
System.out.printf("start_of_year:\t%s%nend_of_year:\t%s%n",
start.with(firstDayOfYear()).format(german),
start.with(lastDayOfYear()).format(german));
}
如果您不能使用Java 8,这些功能都来自Joda Time库。
答案 2 :(得分:0)
您可以像这样使用Java日历
Calendar cal = Calendar.getInstance();
Date now = cal.getTime();
cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
Date mondayOfTheWeek = cal.getTime();
....
日历会自动重新计算相关日期
编辑:该月的最后一天
cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) + 1);
cal.set(Calendar.DAY_OF_MONTH, 1);
cal.add(Calendar.DAY_OF_MONTH, -1);
您可以使用Calendar.YEAR和Calendar.DAY_OF_YEAR
执行相同操作