MongoDB的$week operator狀態
Takes a date and returns the week of the year as a number between 0 and 53.
Weeks begin on Sundays, and week 1 begins with the first Sunday of the year. Days
preceding the first Sunday of the year are in week 0. This behavior is the same as the
「%U」 operator to the strftime standard library function.
然而,Java日曆的DAY_OF_WEEK返回略有不同(美國地區)。例如,2013年,mongo的第1周實際上是第2周。
我的問題是,添加任意1並不能解決問題。是否有一個公式可用於確定使用哪週數來獲取星期開始日期。
場景:我在mongo中運行一個聚合,它返回一個星期的數字。根據週數,我需要到達周開始日期。
總是會像下面的工作?假定日曆是日曆的另一個實例。
Calendar firstDay = Calendar.getInstance(Locale.US);
firstDay.set(Calendar.DAY_OF_YEAR, 1);
int day = firstDay.get(Calendar.DAY_OF_WEEK);
if (day != Calendar.SUNDAY){
//both mongo and java calendar start in different weeks so add 1 to reconcile
calendar.set(Calendar.WEEK_OF_YEAR, number.intValue()+1);
}
你說「根據週數,我需要到達周開始日期。」 - 從星期天開始?星期一?或特定於語言環境? – Trisha
@Trisha在這種情況下,我正在將星期日視爲每週的第一天。但通用的解決方案將受到歡迎。我試圖解決的問題特別針對美國區域設置。 – Nasir
我想我在這裏得到了一個很好的答案http://stackoverflow.com/a/30247836/2366413 –