Java.util.Calendar
lastmod 2020-01-22created 2014-08-066 edits
Calendar is an abstract base class for converting between a Date object and a set of integer fields such as YEAR, MONTH, DAY, HOUR, and so on. (A Date object represents a specific instant in time with millisecond precision. See java.util.Date for details on the Date class.)
Subclasses of Calendar interpret a Date according to the rules of a specific calendar system. The JDK provides one concrete subclass of Calendar, GregorianCalendar. Future subclasses could represent the various types of lunar calendars used in many parts of the world.
Example
Simple version
How to get the current date and current time in Java and print them as strings:
Calendar cal = Calendar.getInstance();
String dateToString = String.format("%04d-%02d-%02d", cal.get(Calendar.YEAR), cal.get(Calendar.MONTH) + 1, cal.get(Calendar.DAY_OF_MONTH));
String timeToString = String.format("%02d:%02d:%02d", cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE), cal.get(Calendar.SECOND));
Today calender
Here is how to get today's date.
Calendar now = Calendar.getInstance();
now.setTimeInMillis(System.currentTimeMillis());
int nowYear = now.get(Calendar.YEAR);
int nowMonth = now.get(Calendar.MONTH);
int nowDay = now.get(Calendar.DATE);
Log.e("TODAY", String.format("Year(%d), Month(%d), Day(%d)", nowYear, nowMonth, nowDay));
Day of week
Here is how to get the name of today's day of the week.
import java.util.Calendar;
// ...
public static String getCurrentWeek(String[] args) {
Calendar oCalendar = Calendar.getInstance();
final String[] week = { "일", "월", "화", "수", "목", "금", "토" };
return week[oCalendar.get(Calendar.DAY_OF_WEEK) - 1] + "요일";
}
Calendar simple example
public static void main(String[] args) {
// 기본적으로 현재날짜와 시간으로 설정된다.
Calendar today = Calendar.getInstance();
System.out.println("이 해의 년도 : " + today.get(Calendar.YEAR));
System.out.println("월(0~11, 0:1월): " + today.get(Calendar.MONTH));
// (today.get(Calendar.MONTH) + 1)) 이런 형식으로 하면 다음월을 받아 올 수
// 있다. today.get(Calendar.MONTH) + 1로 하면 이상한 값이 나온다. (괄호유무)
System.out.println("월(0~11, 0:1월): " + (today.get(Calendar.MONTH) + 1));
System.out.println("이 해의 몇 째 주: " + today.get(Calendar.WEEK_OF_YEAR));
System.out.println("이 달의 몇 째 주: " + today.get(Calendar.WEEK_OF_MONTH));
// DATE와 DAY_OF_MONTH는 같다.
System.out.println("이 달의 몇 일: " + today.get(Calendar.DATE));
System.out.println("이 달의 몇 일: " + today.get(Calendar.DAY_OF_MONTH));
System.out.println("이 해의 몇 일: " + today.get(Calendar.DAY_OF_YEAR));
// 1:일요일, 2:월요일, ... 7:토요일
System.out.println("요일(1~7, 1:일요일): " + today.get(Calendar.DAY_OF_WEEK));
System.out.println("이 달의 몇 째 요일: " + today.get(Calendar.DAY_OF_WEEK_IN_MONTH));
System.out.println("오전_오후(0:오전, 1:오후): " + today.get(Calendar.AM_PM));
System.out.println("시간(0~11): " + today.get(Calendar.HOUR));
System.out.println("시간(0~23): " + today.get(Calendar.HOUR_OF_DAY));
System.out.println("분(0~59): " + today.get(Calendar.MINUTE));
System.out.println("초(0~59): " + today.get(Calendar.SECOND));
System.out.println("1000분의 1초(0~999): " + today.get(Calendar.MILLISECOND));
// 천분의 1초를 시간으로 표시하기 위해 3600000으로 나누었다.(1시간 = 60 * 60초)
System.out.println("TimeZone(-12~+12): " + (today.get(Calendar.ZONE_OFFSET) / (60 * 60 * 1000)));
// 이 달의 마지막 일을 찾는다.
System.out.println("이 달의 마지막 날: " + today.getActualMaximum(Calendar.DATE));
}
See also
- Java
- java.text.DateFormat
- java.text.SimpleDateFormat
- java.util.Date
Favorite site
- Stackoverflow: System.currentTimeMillis() vs. new Date() vs. Calendar.getInstance().getTime()
- Class java.util.Calendar
- Calendar class (printing a calendar)
- How to get the current time and convert it to a string