Skip to main content Link Menu Expand (external link) Document Search Copy Copied

Calendar class ์ •๋ฆฌ

Table of contents

  1. Calendar ํด๋ž˜์Šค
    1. 1. Calendar๋ฅผ Date๋กœ ๋ณ€ํ™˜
    2. 2. Date๋ฅผ Calendar๋กœ ๋ณ€ํ™˜
  2. Calendar ํด๋ž˜์Šค์˜ ์‚ฌ์šฉ

Calendar ํด๋ž˜์Šค


  • Calendar ํด๋ž˜์Šค๋Š” ์ถ”์ƒ ํด๋ž˜์Šค์ด๋ฏ€๋กœ ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์—†๊ณ , getInstance()๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ด์•ผ ํ•œ๋‹ค.
Calendar cal = Calendar.getInstance();
  • ์›๋ž˜ ๋‚ ์งœ์™€ ์‹œ๊ฐ„์„ ๋‹ค๋ฃฐ ๋ชฉ์ ์œผ๋กœ ์ œ๊ณต๋˜์—ˆ๋˜ Date ํด๋ž˜์Šค๋Š” ์ž˜ ์‚ฌ์šฉ๋˜์ง€ ์•Š์ง€๋งŒ, Calendar๋กœ ๋ณ€ํ™˜ํ•˜๊ฑฐ๋‚˜ Calendar๋ฅผ Date๋กœ ๋ณ€ํ™˜ํ•  ์ค„ ์•Œ์•„์•ผ ํ•œ๋‹ค.

1. Calendar๋ฅผ Date๋กœ ๋ณ€ํ™˜

Calendar cal = Calendar.getInstance(); // Calendar ํด๋ž˜์Šค ํ˜ธ์ถœ
Date day = cal.getTime(); // Date ํด๋ž˜์Šค๋กœ ๋ณ€ํ™˜

2. Date๋ฅผ Calendar๋กœ ๋ณ€ํ™˜

Date d = new Date(); //Date ํด๋ž˜์Šค ๊ฐ์ฒด ์ƒ์„ฑ
Calendar cal = Calendar.getInstance(); // Calendar ํด๋ž˜์Šค ํ˜ธ์ถœ
cal.setTime(d); // Calendar์˜ ์‹œ๊ฐ„์„ d๋กœ ๋งž์ถค

Calendar ํด๋ž˜์Šค์˜ ์‚ฌ์šฉ


import java.util.Calendar;

class Main{
    public static void main(String[] args){

    Calendar cal = Calendar.getInstance(); 

    //๋จผ์ € Calendar ํด๋ž˜์Šค๋ฅผ cal ์ด๋ผ๋Š” ์ด๋ฆ„์œผ๋กœ ํ˜ธ์ถœํ•˜๊ณ 

    System.out.println(cal.get(Calendar.(static ์ƒ์ˆ˜)));
    //์œ„์™€ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ์‚ฌ์šฉํ•˜๋ฉด ๋˜๊ณ , static ์ƒ์ˆ˜ ์ž๋ฆฌ์— ๋“ค์–ด๊ฐ€๋Š” ์ข…๋ฅ˜๋Š” ๋งค์šฐ ๋‹ค์–‘ํ•˜๋‹ค.

    System.out.println(cal.get(Calendar.YEAR)); 
    //๋…„๋„ 
    System.out.println(cal.get(Calendar.MONTH)); 
    //์›” : 0~ 11 (11=12์›”)
    System.out.println(cal.get(Calendar.WEEK_OF_YEAR)); 
    //์ด ํ•ด์˜ ๋ช‡์งธ ์ฃผ์ธ์ง€
    System.out.println(cal.get(Calendar.WEEK_OF_MONTH)); 
    //์ด ๋‹ฌ์˜ ๋ช‡์งธ ์ฃผ์ธ์ง€
    System.out.println(cal.get(Calendar.DATE)); 
    //๋ช‡ ์ผ์ธ์ง€
    System.out.println(cal.get(Calendar.DAY_OF_MONTH)); 
    //์ด ๋‹ฌ์˜ ๋ช‡์ผ์ธ์ง€
    System.out.println(cal.get(Calendar.DAY_OF_YEAR)); 
    //์ด ํ•ด์˜ ๋ช‡์ผ์ธ์ง€ 
    System.out.println(cal.get(Calendar.DAY_OF_WEEK)); 
    //์š”์ผ 1~7 (1:์ผ์š”์ผ)
    System.out.println(cal.get(Calendar.HOUR)); 
    //์‹œ๊ฐ„ (0~11)
    System.out.println(cal.get(Calendar.HOUR_OF_DAY)); 
    //์‹œ๊ฐ„(0~23)
    System.out.println(cal.get(Calendar.MINUTE)); 
    //๋ถ„
    System.out.println(cal.get(Calendar.SECOND)); 
    //์ดˆ
    System.out.println(cal.getActualMaximunm(Calendar.DATE)); 
    //์ด ๋‹ฌ์˜ ๋งˆ์ง€๋ง‰ ์ผ
    }
}

