ํฌ์ŠคํŠธ

๐Ÿšง ์žฅ์•  ๋กœ๊ทธ. ๋งคํ•ด ์ฃผ ์ฐจ(week) ๊ณ„์‚ฐ์˜ ์˜ค๋ฅ˜

์ด์Šˆ ๋ฐœ์ƒ

2018๋…„ 12์›” 30์ผ ์ผ์š”์ผ ์ƒˆ๋ฒฝ์ด์—ˆ์Šต๋‹ˆ๋‹ค.

๋‹น์‹œ ์„œ๋น„์Šค ์ค‘์ด๋˜ ๊ฒŒ์ž„์—๋Š” ๋งค์ฃผ ์‚ฌ์šฉ์ž๋“ค๋ผ๋ฆฌ ๊ฒฝ์Ÿํ•ด์„œ ์›”์š”์ผ์„ ๊ธฐ์ ์œผ๋กœ ๋ณด์ƒ์„ ์ฃผ๊ณ  ์ดˆ๊ธฐํ™”ํ•˜๋Š” ์‹œ์Šคํ…œ์ด ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์›”์š”์ผ๋„ ์•„๋‹ˆ๊ณ  ์ผ์š”์ผ ์ƒˆ๋ฒฝ์— ํ•ด๋‹น ์‹œ์Šคํ…œ์ด ์ดˆ๊ธฐํ™” ๋ผ๋ฒ„๋ฆฌ๋Š” ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค.

๋งˆ์นจ, ์ปค๋ฎค๋‹ˆํ‹ฐ๋ฅผ ๋ชจ๋‹ˆํ„ฐ๋ง ์ค‘์ด๋˜ ์šด์˜ํŒ€์ด ๋ฌธ์ œ๋ฅผ ์‹ค์‹œ๊ฐ„์œผ๋กœ ํ™•์ธํ•ด์„œ ๋žญํ‚น์ด ๋ฆฌ์…‹๋๋‹ค๋Š” ์žฅ์•  ๋ณด๊ณ ๋ฅผ ์ „ํŒŒํ–ˆ๊ณ , ๋‹น์‹œ ์ง‘์ด ๊ฐ€์žฅ ๊ฐ€๊นŒ์› ๋˜ ์ œ๊ฐ€ ๋จผ์ € ํšŒ์‚ฌ๋กœ ๋‹ฌ๋ ค๊ฐ€์„œ ์›์ธ ๋ถ„์„์„ ์‹œ์ž‘ํ–ˆ์Šต๋‹ˆ๋‹ค. ๋ฌธ์ œ๋Š” ์ƒ๊ฐ๋ณด๋‹ค ๊ฐ„๋‹จํ•œ ๋ถ€๋ถ„์—์„œ ๋ฐœ์ƒํ•˜๊ณ  ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.

์‚ฌ์šฉ์ž๋“ค์ด ๊ฒฝ์Ÿํ•œ ๊ธฐ๋ก์„ DB์— ๋‚จ๊ธธ ๋•Œ ์—ฐ๋„์™€ ๊ทธํ•ด์˜ ์ฃผ ์ฐจ๋ฅผ ์กฐํ•ฉํ•œ ๊ฐ’์„ ํ‚ค๊ฐ’์œผ๋กœ ์‚ฌ์šฉํ•ด ๊ธฐ๋กํ•˜๊ณ  ์žˆ์—ˆ๋Š”๋ฐ, 12์›” 30์ผ๋กœ ๋„˜์–ด๊ฐ€๋ฉด์„œ ์ฃผ ์ฐจ๊ฐ€ ๋ฆฌ์…‹๋˜๊ณ  ์ƒˆํ•ด ์ฒซ ์ฃผ๋กœ ๊ณ„์‚ฐ์„ ํ•ด์„œ 201852๊ฐ€ ์•„๋‹Œ 201800์„ ํ‚ค๊ฐ’์œผ๋กœ ๊ณ„์‚ฐํ•˜๊ณ  ์žˆ์—ˆ๋˜ ๊ฒƒ์ž…๋‹ˆ๋‹ค.

