이 기사는 Java Date Operation Tool 클래스를 공유하며 하루 전과 하루 후에 지정된 날짜를 얻습니다. 날짜 변환; 두 날짜와 참조를위한 기타 도구 사이의 일수. 특정 내용은 다음과 같습니다
가져 오기 java.text.parseexception; import java.text.simpledateformat; import java.util.arraylist; import java.util.calendar; import java.util.date; import java.util.list; 공개 클래스 dateUtil { / ** * 지정된 날짜가 획득되기 전날 * * @param specifiedday * @param format * @return * @throws 예외 * / public static date getdaybefore (날짜, 문자열 형식) {calendar c = calendar.getinstance (); C. 세트 타임 (날짜); int day = c.get (calendar.date); c.set (calendar.date, day -1); 문자열 daybeforest = new simpledateformat (format) .format (c.gettime ()); 날짜 daybefefore = null; try {daybefore = new SimpledateFormat (format) .parse (daybeforest); } catch (parseException e) {e.printstacktrace (); } 전날 반환; } / ** * 지정된 날짜를 얻은 다음 날 * @param specifiedday * @return * / public static date getDayAfter (날짜, 문자열 형식) {calendar c = calendar.getInstance (); C. 세트 타임 (날짜); int day = c.get (calendar.date); c.set (calendar.date, day + 1); 문자열 dayAfterstr = new SimpleDateFormat (format) .format (c.gettime ()); 날짜 dayafter = null; try {dayafter = new SimpledateFormat (format) .parse (dayAfterstr); } catch (parseException e) {e.printstacktrace (); } return dayAfter; } / ** * 지정된 날짜 전날 * @param specifiedday * @param format * @return * @throws Exception * / public static string getSpecifieddaybefore (String specifiedday, String format) {// simpledateformat simpledateformat = new // simpledateformat ( "yyyy-mm-dd"); Calendar C = Calendar.getInstance (); 날짜 날짜 = null; try {date = new SimpledateFormat ( "YY-MM-DD"). 구문 분석 (지정일); } catch (parseException e) {e.printstacktrace (); } C. 세트 타임 (날짜); int day = c.get (calendar.date); c.set (calendar.date, day -1); String daybefore = new SimpledateFormat ( "yyyy-mm-dd"). 형식 (c.gettime ()); 전날 귀국; } / ** * 지정된 날짜를 얻은 다음날 * @param specifiedday * @return * / public static string getSpecifieddayAfter (문자열 지정일, 문자열 형식) {calendar c = calendar.getInstance (); 날짜 날짜 = null; try {// date = new simpledateformat ( "yy-mm-dd"). 구문 분석 (지정일); 날짜 = 새로운 simpledateformat (형식) .parse (지정일); } catch (parseException e) {e.printstacktrace (); } C. 세트 타임 (날짜); int day = c.get (calendar.date); c.set (calendar.date, day + 1); // string dayAfter = new // simpledateformat ( "yyyy-mm-dd"). 형식 (c.gettime ()); String DayAfter = new SimpledateFormat (format) .format (c.gettime ()); 귀국 dayafter; } / ** * 지정된 형식의 문자열로 날짜 유형을 문자열로 정확하게 정확합니다 * @param day date * @param format 형식 * @return * / public static string date2string (날짜 일, 문자열) {String datest = new SimpledateFormat (format) .format (day.gettime ()); 반환 Datest; } / ** * 지정된 형식의 날짜 유형으로 문자열을 변환 * @param day date * @param format 형식 * @return * / public static date String2date (String datest, String format) {date strdate = null; try {strdate = new SimpledateFormat (format) .parse (dateSt); } catch (parseException e) {e.printstacktrace (); } return strdate; } / **** @title : datedate2comparevalue* @description : 두 날짜 유형 비교 크기* @param dayafter 날짜 유형* @param 날짜 형식 유형* @param formatting* @return boolean* @throws* / public static boolean datedate2comparevalue (date dayafter, date sd) SimpledateFormat (패턴); try {date d1 = sdf.parse (sdf.format (dayafter)); 날짜 d2 = sdf.parse (sdf.format (날짜)); if (d1.gettime ()> = d2.gettime ()) {return true; }} catch (parseexception e) {e.printstacktrace (); } false를 반환합니다. } / ** * 두 날짜 크기의 크기를 비교 * @Title : DateCompareValue * @Description : 안정적인 1 개의 날짜 유형 비교 * @param date1 문자열 유형 * @param date2 date 유형 * @param @return * @return boolean * @throws * / public statrdectecompormpompormportvalue (string dateStrdeCompompompompompompormArevalue) simpledateformat sdf = 새로운 simpledateformat (패턴); try {date d1 = sdf.parse (date1); 날짜 d2 = sdf.parse (sdf.format (날짜)); if (d1.gettime ()> = d2.gettime ()) {return true; }} catch (parseexception e) {// todo 자동 생성 캐치 블록 e.printstacktrace (); } false를 반환합니다. } / ** * 두 날짜 크기의 크기를 비교 * @Title : DateStr2CompareValue * @Description : 두 문자열 유형 비교 시간 크기 * @param date1 * @param date2 * @return boolean * @throws * / @suppresswarnings ( "Unused") public static datestr2comporvalue (string1, string datest2compcompicpcompicpcomporvalue) simpledateformat sdf = 새로운 simpledateformat (패턴); try {date d1 = sdf.parse (date1); 날짜 d2 = sdf.parse (date2); if (d1.gettime ()> = d2.gettime ()) {return true; }} catch (parseexception e) {e.printstacktrace (); } false를 반환합니다. } / **** @title : get2dateListDate* @Date 2016 년 5 월 17 일 오후 2시 11 분 48 초* @Description : 시간 내내 날짜를 얻습니다. 시간 내내 시간을 얻으십시오. 목록 <date> get2datelistDate (String predate, String NextDate, String Format) {list <date> list = new ArrayList <> (); if (nextDate.equals (predate)) {// 시작 시간과 종료 시간이 같으면 list.add (dateUtil.string2date (predate, format))로 직접 돌아갑니다. 반환 목록; } string nexDate = dateUtil.getSpecifiedDayAfter (predate, format); list.add (dateUtil.string2date (predate, format)); list.add (dateUtil.string2date (nexDate, format)); while (! nexdate.equals (nextDate)) {String nextNextDate = dateUtil.getSpecifiedDayAfter (nexDate, format); list.add (dateUtil.string2date (NextDate, Format)); nexdate = NextNextDate; } 반환 목록; } / **** @title : get2dateListDate* @Date 2016 년 5 월 17 일 오후 2시 11 분 48 초* @Description : 시간 내내 시간을 획득하십시오. List <string> get2datelistDatest (String predate, String NextDate, String Format) {list <string> list = new ArrayList <> (); if (nextDate.equals (predate)) {// 시작 시간이 종료 시간과 같으면 같은 날 목록 (predate); 반환 목록; } string nexDate = dateUtil.getSpecifiedDayAfter (predate, format); list.add (predate); list.add (nexdate); while (! nexdate.equals (nextDate)) {String nextNextDate = dateUtil.getSpecifiedDayAfter (nexDate, format); list.add (NextDate); nexdate = NextNextDate; } 반환 목록; } / ** * * @title : get2datelistwithDate * @Date 2016 년 5 월 26 일 오전 9시 20 분 29 일 오전 9:20:29 AM * @Description : 두 날짜 사이에 날짜를 가져옵니다 * @param @param 시작 날짜 날짜 유형 * @param enddate end 날짜 유형 * @param @return * @return 목록> datelist * @throws * / problic grow get2dateListWithDate (날짜 startDate, Date EndDate, String Format) {list <date> list = new ArrayList <> (); if (startDate.getTime ()> endDate.getTime ()) {return list; } string startDatestr = dateUtil.date2string (startDate, format); 문자열 endDatestreps = dateUtil.date2string (endDate, format); if (startDatestr.equals (endDatest)) {// 시작 시간이 종료 시간과 같으면 같은 날 목록 (startDate); 반환 목록; } 날짜 nextDate = dateUtil.getDayAfter (startDate, format); 문자열 nextDatestrest = dateUtil.date2string (NextDate, Format); // string endDatestr = dateUtil.date2string (endDate, format); list.add (startDate); list.add (NextDate); while (! nextDatest.equals (endDatest)) {String nextNextDate = dateUtil.getSpecifiedDayAfter (NextDatest, Format); list.add (dateUtil.string2date (NextDate, Format)); NextDatestr = NextNextDate; } 반환 목록; } public static void main (String [] args)은 예외를 { / ** / String predate = getSpecifieddaybefore ( "2016-05-01", "yyyy-mm-dd"); 문자열 NextDate = getSpecifieddayAfter ( "2016-05-03", "yyyy-mm-dd"); 날짜 befroday = getdaybefore (new date (), "yyyy-mm-dd"); 날짜 데이터 이후 = getDayAfter (새 날짜 (), "yyyy-mm-dd"); // system.out.println ( "전날 :" + predate + "다음 날 :" + nextDate); // system.err.println ( "전날 :" + date2string (befroday, "yyyy-mm-dd") + "// 다음 날 :" + dateapter); 문자열 mat = "yyyy-mm-dd"; // database dateTeMeformat에서 simpledateformat을 읽는 것이 가장 좋습니다. 날짜 dd = dateUtil.getDaybefore (new date (), mat); // date befroday = getDaybefore (new date (), "yyyy-mm-dd"); 문자열 sd = date2string (befroday, "yyyy-mm-dd"); 문자열 datest = dateTimeFormat.format (befroday); // system.out.println ( "dateSt ="+DateStr+"SD"+SD); // list <date> listdate = get2datelistDate ( "2016-05-01", "2016-05-03", "yyyy-mm-dd"); // list <string> listdatest = get2dateListDatest ( "2016-05-01", "2016-05-03", "yyyy-mm-dd"); 날짜 startDate = 새 날짜 (); 날짜 endDate = dateTimeFormat.parse ( "2016-05-31"); list <date> listdate = get2datelistwithDate (startDate, endDate, "yyyy-mm-dd"); for (int i = 0; i <listdate.size (); i ++) {system.err.println (listDate.get (i)); } /*for (int i = 0; i <listdatest.size (); i ++) {//system.out.println(listdatest.get(i)); }*/}위의 내용은이 기사에 관한 모든 것입니다. 모든 사람들이 Java 프로그래밍을 배우는 것이 도움이되기를 바랍니다.