이 기사는 참조를 위해 Java Tool Class DateUtils의 특정 코드를 공유합니다. 특정 내용은 다음과 같습니다
가져 오기 java.text.parseexception; import java.text.simpledateformat; import java.util.calendar; import java.util.date;/*** 설명 : 공개 날짜 도구 클래스*/public class dateUtils {public static string date_format = "yyyy-mm-dd"; 공개 정적 문자열 date_time_format = "yyyy-mm-dd hh : mm : ss"; 공개 정적 문자열 date_format_chinese = "yyyyy year m m month d day"; / ** * 현재 날짜를 가져옵니다 * * * @return * */ public static string getCurrentDate () {String DateStr = null; simpledateformat df = 새로운 simpledateformat (dateUtils.date_format); datest = df.format (new date ()); 반환 Datest; } / ** * 현재 날짜와 시간을 가져옵니다 * * * @return * * / public static string getCurrentDateTime () {String Datest = null; simpledateformat df = 새로운 simpledateformat (dateUtils.date_time_format); datest = df.format (new date ()); 반환 Datest; } / ** * 현재 날짜와 시간을 가져옵니다 * * * @return * * / public static String getCurrentDateTime (String DateFormat) {String Datest = null; simpledateformat df = 새로운 simpledateformat (dateformat); datest = df.format (new date ()); 반환 Datest; } public static String dateDodateTime (날짜 날짜) {String datest = null; simpledateformat df = 새로운 simpledateformat (dateUtils.date_time_format); dateSt = df.format (날짜); 반환 Datest; } / ** * 문자열 날짜 변환 날짜 형식 * * * @param dateStr * @return * / public static 날짜 StringTodate (String DateStr) } 날짜 = 새 날짜 (); simpledateformat df = 새로운 simpledateformat (dateUtils.date_format); try {date = df.parse (dateSt); } catch (parseException e) {date = dateUtils.stringTodate (DateString, "yyyymmdd"); } 반환 날짜; } / ** * 문자열 날짜 변환 날짜 형식 * 사용자 정의 형식 * * @param datest * @return * * / public static date StringTodate (String datest, String dateformat) {date date = new Date (); simpledateformat df = 새로운 simpledateformat (dateformat); try {date = df.parse (dateSt); } catch (parseException e) {e.printstacktrace (); } 반환 날짜; } / ** * 날짜 형식 날짜 변환 문자열 형식으로 변환 * * * @param 날짜 * @return * * / public static String dateToString (날짜 날짜) {String datest = null; simpledateformat df = 새로운 simpledateformat (dateUtils.date_format); dateSt = df.format (날짜); 반환 Datest; } / ** * 날짜 형식을 문자열 형식으로 변환 사용자 정의 형식으로 변환 * @param date * @param dateformat * @return * / public static string dateToString (날짜 날짜, 문자열 dateformat) {String datest = null; simpledateformat df = 새로운 simpledateformat (dateformat); dateSt = df.format (날짜); 반환 Datest; } / ** * 날짜의 날 가치를 얻으십시오 * * * @param 날짜 * 날짜 * @return * * / public static int getdayofdate (날짜 날짜) {int d = 0; Calendar CD = Calendar.getInstance (); CD. 세트 타임 (날짜); d = cd.get (calendar.day_of_month); 반환 d; } / ** * 날짜의 달 값을 얻으십시오 * * * @param 날짜 * 날짜 * @return * * / public static int getmonthofdate (날짜 날짜) {int m = 0; Calendar CD = Calendar.getInstance (); CD. 세트 타임 (날짜); m = cd.get (calendar.month) + 1; 반환 m; } / ** * 날짜의 연도 값을 얻으십시오 * * * @param 날짜 * 날짜 * @return * * / public static int getyearofdate (날짜 날짜) {int y = 0; Calendar CD = Calendar.getInstance (); CD. 세트 타임 (날짜); y = cd.get (calendar.year); 반품 Y; } / ** * 요일 받기 * * * @param 날짜 * 날짜 * @return * * / public static int getweekofdate (날짜 날짜) {int wd = 0; Calendar CD = Calendar.getInstance (); CD. 세트 타임 (날짜); wd = cd.get (calendar.day_of_week) -1; wd 리턴; } / ** * 입력 날짜의 첫날을 받으십시오 * * * @param 날짜 * 입력 날짜 * @return * * / public static date getFirstdayOfmonth (날짜) {Calendar CD = Calendar.getInstance (); CD. 세트 타임 (날짜); cd.set (calendar.day_of_month, 1); 반환 cd.gettime (); } / ** * 입력 날짜의 마지막 날을 얻으십시오 * @param 날짜 * / public static date getLastdayOfmonth (날짜 날짜) {return dateUtils.addday (dateUtils.getFirstdayofmonth (dateUtils.addmonth (date, 1)), -1); } / ** * 도약 연도 여부를 결정 * * * @param 날짜 * 입력 날짜 * @return yes true false * * / public static boolean isleapyear (날짜 날짜) {calendar cd = calendar.getInstance (); CD. 세트 타임 (날짜); int 년 = cd.get (calendar.year); if (년 % 4 == 0 && 년 % 100! = 0 | 년 % 400 == 0) {return true; } else {return false; }} / ** * 정수 번호 * * @param 연도 * @param month * @param month * @param day * @return * * / public static date getDateByymd (int year, int month) {calendar cd = calendar.getInstance (); CD. 세트 (연도, 월 -1, 일); 반환 cd.gettime (); } / ** * 연중 해당 날짜를 가져옵니다. * @param 날짜 * 날짜 입력 * @param iyear * 연도는 이전 * @return * / public static date getyearcycleofdate (날짜, int iyear)를 나타냅니다. {calendar cd = calendar.getInstance (); CD. 세트 타임 (날짜); cd.add (calendar.year, iyear); 반환 cd.gettime (); } / ** * 월주기의 해당 날짜를 가져옵니다 * @param 날짜 * 날짜를 입력하십시오 * @param i * @return * * / public static date get monthcycleofdate (날짜 i) {calendar cd = calendar.getInstance (); CD. 세트 타임 (날짜); cd.add (calendar.month, i); 반환 cd.gettime (); } / ** * FromDate와 Todate의 차이를 몇 년 동안 계산 * * @param fromdate * @param todate * @return year * * / public static int getyearbyminusdate (날짜 Fromdate, Date Todate) {calendar df = calendar.getInstance (); df.settime (Fromdate); calendar dt = calendar.getInstance (); dt.settime (Todate); return dt.get (calendar.year) -df.get (calendar.year); } / ** * FromDate와 Todate의 차이를 몇 개월 계산 * * @param fromdate * @param todate * @return month * * / public static int getmonthbyminusdate (날짜 Fromdate, Date Todate) {Calendar df = calendar.getInstance (); df.settime (Fromdate); calendar dt = calendar.getInstance (); dt.settime (Todate); dt.get (calendar.year)*12+dt.get (calendar.month)- (df.get (calendar.year)*12+df.get (calendar.month)); } / ** * FromDate와 Todate의 차이를 며칠 계산 * * @param fromdate * @param todate * @return days * * / public static long getdaybyminusdate (Object fromdate, Object Todate) {날짜 f = dateutils.chgobject (fromdate); 날짜 t = dateUtils.chgobject (todate); 긴 fd = f.gettime (); 긴 td = t.gettime (); 반환 (TD-FD)/(24L * 60L * 60L * 1000L); } / ** * 연령 계산 * * @param 생일 * 날짜 날짜 * @param calcdate * 계산 될 날짜 요점 * @return * / public static int calcage (날짜, 날짜, 날짜 calcdate) {int cyear = dateUtils.getyearofdate (calcdate); int cmonth = dateUtils.getMonthofDate (calcdate); int cday = dateUtils.getDayofDate (calcdate); int byear = dateUtils.getyearofdate (생일); int bmonth = dateUtils.getMonthofDate (생일); int bday = dateUtils.getDayofDate (생일); if (cmonth> bmonth || (cmonth == bmonth && cday> bday)) {return cyear-byear; } else {return cyear-1-byear; }} / ** * ID 카드에서 생년월일을 가져옵니다 * * @param idno * id 카드 번호 * @return * * / public static string getBirthdayfromIdcard (String idno) {calendar cd = calendar.getInstance (); if (idno.length () == 15) {cd.set (calendar.year, integer.valueof ( "19" + idno.substring (6, 8)) .intvalue ()); cd.set (calendar.month, integer.valueof (idno.substring (8, 10)) .intvalue () -1); cd.set (calendar.day_of_month, integer.valueof (idno.substring (10, 12)). intvalue ()); } else if (idno.length () == 18) {cd.set (calendar.year, integer.valueof (idno.substring (6, 10)) .intvalue ()); cd.set (calendar.month, integer.valueof (idno.substring (10, 12)) .intvalue () -1); cd.set (calendar.day_of_month, integer.valueof (idno.substring (12, 14)). intvalue ()); } return dateUtils.dateToString (cd.getTime ()); } / *** 입력 날짜에 (+)를 증가 시키거나 빼거나 (-) 일일* @param 날짜* 입력 날짜* @param emblem* 증가 또는 감소 일수* / public static date addday (날짜 날짜, int iday) {calendar cd = calendar.getinstance (); CD. 세트 타임 (날짜); cd.add (calendar.day_of_month, iday); 반환 cd.gettime (); } / *** 입력 날짜에 추가 (+) 또는 suptract (-) 달 날짜* @param 날짜* 날짜 입력* @param image* 월 점수 증가 또는 감소* / public static date addmonth (날짜 날짜, int image) {calendar cd = calendar.getInstance (); CD. 세트 타임 (날짜); cd.add (calendar.month, image); 반환 cd.gettime (); } / ** * 입력 날짜에 추가 (+) 또는 뺄셈 (-) 연도 * @param 날짜 * 날짜 입력 * @param emblem * 셀 수치 * / public static date addyear (날짜 날짜, int iyear) {calendar cd = calendar.getinstance (); CD. 세트 타임 (날짜); cd.add (calendar.year, iyear); 반환 cd.gettime (); } / ** * 객체 유형 날짜 * @param 날짜 * @return * / public static date chgobject (객체 날짜) {if (date! = null && date instanceof date) {return (날짜) 날짜; } if (date! = null && date instancef) {return dateUtils.stringTodate ((string) 날짜); } return null; } public static long getagebybirthday (문자열 날짜) {날짜 생일 = StringTodate (날짜, "yyyy-mm-dd"); long sec = new date (). gettime () - 생일 .gettime (); 긴 나이 = sec/(1000*60*60*24)/365; 귀환 연령; }/** * @param args */public static void main (string [] args) {// string temp = dateUtil.dateToString (getLastdayOfmonth (new Date ()), // dateUtil.date_format_chinese); // String s = dateUtil.dateToString (dateUtil.addday (dateUtil.Addyear (new Date (), 1), -1)); long s = dateUtils.getDayByminusDate ( "2012-01-01", "2012-12-31"); System.err.println (s); }}위는이 기사의 모든 내용입니다. 모든 사람의 학습에 도움이되기를 바랍니다. 모든 사람이 wulin.com을 더 지원하기를 바랍니다.