Calendar ํด๋ž˜์Šค๋ฅผ ์ƒ์„ฑํ•˜๋ฉด ๊ธฐ๋ณธ๊ฐ’์œผ๋กœ ์˜ค๋Š˜ ๋‚ ์งœ์™€ ์‹œ๊ฐ„์ด ์ €์žฅ๋œ๋‹ค.

cal.get()์ด ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ์–ด์˜ค๋Š” ๊ฒƒ์ด๋ผ๋ฉด,

cal.set()์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋‹ค.

๋งŒ์•ฝ 2022-03-01์˜ ๋‚ ์งœ๋ฅผ ๊ฐ€์ง„ Calendar ํด๋ž˜์Šค๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ํ•˜๋ฉด ๋œ๋‹ค. (Month ํ•„๋“œ๋Š” 1์›”์ด 0)

import java.util.Calendar;

class Main{
    public static void main(String[] args){

    Calendar cal = Calendar.getInstance();

    cal.set(2022,2,1);

    System.out.println(cal.get(Calendar.YEAR));
    //2022์ด ์ถœ๋ ฅ๋  ๊ฒƒ
    System.out.println(cal.get(Calendar.MONTH));
    //2์ด ์ถœ๋ ฅ๋  ๊ฒƒ
    System.out.println(cal.get(Calendar.DATE));
    //1์ด ์ถœ๋ ฅ๋  ๊ฒƒ
    }
}

cal.add() ์™€ cal.roll()์„ ์‚ฌ์šฉํ•˜๋ฉด ์›ํ•˜๋Š” ํ•„๋“œ์— ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ฑฐ๋‚˜ ๋บ„ ์ˆ˜ ์žˆ๋‹ค.

import java.util.Calendar;

class Main{
    public static void main(String[] args){
    Calendar cal = Calendar.getInstance();
    cal.set(2022,0,31);//2022๋…„ 1์›” 31์ผ๋กœ ์ง€์ •ํ–ˆ์„๋•Œ (MONTH ํ•„๋“œ๋Š” 0๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๋ฏ€๋กœ 0์ด 1์›”์ด๋‹ค.)

    cal.add(Calendar.YEAR,1);
    //YEAR์— +1 ์„ ํ•ด์ค€๋‹ค.
    cal.add(Calendar.MONTH,-1);
    //MONTH ์— -1 ์„ ํ•ด์ค€๋‹ค.

    cal.roll(Calendar.DATE,1);
    //roll์ด๋ฏ€๋กœ MONTH์—๋Š” ์˜ํ–ฅ์„ ์ฃผ์ง€ ์•Š๊ณ  DATE๋งŒ 1๋กœ ๋ณ€ํ•  ๊ฒƒ
    }
}

add๋Š” ์˜ˆ๋ฅผ ๋“ค์–ด 7์›”31์ผ ์ธ ์ƒํ™ฉ์—์„œ DATEํ•„๋“œ์— +1์„ ํ•ด์ฃผ๋ฉด MONTH ํ•„๋“œ์—๋„ ์˜ํ–ฅ์„ ์ฃผ์–ด 8์›”1์ผ์ด ๋˜์ง€๋งŒ,

rollํ•„๋“œ๋Š” ๋‹ค๋ฅธ ํ•„๋“œ์—๋Š” ์˜ํ–ฅ์„ ์ฃผ์ง€ ์•Š์•„์„œ 7์›”1์ผ์ด ๋œ๋‹ค.