๋ฒŒ์จ ์ถœ์‹œํ•œ ์ง€ 4์ฃผ๋…„์ด ์ง€๋‚œ ๊ฒŒ์ž„์ด์—ˆ๋Š”๋ฐ, ๋งค๋…„ ์ฒซ ์ฃผ์— ๊ฐ™์€ ์žฅ์• ๊ฐ€ ๋ฐœ์ƒํ–ˆ๋˜ ๊ฒƒ์ธ๊ฐ€? ํ•˜๊ณ  ๋ดค๋”๋‹ˆ ๋น„์Šทํ•œ ๋ฌธ์ œ๊ฐ€ ๋งค๋…„ ์žˆ์–ด์„œ ์šด์˜ํŒ€์—์„œ ๋ชจ๋‹ˆํ„ฐ๋งํ•˜๊ณ  ์žˆ์—ˆ๋˜ ๊ฒƒ์ด์—ˆ์Šต๋‹ˆ๋‹ค. ์žฅ์•  ์›์ธ์„ ํŒŒ์•…ํ•˜์ง€๋„ ๋ชปํ•˜๊ณ  ์ˆ˜์ •๋„ ๋ชปํ•œ ์ฑ„๋กœ ๊ณ„์† ์„œ๋น„์Šคํ•ด ์™”๋˜ ๊ฒƒ์ด์ฃ .

์›์ธ ํŒŒ์•…

์šฐ์„  ์ดํ•ด๋ฅผ ๋•๊ธฐ ์œ„ํ•œ ์ฝ”๋“œ๋ฅผ ๋ณด์—ฌ๋“œ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค.

1
2
3
4
5
6
7
8
9
10
Calendar calendar = Calendar.getInstance();
calendar.set(2018, 11, 30); // 2018-12-30 ์›”์€ 0๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๋ฏ€๋กœ 12์›”์€ 11
int year = calendar.get(Calendar.YEAR);  // ๋…„
int week = calendar.get(Calendar.DAY_OF_WEEK); // ์š”์ผ [1-7]
int weekOfYear = calendar.get(Calendar.WEEK_OF_YEAR); // ์ฃผ ์ฐจ
if (week == 1) {
  weekOfYear--; // ์›”์š”์ผ์— ๋ฆฌ์…‹์ด๋ฏ€๋กœ ์ผ์š”์ผ์ด๋ฉด weekOfYear ๊ฐ’์„ ํ•˜๋‚˜ ๊ฐ์†Œ ์‹œํ‚ด
}
String yearWeekParam = String.format("%d%02d", year, weekOfYear);
System.out.println(yearWeekParam); // 201800

๊ทธ ํ•ด ๋งˆ์ง€๋ง‰ ์ฃผ๊ฐ€ ์ƒˆ ํ•ด์™€ ๊ฒน์ณ์žˆ์„ ๋•Œ๋Š” ์ƒˆ ํ•ด ์ฒซ ์ฃผ์— ํŽธ์ž…์ด ๋˜๋Š”๋ฐ, ์ด ์ฝ”๋“œ์—์„œ๋Š” ๊ทธ ์—ฐ๋„๊ฐ€ ๋ฐ”๋€Œ์—ˆ์„ ๋•Œ ์ฒ˜๋ฆฌ๊ฐ€ ์—†๊ณ  ์ƒˆํ•ด ์ฒซ ์ฃผ๋กœ ํŒ๋‹จํ•˜์—ฌ 1์ด ๋œ weekOfYear ๊ฐ’์„ ๊ฐ์†Œ์‹œ์ผœ ๋ฒ„๋ ค์„œ ์ฒ˜๋ฆฌ๊ฐ€ ์ž˜๋ชป๋˜๊ณ  ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.

  • 2018๋…„ 12์›” 29์ผ ํ† ์š”์ผ(52์ฃผ ์ฐจ): 201852(์˜๋„) -> 201852(์ผ์น˜)
  • 2018๋…„ 12์›” 30์ผ ์ผ์š”์ผ(52์ฃผ ์ฐจ): 201852(์˜๋„) -> 201800(์˜ค๋ฅ˜)

๋ฌธ์ œ ์ˆ˜์ •

์ฝ”๋“œ๋ฅผ ๊ณ ์น˜๋Š” ๊ฒƒ์€ ๊ฐ„๋‹จํ–ˆ์Šต๋‹ˆ๋‹ค. ์ผ์ฃผ์ผ์˜ ์‹œ์ž‘์„ ์›”์š”์ผ๋กœ ๋ณ€๊ฒฝํ•˜๋ฉด ๋์œผ๋‹ˆ๊นŒ์š”.

