개발 창고/Java

[JAVA] How to use Calendar Class

로이제로 2024. 2. 11. 11:32
반응형

The Calendar class is one of the most popular items for Java developers.

Calendar is a feature included in java's util and is a built-in offering on Java-based platforms.

Java Platform SE 7


EXAMPLE

The source below is the one that extracts the date this Saturday of last year as of today.
For SimpleDateFormat
You can check it in the link below.

2024.02.10 - [개발 창고/Java] - [JAVA] How to use SimpleDateFormat Class

 

[JAVA] How to use SimpleDateFormat Class

When I develop Java, I forget how to use it when I want to return today's date lightly If you think about it, it's a simple date format like a class name Date date = new Date(); // Return today's date to Date type String format = "yyyy-MM-dd"; // Year(4)-M

royzero.tistory.com

SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd E");

Calendar cal = Calendar.getInstance();                  // Calendar Declaration
// Calendar cal = Calendar.getInstance(Locale.KOREA);   // Tip. If Locale is not included, the Default value is applied (based on installed environment)
// cal.setTime(new Date());                             // Tip. If you do not specify a specific date, the date will be applied as Default
System. out.println(fmt.format(cal.getTime()));         // 2024-02-11 Sun

int year = cal.get(Calendar.YEAR);                      // Return this year as of today
int week = cal.get(Calendar.WEEK_OF_YEAR);              // Return week of year as of today
cal.set(Calendar. YEAR, year-1);                        // As of today - 1 year applied
cal.set(Calendar. WEEK_OF_YEAR, week);                  // As of today week of year applied
cal.set(Calendar. DAY_OF_WEEK, Calendar.MONDAY);        // As of today monday applied
System. out.println(fmt.format(cal.getTime()));         // 2023-02-13 Mon

FIELD AND DESCRIPTION

FIELD DESCRIPTION
YEAR Field number for get and set indicating the year.
MONTH Field number for get and set indicating the month.
(Returns January to 0 and December to 11. +1 for actual marking)
DATE Field number for get and set indicating the day of the month.
HOUR Field number for get and set indicating the hour of the morning or afternoon.
(AM/PM BASED. 0~11)
HOUR_OF_DAY Field number for get and set indicating the hour of the day.
(24HOURE BASED. 0~23)
MINUTE Field number for get and set indicating the minute within the hour.
SECOND Field number for get and set indicating the second within the minute.
MILLISECOND Field number for get and set indicating the millisecond within the second.
(All milliseconds use cal.getTimeInMillis())
DAY_OF_YEAR Field number for get and set indicating the day number within the current year.
WEEK_OF_MONTH Field number for get and set indicating the week number within the current month. (1~5)
WEEK_OF_YEAR Field number for get and set indicating the week number within the current year. (1~53)

 

You can check the test source by attaching the source below to the source above.

SimpleDateFormat fmt02 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss D E W w");
System. out.println(fmt02.format(cal.getTime()));
System.out.println("YEAR\t" + cal.get(Calendar.YEAR));
System.out.println("MONTH\t" + cal.get(Calendar.MONTH));
System.out.println("DATE\t" + cal.get(Calendar.DATE));
System.out.println("HOUR\t" + cal.get(Calendar.HOUR));
System.out.println("HOUR_OF_DAY\t" + cal.get(Calendar.HOUR_OF_DAY));
System.out.println("MINUTE\t" + cal.get(Calendar.MINUTE));
System.out.println("SECOND\t" + cal.get(Calendar.SECOND));
System.out.println("MILLISECOND\t" + cal.get(Calendar.MILLISECOND));
System.out.println("DAY_OF_YEAR\t" + cal.get(Calendar.DAY_OF_YEAR));
System.out.println("WEEK_OF_MONTH\t" + cal.get(Calendar.WEEK_OF_MONTH));
System.out.println("WEEK_OF_YEAR\t" + cal.get(Calendar.WEEK_OF_YEAR));

