프로젝트에서 시간을 처리 해야하는 경우가 종종 있습니다. 다음은 앞으로 쉽게 사용하고 검토하기 위해 일반적으로 사용되는 작업입니다.
1. 문자열을 현재까지 변환하십시오
/*** 날짜로 문자열을 변환* @param datest 변환해야 할 날짜* @param dateformat 날짜 형식 형식 yyyy-mm-dd/yyyy-mm-dd hh : mm : mm : ss*/public static date todate (string dateStres, simpledateformat dateformat) 던지기를 던지기 날짜 = null; try {date = dateformat.parse (datest); } catch (parseException e) {logger.debug ( "문자열을 날짜로 변환하지 않으면 {}", dateStrest); } 반환 날짜; }2. 현재까지 타임 스탬프
/ ** * 날짜가 timestamp * @param 날짜 * @return */ public static string dateToTime (긴 시간, simpledateformat dateformat) parseexception {string data = null; try {dateformat.format (새 날짜 (시간*1000)); } catch (예외 e) {logger.debug ( "현재까지 오래 전환 실패, {}", 시간); } 반환 데이터; }3. 문자열로 날짜를 형식화하십시오
/ ** * 날짜는 문자열로 형식화됩니다. }
4. 지정된 날짜 전후에 날짜를 받으십시오. 10 초는 00:00:00입니다.
/ *** 지정된 날짜 전후에 날짜를 가져 오십시오* @param 날짜* @param num 양수 숫자는 후에, 그리고 음수는* @return yyyy-mm-dd 00:00:00*/ public static date getSpecificDate (날짜 날짜, in num) {calendar todaycal = calendar.getInstance (); Todaycal.settime (날짜); Calendar C = Calendar.getInstance (); C.SET (Todaycal.get (calendar.year), todaycal.get (calendar.month), todaycal.get (calendar.day_of_month) + num, 0, 0, 0); 반환 c.gettime (); }5. 지정된 날짜 전후 날짜를 받으십시오. 시간, 분 및 두 번째는 현재의 것입니다.
/ *** 지정된 날짜 전후에 날짜를 가져옵니다* @param 날짜* @param num 양수 숫자는 전이고 음수는* @return yyyy-mm-dd + 현재 시간, 분 및 두 번째*/ public static date getspecificdateandhhmmss (날짜, int num) {calendar c = calendar.getinstance (); C. 세트 타임 (날짜); int day = c.get (calendar.date); c.set (calendar.date, day -num); 반환 c.gettime (); }6. 시간 유형의 시간 문자열 변환 시간과 시간으로
/ ** * 시간 유형의 시간 문자열을 시간과 시간으로 변환 * HH-MM-SS- >> hh-mm * @param time * @return */ public static string timetohhmm (문자열 시간) {return time.substring (0, time.length () -3); }7. 특정 날짜의 시간과 분을 얻으십시오
/ ** * 날짜의 시간과 분 * @param 날짜 * @return hh-mm */ public static string gethm (날짜 날짜) {calendar ca = calendar.getInstance (); ca.settime (날짜); 정수 시간 = ca.get (calendar.hour_of_day); // 시간 정수 분 = ca.get (calendar.minute); // minute string rs_hour = hour.tostring (); 문자열 rs_minute = minute.toString (); if (rs_hour.length () == 1) {rs_hour = "0" + 시간; } if (rs_minute.length () == 1) {rs_minute = "0" + minute; } return rs_hour + ":" + rs_minute; }8. 타임 스트링 타입 시간 ->> 0에서 시작하는 초 수
/ ** * 시간 문자열 시간 유형->> 제로 포인트에서 초의 수 * @param time hh-mm / hh-mm-ss * @return * / public static integer timetoseconds (문자열 시간) {string [] timesplit = null; int 시간 = 0, 분 = 0, 초 = 0; try {timesplit = time.split ( ":"); if (timesplit.length == 2) {시간 = integer.valueof (timesplit [0])*60*60; 분 = integer.valueof (timesplit [1])*60; } else if (timesplit.length == 3) {시간 = integer.valueof (timesplit [0])*60*60; 분 = integer.valueof (timesplit [1])*60; seconds = integer.valueof (timesplit [2]); } else {logger.debug ( "시간 변환 실패, {}", 시간); }} catch (예외 e) {logger.debug ( "시간 변환 실패, {}", 시간); e 던지기; } 반환 시간 + 분 + 초; }9. 제로 포인트의 시작시 초의 수->> HH-MM-SS
/** * 제로 포인트의 시작시 초의 수->> HH-MM-SS * @Param Durationseconds * @return * /public static String getDuration (int durationseconds) {int 시간 = 듀서 컨드 /(60 * 60); int leftseconds = durationseconds % (60*60); int minutes = leftseconds / 60; int 초 = leftseconds % 60; StringBuffer sbuffer = new StringBuffer (); sbuffer.append (addzeroprefix (시간)); sbuffer.append ( ":"); sbuffer.append (addzeroprefix (minutes)); sbuffer.append ( ":"); sbuffer.append (addzeroprefix (seconds)); 반환 sbuffer.toString (); } public static string addzeroprefix (int number) {if (number <10) return "0"+number; 그렇지 않으면 ""+번호; }10. 두 날짜 사이의 초 수 비교
/ ** * 두 날짜 사이의 초 비교 * @param startdate * @param enddate * @return */ public static int gettimeseconds (날짜 시작 date, date enddate) {long a = enddate.gettime (); long b = startDate.getTime (); return (int) ((a -b) / 1000); }11. 두 기간 사이에 교차점이 있는지 결정
/ ** * 두 날짜 사이의 초 비교 * @param startdate * @param enddate * @return */ public static int gettimeseconds (날짜 시작 date, date enddate) {long a = enddate.gettime (); long b = startDate.getTime (); return (int) ((a -b) / 1000); }12. 지정된 주일을 받으십시오 (1-7은 각각 월요일부터 일요일까지)
/ ** * 지정된 날짜는 요일입니다 (1-7은 각각 월요일 ~ 일요일을 나타냅니다) * @return */ public static int dayofweek (날짜 날짜) {calendar now = calendar.getInstance (); now.settime (날짜); 부울 isfirstday = (now.getfirstdayofweek () == calendar.sunday); int weekday = now.get (calendar.day_of_week); if (isfirstday) {Weekday = Weepday -1; if (Weekday == 0) {Weekday = 7; }} 주중 반환; } 위는이 기사의 모든 내용입니다. 이 기사의 내용이 모든 사람의 연구 나 업무에 도움이되기를 바랍니다. 또한 wulin.com을 더 지원하기를 바랍니다!