1
2
3
4
5
6
7
8
9
Calendar calendar = Calendar.getInstance();
calendar.setFirstDayOfWeek(Calendar.MONDAY); // ์ผ์ฃผ์ผ์˜ ์‹œ์ž‘์„ ์›”์š”์ผ๋กœ ์„ค์ •
calendar.set(2018, 11, 30);
int year = calendar.get(Calendar.YEAR);
// ... ์‚ญ์ œ
int weekOfYear = calendar.get(Calendar.WEEK_OF_YEAR);
// ... ์‚ญ์ œ
String yearWeekParam = String.format("%d%02d", year, weekOfYear);
System.out.println(yearWeekParam); // 201852

์ด๊ฒƒ์œผ๋กœ 2018๋…„ 12์›” 30์ผ์— ๋ฐœ์ƒํ•œ ํ•ด๋‹น ๋ฌธ์ œ๋Š” ํ•ด๊ฒฐ๋์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ๋‹ค๋ฅธ ๋ฌธ์ œ๊ฐ€ ๋” ์žˆ์Šต๋‹ˆ๋‹ค.

  • 2018๋…„ 12์›” 31์ผ ์›”์š”์ผ(1์ฃผ ์ฐจ): 201901(์˜๋„) -> 201801(์˜ค๋ฅ˜)
  • 2019๋…„ 1์›” 1์ผ ์ผ์š”์ผ(1์ฃผ ์ฐจ): 201901(์˜๋„) -> 201901(์ผ์น˜)

2018๋…„ 12์›” 31์ผ์ด ๋˜์—ˆ์„ ๋•Œ ์ƒˆํ•ด๊ฐ€ ๋˜์–ด ์ฃผ ์ฐจ๊ฐ€ 1์ด ๋˜์—ˆ์ง€๋งŒ, ์—ฐ๋„๋Š” ๋ฐ”๋€Œ์ง€ ์•Š์•˜๊ธฐ ๋•Œ๋ฌธ์— ํ‚ค๊ฐ’์ด 201801์ด ๋˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์ด ๋ฌธ์ œ๋Š” ์•„๋ž˜ ์ฝ”๋“œ์™€ ๊ฐ™์ด ์ฃผ ์ฐจ์™€ ์›”์„ ๊ฒ€์‚ฌํ•ด์„œ ์—ฐ๋„๋ฅผ ์ฆ๊ฐ€์‹œ์ผœ ์ฃผ๋Š” ๊ฒƒ์œผ๋กœ ํ•ด๊ฒฐํ–ˆ์Šต๋‹ˆ๋‹ค.

1
2
3
4
5
6
7
8
9
10
11
Calendar calendar = Calendar.getInstance();
calendar.setFirstDayOfWeek(Calendar.MONDAY);
calendar.set(2018, 11, 31);
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int weekOfYear = calendar.get(Calendar.WEEK_OF_YEAR);
if (weekOfYear == 1 && month != 0) { // ์ƒˆํ•ด ์ฒซ ์ฃผ์ธ๋ฐ 1์›”์ด ์•„๋‹Œ ๊ฒฝ์šฐ ์—ฐ๋„ ๊ฐ’์„ ์ฆ๊ฐ€
  year++;
}
String yearWeekParam = String.format("%d%02d", year, weekOfYear);
System.out.println(yearWeekParam); // 201901

๊ทธ๋Ÿฐ๋ฐ, ์—ฌ๊ธฐ์„œ ์˜๋ฌธ์ด ์ƒ๊ฒผ์Šต๋‹ˆ๋‹ค

2018๋…„ 12์›” 31์ผ์ด 201901์ด ๋˜๋Š” ๊ฒƒ์ด ๋งž์„๊นŒ์š”? 2018๋…„์˜ ๋งˆ์ง€๋ง‰ ์ฃผ ์ฐจ๋‹ˆ๊นŒ 201853์ด ๋˜๋Š” ๊ฒƒ์ด ๋งž์„๊นŒ์š”?

