简体   繁体   English

Android-确定星期的开始和结束日期

[英]Android - determine start and end date of week

I have written simple code for getting start and end date of a week. 我已经编写了用于获取一周开始日期和结束日期的简单代码。 It goes like 它像

Calendar calendar = new GregorianCalendar();
calendar.set(Calendar.WEEK_OF_YEAR, week);
calendar.set(Calendar.YEAR, year);

calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
start = new DateTime(calendar.getTime());

calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
end = new DateTime(calendar.getTime());

start and end are DateTime from joda time . startend是从joda time startDateTime week and year are parameters. weekyear是参数。

The problem is that when I debug this piece of code, it's working. 问题是,当我调试这段代码时,它正在工作。 When not debugging, the start and end is set for current week. 如果不进行调试,则将startend设置为当前星期。 To be clear, I have to debug line by line not to only run app in debug mode. 需要明确的是,我必须逐行调试,而不是仅在调试模式下运行应用程序。 Sometimes the year is set but start and end dime are the same. 有时会设定年份,但开始和结束时间相同。 I have no idea what could cause that. 我不知道是什么原因造成的。

Actually, I could figure out what was the problem, but if someone is looking for a really easy way to get start and end date of any week in any year you can use JodaTime . 实际上,我可以找出问题所在,但如果有人正在寻找一种非常简单的方法来获取任何一年中任何一周的开始和结束日期,则可以使用JodaTime

DateTime start = new DateTime().withWeekOfWeekyear(week).withYear(year).withDayOfWeek(1); //1 - monday

DateTime end = new DateTime().withWeekOfWeekyear(week).withYear(year).withDayOfWeek(7); //7 - sunday

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM