Java Calendar Class Time Operation은 아마도 캘린더를 만들고 관리하는 가장 쉬운 솔루션 일 것입니다. 데모 코드는 간단하여 시간의 축적 및 뺄셈, 날짜와 시간 비교를 보여줍니다.
참고 :
달력의 달은 0에서 시작합니다. 즉, 연중 12 개월은 0 ~ 11으로 표현됩니다.
calendar.day_of_week의 정의와 값은 다음과 같습니다.
캘린더 .sunday = 1
Calendar.monday = 2
Calendar.tuesday = 3
Calendar.wednesday = 4
캘린더. 목요일 = 5
Calendar.Friday = 6
Calendar.saturday = 7
SimpledateFormat의 형식 정의
Java Calendar Demo 코드는 다음과 같습니다.
패키지 데모; import java.util.date; import java.text.simpledateformat; import java.text.dateformat; import java.text.parseexception; java.util.calendar 가져 오기; 공개 클래스 테스트 {public test () {} public static void main (String [] args) {// 문자열 변환 날짜 형식 // dateformat fmtdateTime = new SimpledateFormat ( "YYYY-MM-DD HH : MM : SS"); // 들어오는 매개 변수를 수신 // 문자열 strdate = args [1]; // 날짜 형식을 가져옵니다. // 날짜 날짜 = fmtdatetime.parse (strdate); // 오늘 날짜 시간의 전체 표시 str = (new simpledateformat ( "yyyy-mm-dd hh : mm : ss : sss"). 형식 (new date ()); System.out.println (str); // 캘린더 객체 생성 캘린더 캘린더 = calendar.getInstance (); {// 캘린더의 시간을 설정하려면 {// 메소드 // 전달 된 시간 형식 SimpledateFormat dateformat = new SimpledateFormat ( "yyyy-md h : m : s"); // 날짜 날짜를 지정합니다. dateformat.parse ( "2013-6-1 13:24:16"); // 날짜까지 캘린더를 설정합니다. 세트 타임 (날짜); // 방금 설정된 시간을 특정 형식으로 표시합니다. System.out.println (str); } catch (parseException e) {e.printstacktrace (); } // 또는 다른 캘린더 방법 // 연도, 월, 날짜, 시간, 시간, 두 번째 캘린더 = calendar.getInstance (); Calendar.Set (2013, 1, 2, 17, 35, 44); str = (new simpledateformat ( "yyyy-mm-dd hh : mm : ss : sss"). 형식 (calendar.gettime ()); System.out.println (str); // 현재 시간을 가져 오는 캘린더 메소드 // 초기화 (재설정) Calendar Object Calendar = Calendar.getInstance (); // 또는 날짜를 사용하여 캘린더 객체 캘린더를 초기화합니다. // settime은 위의 줄과 유사합니다 // 날짜 날짜 = 새 날짜 (); // calendar.settime (날짜); str = (new simpledateformat ( "yyyy-mm-dd hh : mm : ss : sss"). 형식 (calendar.gettime ()); System.out.println (str); // 연도를 표시 int 연도 = calendar.get (calendar.year); System.out.println ( "연도는 =" + string.valueof (년)); // 월 (0부터 시작하여 실제 디스플레이를 추가해야합니다) int month = calendar.get (calendar.month); System.out.println ( "nth is =" + (Month + 1)); // 요일의 며칠 int week = calendar.get (calendar.day_of_week); System.out.println ( "주가 =" + 주); // 올해의 n 번째 날 int day_of_year = calendar.get (calendar.day_of_year); System.out.println ( "day_of_year는 =" + day_of_year); // 이번 달의 n 번째 날 int day_of_month = calendar.get (calendar.day_of_month); system.out.println ( "day_of_month =" + string.valueof (day_of_month)); // 3 시간 후 Calendar.add (Calendar.hour_of_day, 3); int hour_of_day = calendar.get (calendar.hour_of_day); System.out.println ( "Houst_of_day + 3 =" + Hour_of_day); // 현재 분 int minute = calendar.get (calendar.minute); System.out.println ( "minute =" + minute); // calendar.add (calendar.minute, 15); minute = calendar.get (calendar.minute); System.out.println ( "minute + 15 =" + minute); // calendar.add (calendar.minute, -30); minute = calendar.get (calendar.minute); System.out.println ( "minute -30 =" + minute); // 형식 display str = (new simpledateformat ( "yyyy-mm-dd hh : mm : ss : ss"). 형식 (calendar.gettime ()); System.out.println (str); // 캘린더 재설정 현재 시간 캘린더를 표시합니다. str = (new simpledateformat ( "yyyy-mm-dd hh : mm : ss : ss"). 형식 (calendar.gettime ()); System.out.println (str); // 시간 캘린더 calendarnew = calendar.getInstance ()를 비교하기위한 캘린더를 만듭니다. // 5 시간 전으로 설정하면 후자는 크며 -1 calendarnew.add (calendar.hour, -5)를 표시합니다. System.out.println ( "시간 비교 :" + calendarnew.compareto (calendar)); // 7 시간 전으로 설정하면 전자는 크고 1 Calendarnew.add (calendar.hour, +7)를 표시합니다. System.out.println ( "시간 비교 :" + calendarnew.compareto (calendar)); // 2 시간을 반환하면 시간이 동일하며 0 Calendarnew.add (Calendar.hour, -2)를 표시합니다. System.out.println ( "시간 비교 :" + calendarnew.compareto (calendar)); }} 시차를 계산하려면 Calendar.getTimeInmillis ()를 사용하여 두 번 사이의 마이크로 초 시차를 얻고 예를 들어 위상차의 일수를 얻기 위해 변환 할 수 있습니다. 코드는 다음과 같습니다.
// 마이크로 초 시차 시간 차이 Long Val = Calendarend.getTimeInmillis () -CalendArbegin.getTimeInmillis (); // 변환 후 일수를 얻습니다. 긴 날 = val / (1000 * 60 * 60 * 24);
위는이 기사의 모든 내용입니다. 모든 사람의 학습에 도움이되기를 바랍니다. 모든 사람이 wulin.com을 더 지원하기를 바랍니다.