์‹ค์ œ ๊ฐœ๋ฐœํŒ€์—์„œ๋„ ์˜๊ฒฌ ์ถฉ๋Œ์ด ์ž ๊น ๋ฐœ์ƒํ–ˆ์—ˆ๋Š”๋ฐ, ์‚ฌ์šฉ ์ค‘์ธ Calendar ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—์„œ ์ƒˆํ•ด ์ฒซ ์ฃผ๋Š” 1์›” 1์ผ์ด ํฌํ•จ๋œ ์ฃผ๋กœ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ณ„์‚ฐ์˜ ํŽธ์˜์„ฑ์„ ๊ณ ๋ คํ•ด์„œ ๊ทธํ•ด ๋งˆ์ง€๋ง‰ ์ฃผ๋Š” ์ƒˆํ•ด ์ฒซ ์ฃผ์— ํŽธ์ž…๋˜๋Š” ๊ฒƒ์œผ๋กœ ๊ฒฐ๋ก ์ด ๋‚ฌ์Šต๋‹ˆ๋‹ค.

๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋งˆ๋‹ค ๊ตฌํ˜„์ด ๋‹ค๋ฅด๊ธด ํ•˜๋”๊ตฐ์š”. ๋‚˜์ค‘์— ์ฐพ์•„๋ณธ moment.js์—์„œ๋Š” 1์›” 1์ผ์ด ์ง€๋‚œํ•ด ๋งˆ์ง€๋ง‰ ์ฃผ ์ฐจ์— ๊ฒน์น˜๋Š” ๊ฒฝ์šฐ ๋งˆ์ง€๋ง‰ ์ฃผ ์ฐจ์— ํŽธ์ž…๋˜๊ณ  ์ฒซ ์ฃผ ์ฐจ๊ฐ€ ๋ฐ€๋ฆฌ๋„๋ก ๊ตฌํ˜„์ด ๋˜์–ด์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.

๊ฒฐ๋ก 

๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์— ์ข…์†๋œ ๋ฐฉ์‹์œผ๋กœ ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ ๊ทธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์— ๋Œ€ํ•œ ์ถฉ๋ถ„ํ•œ ํŒŒ์•…์ด ํ•„์š”ํ•˜๋‹ค๋Š” ์ ๊ณผ ๋‹ค์–‘ํ•œ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๊ฐ€ ํ•„์š”ํ•˜๋‹ค๋Š” ์  ๊ผญ ๊ธฐ์–ตํ•ด์•ผ๊ฒ ์Šต๋‹ˆ๋‹ค.

์• ์ดˆ์— ๊ธฐ์ค€์ด ๋˜๋Š” ์ผ๋กœ๋ถ€ํ„ฐ ์ฃผ ์ฐจ๋ฅผ ์นด์šดํŠธํ•˜๋Š” ๋ฐฉ์‹์ด์—ˆ๋‹ค๋ฉด ์ด๋Ÿฐ ๋ฌธ์ œ๋„ ์—†์—ˆ์„ ๊ฒƒ์ด๊ณ  ๋” ๊ตฌํ˜„ํ•˜๊ธฐ ์‰ฝ๊ณ  ์ข‹์•˜์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ €๋Š” ์‹ค์ œ๋กœ ์ด ๊ฒฝํ—˜ ๋•๋ถ„์— ์ดํ›„ ์ด์™€ ๋น„์Šทํ•œ ์ดˆ๊ธฐํ™” ๋ฐฉ์‹์„ ๊ตฌํ˜„ํ•  ๋•Œ๋Š” ์„œ๋น„์Šค ์‹œ์ž‘์ผ์„ ๊ธฐ์ค€์œผ๋กœ ํ‚ค๊ฐ’์„ ์นด์šดํŠธํ•˜๋Š” ๊ฒƒ์œผ๋กœ ๊ตฌํ˜„ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

๋ฒˆ์™ธ

๊ธ€์„ ์ž‘์„ฑํ•˜๋ฉด์„œ ์ข€ ๋” ์กฐ์‚ฌํ•ด ๋ณด๋‹ˆ ISO์—์„œ ํ‘œ์ค€์œผ๋กœ ์ง€์ •ํ•œ ์ฃผ ์ฐจ ๊ณ„์‚ฐ ๋ฐฉ๋ฒ•์ด ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. ์ž์„ธํ•œ ๊ฒƒ์€ ๋งํฌ๋ฅผ ์ฐธ๊ณ  ๋ฐ”๋ž๋‹ˆ๋‹ค. https://en.wikipedia.org/wiki/ISO_week_date#First_week

์ด ๊ธ€์€ ์ €์ž‘๊ถŒ์ž์˜ CC BY 4.0 ๋ผ์ด์„ผ์Šค๋ฅผ ๋”ฐ๋ฆ…๋‹ˆ๋‹ค.