1. java.util.calendar 소개
캘린더 클래스는 특정 모멘트를 해, 월, Day_of_Month, Hour 등과 같은 일련의 달력 필드로 변환하는 몇 가지 방법을 제공하는 초록 클래스이며 다음 주 날짜 얻기와 같은 달력 필드를 조작하는 몇 가지 방법을 제공합니다. 순간은 밀리 초 값으로 표현 될 수 있으며, 이는 에포크의 오프셋입니다 (예 : 00 : 00 : 00 : 00.000, GMT, 1970 년 1 월 1 일, GMT).
2. 간단한 예
// 출력 날짜 java.text.simpledateformat 형식 = new java.text.simpledateformat ( "yyyy-mm-dd"); calendar cal = calendar.getinstance (); // 현재 날짜를 가져갑니다. System.out.println ( "오늘은" + format.format (cal.gettime ()); cal = calendar.getInstance (); cal.add (calendar.day_of_month, -1); // 현재 날짜 전에 하루를 취하십시오. calendar.getInstance (); cal.add (calendar.day_of_month, +1); // 현재 날짜가 다음 날 날짜를 찍습니다 .system.out.println ( "다음 날은 :" + format.format (cal.gettime ());
또는
java.util.date today = new java.util.date (); java.text.simpledateformat dateformat = new java.text.simpledateformat ( "yyyy-mm-dd"); java.text.simpledateformat datetimeformat = new java.text.simpledateformat ( "yyyy-mm-dd hh : mm : ss"; "+dateformat.format (오늘)); System.out.println ("지금은 "+dateTimeformat.format (오늘));2. 특정 시간을 구성하십시오
java.text.simpledateformat format = new java.text.simpledateformat ( "yyyy-mm-dd"); 캘린더 캘린더 = New GregorianCalendar (2007, 11, 25,0,0,0); 날짜 = calendar.gettime (); System.out.println ( "2007 크리스마스 :"+format.format (date));
GregorianCalendar 생성자의 매개 변수는 다음과 같습니다.
또는
java.text.simpledateformat format = new java.text.simpledateformat ( "yyyy-mm-dd"); java.util.date date = format.parse ( "2007-12-25"); System.out.println ( "2007 크리스마스 :"+format.format (date));
3. 날짜의 각 부분을 가져 가십시오
int 년 = calendar.get (calendar.year); int month = calendar.get (calendar.month) +1; int day = calendar.get (calendar.day_of_month); int hour = calendar.get (calendar.hour_of_day); int minute = calendar.get (calendar.minute); int second = calendar.get (calendar.second);
달에 1을 추가하십시오
4. 현재 달의 최대 일 수를 얻으십시오.
Calendar Cal = Calendar.getInstance (); int day = cal.getActualMaximum (calendar.day_of_month); System.out.println (일);
5. 달의 마지막 날을 찍으십시오
Calendar Cal = Calendar.getInstance (); int maxday = cals.getActualMaximum (calendar.day_of_month); java.text.format formatter3 = new java.text.simpledateformat ( "yyyy-mm-"+maxday); System.out.println (formatter3.format (cal.gettime ());
6. 월의 첫날을 찍으십시오
java.text.simpledateformat format = new java.text.simpledateformat ( "yyyy-mm-01"); java.util.date firstday = new java.util.date (); System.out.println ( "첫날은"+formats.format (Firstday));
7. 두 날짜 사이의 일수를 찾으십시오
java.text.simpledateformat format = new java.text.simpledateformat ( "yyyy-mm-dd"); java.util.date barkindate = format.parse ( "2007-12-24"); java.util.date enddate = format.parse ( "2007-12-25"); Long Day = (date.gettime () -MyDate.getTime ())/(24*60*60*1000); System.out.println ( "분리 된 일 수 ="+일);
8. 1 년 전 날짜
java.text.format formatter = new java.text.simpledateformat ( "yyyy-mm-dd"); java.util.date today date = new java.util.date (); long beforeTime = (Todaydate.getTime ()/1000) -60*60*24*365; Todaydate.settime (BeforeTime*1000); 문자열 beforedate = formatter.format (오늘날); System.out.println (Beforedate);
9. 1 년 후 날짜
java.text.format formatter = new java.text.simpledateformat ( "yyyy-mm-dd"); java.util.date today date = new java.util.date (); 긴 후 = (Today.getTime ()/1000)+60*60*24*365; Todaydate.settime (Aftertime*1000); 문자열 송글데이트 = formatter.format (Today date); System.out.println (AfterDate);
10 시간 또는 10 시간 후
java.util.calendar cal = java.util.calendar.getinstance (); cal.settime (dateoper); cal.add (java.util.calendar.hour_of_day, 10); System.out.println ( "날짜 :"+forma.format (cal.gettime ());
11. 10 시간 전
java.util.calendar cal = java.util.calendar.getinstance (); cal.settime (dateoper); cal.add (java.util.calendar.hour_of_day, -10); System.out.println ( "날짜 :"+forma.format (cal.gettime ());
12. 현재 날짜의 월요일과 일요일
simpledateformat dateformat = new simpledateformat ( "yyyymmdd"); gregoriancalendar cal = new gregoriancalendar (); int dayinweek = cal.get (calendar.day_of_week); int 오프셋 = 0; if (dayinweek == 1) {// 일요일 오프셋 = 6; 2;} cal.add (gregoriancalendar.day_of_month, -offset); 문자열 sday = dateformat.format (cal.gettime ()); cal.add (gregoriancalendar.day_of_month, 6); String eday = dateformat.format (cal.gettime.out.out.println) sday); system.out.println ( "이번 주 일요일 :" + eday);12. 올해에 속한 현재 날짜의 주를 받으십시오.
gregoriancalendar cal = new GregorianCalendar (); int weekofyear = cal.get (calendar.week_of_year); System.out.println ( "이번 주에는 다음 주에 속한다 :" + Weekofyear);
요약
위는이 기사의 전체 내용입니다. 이 기사의 내용이 귀하의 연구 나 업무에 도움이되기를 바랍니다. 궁금한 점이 있으면 의사 소통을 위해 메시지를 남길 수 있습니다.