이 기사에서는 참조에 대한 시간 변환을위한 DateUtils 도구 클래스를 공유합니다. 특정 내용은 다음과 같습니다
import java.sql.timestamp; import java.text.simpledateformat; import java.util.calendar; import java.util.date; import java.util.hashmap; import java.util.map; import java.util.map; import net.sf.json.jsonob;/** and thend classe* and theng classe* and theng classe; wul * 2015-12-31 */public class dateUtil {public static final String date_normal_format = "yyyy-mm-dd"; public static final String dateTime_normal_format = "yyyy-mm-dd hh : mm : ss"; 공개 정적 최종 문자열 date_compact_format = "yyyymmdd"; public static final String dateTime_compact_format = "yyymmddhhmmss"; 공개 정적 최종 문자열 ym_normal_format = "yyyy-mm"; 공개 정적 최종 문자열 ym_compact_format = "yyyymm"; / ** * string to timestamp * @param dateStres * @return * @author wul * 2016-1-17 */ public static timestamp StringTotimestamp (String DateStrim) {try {if (datest.length () <= 10) {dateStr += "00:00:00"; } return timestamp.valueof (dateSt); } catch (예외 e) {e.printstacktrace (); 널 리턴; }} / ** * 날짜에 대한 문자열 * @param dateStr * @param format * @return * @author wul * 2016-1-17 * / public static date StringTodate (String DateSt, String format) {if (dateSt == null || "".Equals (dateSt)) {return null; } 날짜 = null; // 형식의 형식은 날짜 형식의 형식과 일치해야합니다. simpledateformat sdf = new SimpledateFormat (format); try {date = sdf.parse (dateSt); } catch (예외 e) {e.printstacktrace (); } 반환 날짜; } / ** * 문자열 날짜 * @param date * @param format * @return * @author wul * 2016-1-17 * / public static string dateToString (날짜, 문자열 형식) {simplededateformat sdf = new simpledateformat (format); 문자열 currentDate = sdf.format (날짜); Current Date를 반환합니다. } / ** * 타임 스탬프 날짜 * @param 날짜 * @return * @author wul * 2016-1-17 * / public static timestamp dateToTimestamp (날짜 날짜) {timestamp ts = new timestamp (date.gettime ()); 반환 ts; } / ** * string * @param ts * @return * @author wul * 2016-1-17 * / public static string timestamptostring (timestamp ts) {String tsstr = null; simpledateformat sdf = new simpledateformat (dateTime_normal_format); try {tsstr = sdf.format (ts); } catch (예외 e) {e.printstacktrace (); } return tsstr; } / ** * timestamp to dite * @param ts * @return * @author wul * 2016-1-17 * / public static date timestamptodate (timestamp ts) {return ts; } / ** * 현재 시간과 형식을 가져옵니다. 문자열 currentDate = sdf.format (new date ()); Current Date를 반환합니다. } / ** * 현재 시간과 형식을 가져옵니다. 문자열 currentDate = sdf.format (new date ()); Current Date를 반환합니다. } / ** * 현재 시간과 형식을 가져옵니다. 문자열 currentDate = sdf.format (new date ()); Current Date를 반환합니다. } / ** * 현재 시간과 형식을 가져옵니다. 문자열 currentDate = sdf.format (new date ()); Current Date를 반환합니다. } / ** * 형식 20101202 2010-12-02까지의 시간 * * @param datestring time format : yyyymmdd * @return * / public static string getDateCompactTonormal (String Datestring) {StringBuilder sb = new StringBuilder (); sb.append (datestring.substring (0,4)). Append ( "-"). Append (datestring.subsequence (4, 6)). Append ( "-"). Append (datestring.substring (6, 8)); 반환 sb.toString (); } / ** * 형식 20101202101423 2010-12-02까지의 시간 10:14:23 * * @param datestring time 형식 : yyyyymmddhhmmss * @return * / public static string getDateMecompacttonormal (string datestring) {stringbuilder sb = new stringbuilder (); sb.append (datestring.substring (0,4)). Append ( "-"). Append (datestring.subsequence (4, 6)). Append ( "-"). Append (datestring.substring (6, 8)) .append ( "") .append (datestring.substring (8, 10)). 반환 sb.toString (); } / ** * 인터페이스의 시간 입력을 간헐적 인 시간 문자열로 변환 * 2010-12-02 10:14:23 20101202101423 * @param datenormalstr string * @return String * / public static string getCompactString (String DatenOmalStr) {StringBuffer ret = new StringBuffer (); try {ret.append (datenormalstr.substring (0, 4)); ret.append (datenormalstr.substring (5, 7)); ret.append (datenormalstr.substring (8, 10)); ret.append (datenormalstr.substring (11, 13)); ret.append (datenormalstr.substring (14, 16)); ret.append (datenormalstr.substring (17, 19)); } catch (예외 예) {// 문자열이 충분히 길지 않으면 이전 부분을 반환} return ret.toString (); } / ** * 20101202 (101423) 시간 형식 * @param datestring time format : yyyymmdd (hhmmss) * @return * / public static string getyear (String Datestring) {return datestring.substring (0,4); } / ** * 20101202 (101423) 시간 형식 * @param datestring time format : yyyymmdd (hhmmss) * @return * / public static string getmonth (String datestring) {return datestring.substring (4,6); } / ** * 20101202 시간 형식으로 날짜를 가져옵니다. 시간 형식 * @param datestring time format : yyyymmdd * @return * / public static string getDaynotime (String Datestring) {return datestring.substring (6); } / ** * 현재 날짜 전 날짜를 가져 와서 일의 수 * @param numval * @param dateformat * @return * @author wul * 2016-1-17 * / public static string getBeforedatePlusday (int numval, string dateformat) {Calendar Calendar = calendar.getInstance (); Long CurrentTimeMillis = calendar.getTimeInmillis (); 긴 시간 킬리스 = 60 * 60 * 1000; 긴 dval = numval * 24 * Hourinmillis; simpledateformat sdf = new simpledateformat (dateformat); 문자열 currentDate = sdf.format (currentTimEmillis -dval); Current Date를 반환합니다. } / ** * 현재 날짜 전 날짜를 가져 와서 일의 수 * @param numval * @param dateformat * @return * @author wul * 2016-1-17 * / public static string getAfterDatePlusday (int numval, string dateformat) {Calendar Calendar = calendar.getInstance (); Long CurrentTimeMillis = calendar.getTimeInmillis (); 긴 시간 킬리스 = 60 * 60 * 1000; 긴 dval = numval * 24 * Hourinmillis; simpledateformat sdf = new simpledateformat (dateformat); 문자열 currentDate = sdf.format (currentTimeMillis + dval); Current Date를 반환합니다. } / ** * 현재 날짜 전 날짜를 가져 와서 시간별로 전달하십시오 * @param numval * @param dateformat * @ETurn * @Author wul * 2016-1-17 * / public static string getBeforedatePlushour (int numval, string dateformat) {Calendar Calendar = calendar.getInstance (); Long CurrentTimeMillis = calendar.getTimeInmillis (); 긴 시간 킬리스 = 60 * 60 * 1000; 긴 dval = numval * Hourinmillis; simpledateformat sdf = new simpledateformat (dateformat); 문자열 currentDate = sdf.format (currentTimEmillis -dval); Current Date를 반환합니다. } / ** * 현재 날짜 전 날짜를 가져 와서 시간별로 전달하십시오 * @param numval * @param dateformat * @ETurn * @Author wul * 2016-1-17 * / public static string getAfterDatePlushour (int numval, String DateFormat) {Calendar Calendar = calendar.getInstance (); Long CurrentTimeMillis = calendar.getTimeInmillis (); 긴 시간 킬리스 = 60 * 60 * 1000; 긴 dval = numval * Hourinmillis; simpledateformat sdf = new simpledateformat (dateformat); 문자열 currentDate = sdf.format (currentTimeMillis + dval); Current Date를 반환합니다. } / ** * 두 날짜 사이에 다른 날의 수 * @param barkindate * @param enddate * @eTurn * @Author wul * 2016-1-18 * / public static int daysbetween (날짜 시작, 날짜 종료) {calendar cal = calendar.getInstance (); cal.settime (시작); 긴 타임 1 = cal.getTimeInmillis (); cal.settime (enddate); 긴 타임 2 = cal.gettimeinmillis (); 긴 사이의 _days = (time2 -time1) / (1000 * 3600 * 24); return integer.parseint (string.valueof (사이)); } / ** * 특정 달의 일수를 얻습니다 * @param year * @param month * @return * @author wul * 2016-1-18 * / public static int getmonthdays (int year, int month) {calendar cal = calendar.getinstance (); cal.set (calendar.year, 연도); cal.set (calendar.month, 월 -1); Cal.getActualMaximum (Calendar.Date)을 반환합니다. } / ** * 연도를 추가 또는 빼기 * @param 날짜 * @param plustime * @return * @author wul * 2016-1-18 * / public static date getDatePlusyear (날짜 날짜, int plustime) {calendar cal = calendar.getinstance (); cal.settime (날짜); cal.add (calendar.year, plustime); 날짜 d = cal.gettime (); 반환 d; } / ** * 시간을 추가하거나 빼기 * @param 날짜 * @param plustime * @return * @author wul * 2016-1-18 * / public static date getDatePlusmonth (날짜 날짜, int plustime) {calendar cal = calendar.getinstance (); cal.settime (날짜); cal.add (calendar.month, plustime); 날짜 d = cal.gettime (); 반환 d; } / ** * 일일 수를 추가하고 빼기 * @param 날짜 * @param plustime * @ETurn * @Author wul * 2016-1-18 * / public static date getDatePlusday (날짜 날짜, int plustime) {Calendar cal = calendar.getInstance (); cal.settime (날짜); cal.add (calendar.date, plustime); 날짜 d = cal.gettime (); 반환 d; } / ** * 시간 추가 또는 빼기 * @param date * @param plustime * @return * @author wul * 2016-1-18 * / public static date getDatePlushour (날짜 날짜, int plustime) {calendar cal = calendar.getInstance (); cal.settime (날짜); cal.add (calendar.hour, plustime); 날짜 d = cal.gettime (); 반환 d; } / ** * 시간 추가 또는 빼기 * @param 날짜 * @param plustime * @return * @author wul * 2016-1-18 * / public static date getDatePlusminute (날짜 날짜, int plustime) {calendar cal = calendar.getInstance (); cal.settime (날짜); cal.add (calendar.minute, plustime); 날짜 d = cal.gettime (); 반환 d; } / ** * 시간을 추가하고 빼기 * @param 날짜 * @param plustime * @return * @author wul * 2016-1-18 * / public static date getDatePlussecond (날짜 날짜, int plustime) {Calendar cal = calendar.getinstance (); cal.settime (날짜); cal.add (calendar.second, plustime); 날짜 d = cal.gettime (); 반환 d; } / ** * 현재 연도를 반환 * @return * @author wul * 2016-1-18 * / public static int getCurrentyEar () {calendar calendar = calendar.getInstance (); return calendar.get (1); } / ** * 현재 달을 반환 * @return * @author wul * 2016-1-18 * / public static int getCurrentMonth () {calendar calendar = calendar.getInstance (); return calendar.get (2) + 1; } / ** * 현재 하루를 반환 * @return * @author wul * 2016-1-18 * / public static int getCurrentDay () {Calendar Calendar = calendar.getInstance (); return calendar.get (5); } / ** * 현재 시간을 반환 * @return * @author wul * 2016-1-18 * / public static int getCurrenthour () {calendar calendar = calendar.getInstance (); return calendar.get (11); } / ** * 현재 분을 반환 * @return * @author wul * 2016-1-18 * / public static int getCurrentMinute () {Calendar Calendar = calendar.getInstance (); return calendar.get (12); } / ** * 현재 두 번째 * @return * @author wul * 2016-1-18 * / public static int getCurrentSecond () {Calendar Calendar = Calendar.getInstance (); return calendar.get (13); } / ** * 현재 연도를 반환 * @return * @author wul * 2016-1-18 * / public static int getyear (날짜 날짜) {calendar calendar = calendar.getInstance (); calendar.settime (날짜); return calendar.get (1); } / ** * 현재 달을 반환 * @return * @author wul * 2016-1-18 * / public static int getmonth (날짜 날짜) {calendar calendar = calendar.getInstance (); calendar.settime (날짜); return calendar.get (2) + 1; } / ** * 현재 날을 반환 * @return * @author wul * 2016-1-18 * / public static int getday (날짜 날짜) {Calendar Calendar = calendar.getInstance (); calendar.settime (날짜); return calendar.get (5); } / ** * 현재 시간을 반환 * @return * @author wul * 2016-1-18 * / public static int gethour (날짜 날짜) {calendar calendar = calendar.getInstance (); calendar.settime (날짜); return calendar.get (11); } / ** * 현재 분을 반환 * @return * @author wul * 2016-1-18 * / public static int getminute (날짜 날짜) {Calendar Calendar = calendar.getInstance (); calendar.settime (날짜); return calendar.get (12); } / ** * 현재 두 번째 * @return * @author wul * 2016-1-18 * / public static int getsecond (날짜 날짜) {Calendar Calendar = calendar.getInstance (); calendar.settime (날짜); return calendar.get (13); } public static void main (String [] args) {System.out.println (dateUtil.dateToString (new java.sql.date (system.currenttimeMillis ()), dateUtil.date_normal_format); map <string, object> map = new Hashmap <String, Object> (); map.put ( "날짜", 새 날짜 ()); 문자열 json = jsonobject.fromobject (map) .toString (); System.out.println (JSON); }}위는이 기사의 모든 내용입니다. 모든 사람의 학습에 도움이되기를 바랍니다. 모든 사람이 wulin.com을 더 지원하기를 바랍니다.