[英]Change the first week of a year
Using Java's Calendar or something else, let's say I set the date to 2012-10-26. 使用Java的日历或其他东西,假设我将日期设置为2012-10-26。 I want this date to be the first day of a year.
我希望这个日期成为一年中的第一天。 Is it possible to get the week number of a specific date -let's say it's 2012-12-10- assuming that the first week of the year starts from 2012-10-26?
是否有可能得到特定日期的周数 - 假设它是2012-12-10-假设一年的第一周从2012-10-26开始?
I know my question sounds a little weird, but I could use any useful input. 我知道我的问题听起来有点奇怪,但我可以使用任何有用的输入。
Thanks. 谢谢。
--Details - 细节
I have a database table called WeeklySchedule . 我有一个名为WeeklySchedule的数据库表。 In this table I have columns starting from Week1 to Week52 , and every column have some data about the days of that week.
在此表中,我有从第1 周到第52 周的列,每列都有一些关于该周日期的数据。 Unfortunately, Week1 is not really the first week of the year.
不幸的是, 第一周并不是真正的第一周。 I need to do my calculations assuming that Week1 starts from 2012-08-26 .
假设Week1从2012-08-26开始,我需要进行计算。
Here's one approach using the Calendar API: 以下是使用Calendar API的一种方法:
private static final Calendar START_CAL = createAugust26th2012Cal();
public int getWeekNumber(Calendar someCal) {
int theWeek = someCal.get(Calendar.WEEK_OF_YEAR);
int weekCount = 0;
while (theWeek != START_CAL.get(Calendar.WEEK_OF_YEAR)) {
someCal.add(Calendar.WEEK_OF_YEAR, -1);
theWeek = someCal.get(Calendar.WEEK_OF_YEAR);
weekCount++;
}
return weekCount;
}
Or you could get the diff (in millis) between two calendars and divide the result by the number of millis in a week: 或者你可以得到两个日历之间的差异(毫秒)并将结果除以一周内的毫秒数:
If you use this approach, don't forget to take TimeZones and Daylight Saving Time into account. 如果您使用此方法,请不要忘记考虑TimeZones和夏令时。
I can't remember exactly what the code looks like to compensate for DST, but I think it's something like this: (I can't quite remember if you add the offset to or subtract the offset from the two operands) 我不记得究竟代码是什么来补偿DST,但我认为它是这样的:( 我不记得你是否添加偏移量或从两个操作数中减去偏移量)
private static final long ONE_WEEK_MILLIS = 1000 * 60 * 60 * 24 * 7;
public int getWeeksBetween(Calendar calOne, Calendar calTwo) {
long millisOne = calOne.getTime().getTime();
long millisTwo = calTwo.getTime().getTime();
long offsetOne = calOne.getTimeZone().getOffset(millisOne);
long offsetTwo = calTwo.getTimeZone().getOffset(millisTwo);
long diff = (millisTwo - offsetTwo) - (millisOne + offsetOne );
return diff / ONE_WEEK_MILLIS;
}
I haven't ever seen a built-in function for this, only calls like Calendar.get(WEEK_OF_YEAR)
or Joda 's LocalDate.getWeekOfWeekyear()
. 我没有见过这样的内置函数,只有像
Calendar.get(WEEK_OF_YEAR)
或Joda的LocalDate.getWeekOfWeekyear()
这样的调用。 java.util.Calendar
(and Date
) is often regarded as a bad library, compared to a library like Joda Time. 与像Joda Time这样的库相比,
java.util.Calendar
(和Date
)通常被认为是一个糟糕的库。 This SO post has a good list of pros and cons. 这篇SO帖子有很多优点和缺点。
Note that if your database table applies to year after year repeats across a year, you may run into trouble. 请注意,如果您的数据库表适用于一年中一年又一年的重复,您可能会遇到麻烦。 Some years ( 71 years per 400, or about 1 every 5.6 ) have 53 weeks.
有些年份( 每400人71岁,或每5.6人约1人 )有53周。
Given the above, you may want to settle for an approximation, such as counting the number of days between the two dates divided by 7 (all mod 52), which will slip by a small amount each year, or calculating (weekOfYear(date) - weekOfYear(startDate)) % 52
, which may repeat a week around the beginning of the calendar year. 鉴于上述情况,您可能希望得出一个近似值,例如计算两个日期之间的天数除以7(所有模型52),每年将减少一小部分,或计算
(weekOfYear(date) - weekOfYear(startDate)) % 52
,可能在日历年的开始周围重复一周。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.