Execution result

 

 It has been returned to 2019 because the existing source has been doing -1 year and it is as below the first line of the result.

 

RESULT DESCRIPTION
2023 Year
1 Month
13 Date
2 Hour (24 hours based)
17 Minutes
53 Seconds
44 Day 44 as of January 1st
3 Week 3 as of this month
7 7th week as of this year

More details can be found at the link below.

https://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html

 

Calendar (Java Platform SE 7 )

Adds or subtracts (up/down) a single unit of time on the given time field without changing larger fields. For example, to roll the current date up by one day, you can achieve it by calling: roll(Calendar.DATE, true). When rolling on the year or Calendar.YE

docs.oracle.com


Full Source

import java.util.Calendar;
import java.text.SimpleDateFormat;

class Main {
	public static void main(String[] args) {
		SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd E");
		
		Calendar cal = Calendar.getInstance();                  // Calendar Declaration
		// Calendar cal = Calendar.getInstance(Locale.KOREA);   // Tip. If Locale is not included, the Default value is applied (based on installed environment)
		// cal.setTime(new Date());                             // Tip. If you do not specify a specific date, the date will be applied as Default
		System. out.println(fmt.format(cal.getTime()));         // 2024-02-11 Sun
		
		int year = cal.get(Calendar.YEAR);                      // Return this year as of today
		int week = cal.get(Calendar.WEEK_OF_YEAR);              // Return week of year as of today
		cal.set(Calendar. YEAR, year-1);                        // As of today - 1 year applied
		cal.set(Calendar. WEEK_OF_YEAR, week);                  // As of today week of year applied
		cal.set(Calendar. DAY_OF_WEEK, Calendar.MONDAY);        // As of today monday applied
		System. out.println(fmt.format(cal.getTime()));         // 2023-02-13 Mon

		SimpleDateFormat fmt02 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss D E W w");
		System. out.println(fmt02.format(cal.getTime()));
		System.out.println("YEAR\t" + cal.get(Calendar.YEAR));
		System.out.println("MONTH\t" + cal.get(Calendar.MONTH));
		System.out.println("DATE\t" + cal.get(Calendar.DATE));
		System.out.println("HOUR\t" + cal.get(Calendar.HOUR));
		System.out.println("HOUR_OF_DAY\t" + cal.get(Calendar.HOUR_OF_DAY));
		System.out.println("MINUTE\t" + cal.get(Calendar.MINUTE));
		System.out.println("SECOND\t" + cal.get(Calendar.SECOND));
		System.out.println("MILLISECOND\t" + cal.get(Calendar.MILLISECOND));
		System.out.println("DAY_OF_YEAR\t" + cal.get(Calendar.DAY_OF_YEAR));
		System.out.println("WEEK_OF_MONTH\t" + cal.get(Calendar.WEEK_OF_MONTH));
		System.out.println("WEEK_OF_YEAR\t" + cal.get(Calendar.WEEK_OF_YEAR));
	}
}

Execution result


[JAVA] Calendar Class 사용하기

🇰🇷 Korean

2020.07.23 - [개발 창고/Java] - [JAVA] Calendar Class 사용하기

 

[JAVA] Calendar Class 사용하기

Calendar 클래스는 자바 개발자라면 자주 쓰는 항목 중 하나입니다. Calendar는 java의 util에 포함된 기능으로 자바 기반 플랫폼에서는 기본 제공이라고 보시면 됩니다. 아래의 소스는 오늘자 기준으

royzero.tistory.com

 

반응형

'개발 창고 > Java' 카테고리의 다른 글

[JAVA] How to use SimpleDateFormat Class  (91) 2024.02.10
[JAVA] How to join an array  (154) 2024.01.16
[JAVA] 이메일 유효성 검사  (0) 2023.03.16
[POI] 엑셀 폰트 변경  (0) 2023.02.10
[Nexacro] DataSet to Class  (0) 2023.01.27