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.
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
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));
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
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));
}
}
[JAVA] Calendar Class 사용하기
🇰🇷 Korean
2020.07.23 - [개발 창고/Java] - [JAVA] Calendar Class 사용하기
'개발 창고 > 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 |