이 기사에서는 참조를 위해 전체 Java 날짜 및 타임 스탬프 변환을 공유합니다. 특정 내용은 다음과 같습니다
패키지 com.crm.util; import java.math.bigdecimal; import java.text.decimalformat; import java.text.parseexception; import java.text.simpledateformat; java.util.calendar 가져 오기; import java.util.date; / ** * @author dingjiacheng */ public class dateformatutil {/ ** * timestamp to ditat * @param ms * @return */ public static date transford (정수 ms) {if (ms == null) {ms = 0; } long msl = (long) ms*1000; simpledateformat sdf = new simpledateformat ( "yyyy-mm-dd hh : mm : ss"); 날짜 온도 = null; if (ms! = null) {try {String str = sdf.format (msl); temp = sdf.parse (str); } catch (parseException e) {e.printstacktrace (); }} 반환 온도; } / ** * 오후 9시 30 분에 타임 스탬프를 가져옵니다 * * @return * / public static int gettimes (int day, int hour, int minute) {calendar cal = calendar.getInstance (); cal.add (calendar.date, day); cal.set (calendar.hour_of_day, 시간); cal.set (calendar.second, 0); cal.set (calendar.minute, minute); cal.set (calendar.millisecond, 0); return (int) (cal.gettimeinmillis () / 1000); } / ** * 현재 시간을 풀 시간을 얻습니다 * * @return * / public static int getIntegraltime () {Calendar cal = calendar.getInstance (); cal.add (calendar.hour_of_day, 1); cal.set (calendar.second, 0); cal.set (calendar.minute, 0); cal.set (calendar.millisecond, 0); return (int) (cal.gettimeinmillis () / 1000); } public static int getIntegralTimeend () {Calendar cal = calendar.getInstance (); cal.set (calendar.hour_of_day, 24); cal.set (calendar.second, 0); cal.set (calendar.minute, 0); cal.set (calendar.millisecond, 0); return (int) (cal.gettimeinmillis () / 1000); } / ** * 현재 timestamp * @param ms * @return * / public static date transfordate3 (정수 ms) {if (ms == null) {ms = 0; } long msl = (long) ms*1000; simpledateformat sdf = 새로운 simpledateformat ( "yyyy-mm-dd hh : mm"); 날짜 온도 = null; if (ms! = null) {try {String str = sdf.format (msl); temp = sdf.parse (str); } catch (parseException e) {e.printstacktrace (); }} 반환 온도; } / ** * 현재 timestamp * @param ms * @return * / public static date transford (long ms) {if (ms == null) {ms = (long) 0; } long msl = (long) ms*1000; simpledateformat sdf = new simpledateformat ( "yyyy-mm-dd hh : mm : ss"); 날짜 온도 = null; if (ms! = null) {try {String str = sdf.format (msl); temp = sdf.parse (str); } catch (parseException e) {e.printstacktrace (); }} 반환 온도; } public static String transfordate1 (정수 ms) {String str = ""; if (ms! = null) {long msl = (long) ms*1000; simpledateformat sdf = new simpledateformat ( "yyyy-mm-dd hh : mm : ss"); if (ms! = null) {try {str = sdf.format (msl); } catch (예외 e) {e.printstacktrace (); }}} return str; } public static String transfordate2 (정수 ms) {String str = ""; if (ms! = null) {long msl = (long) ms*1000; simpledateformat sdf = new simpledateformat ( "yyyy-mm-dd"); if (ms! = null) {try {str = sdf.format (msl); } catch (예외 e) {e.printstacktrace (); }}} return str; } public static string transfordate4 (정수 ms) {String str = ""; if (ms! = null) {long msl = (long) ms*1000; simpledateformat sdf = new simpledateformat ( "yyyy.mm.dd"); if (ms! = null) {try {str = sdf.format (msl); } catch (예외 e) {e.printstacktrace (); }} return str; } public static string transfordate5 (정수 ms) {String str = ""; if (ms! = null) {long msl = (long) ms*1000; simpledateformat sdf = 새로운 simpledateformat ( "yyyy/mm/dd hh : mm : ss"); if (ms! = null) {try {str = sdf.format (msl); } catch (예외 e) {e.printstacktrace (); }} return str; } public static string transfordateinchinese (정수 MS) {String str = ""; if (ms! = null) {long msl = (long) ms*1000; simpledateformat sdf = 새로운 simpledateformat ( "yyyyy 년 mm 달 dd 날짜 hh : mm : ss"); if (ms! = null) {try {str = sdf.format (msl); } catch (예외 e) {e.printstacktrace (); }} return str; } / ** * 타임 스탬프 날짜 * @param 날짜 * @return * / public static integer transformIllisecond (날짜 날짜) {if (date == null) return null; return (int) (date.gettime ()/1000); }/*** 현재 timestamp* @return*/public static integer currentTimestamp () {return (int) (System.CurrentTimeMillis ()/1000); } / ** * timeStamp에서 날짜 문자열 * @param dateStres * @return * / public static integer transformIllisecond (String DateSt) {date date = dateformatutil.formatdate (datest); 반환 날짜 == null? null : dateformatutil.transformillisecond (날짜); } / ** * 날짜 문자열 timestamp * @param dateStres * @return * / public static inger transformIllisecond (문자열 datest, 문자열 형식) {date date = dateformatutil.formatdate (datest, format); 반환 날짜 == null? null : dateformatutil.transformillisecond (날짜); } / ** * "yyyy-mm-dd" * @return * / public static integer transformillisecondbytim (String datest, String tim)과 같은 timestamp timestamp * @param dateStres * @param 형식으로 날짜 문자열 {simplededateformat sdf = new simpledateformat (tim); 날짜 날짜 = null; try {date = sdf.parse (dateSt); } catch (parseException e) {e.printstacktrace (); } 반환 날짜 == null? null : dateformatutil.transformillisecond (날짜); } / ** * 현재까지 문자열, 형식은 다음과 같습니다. "yyyy-mm-dd hh : mm : ss" * @param dateSt * @return * / public static date formatdate (string dateStr) {simplededateformat sdf = new SimpledateFormat ( "yyyy-mm-dd hh : mm : ss"); 날짜 결과 = null; try {result = sdf.parse (dateSt); } catch (parseException e) {e.printstacktrace (); } 반환 결과; } / ** * 현재까지의 문자열, 형식 : "yyyy-mm-dd hh : mm : ss" * @param dateStr * @return * / public static date formatdate (String Datest, String Format) {simplededateformat sdf = new SimpledateFormat (format); 날짜 결과 = null; try {result = sdf.parse (dateSt); } catch (parseException e) {e.printstacktrace (); } 반환 결과; } / ** * 문자열 날짜 * @param 날짜 * @return * / public static string formatdate (날짜 날짜) {simplededateformat sdf = new simpledateformat ( "yyyy-mm-dd hh : mm : ss"); 문자열 결과 = null; 결과 = sdf.format (날짜); 반환 결과; } / ** * 문자열 날짜 * @param 날짜 * @return * / public static string formatdate (날짜 날짜, 문자열 형식) {simpledateformat sdf = new SimpledateFormat (형식); 문자열 결과 = null; 결과 = sdf.format (날짜); 반환 결과; } / ** * 타임 스탬프 형식 출력 (httpl 템플릿의 경우) * * @param ms timestamp * @param 형식 형식 형식 * @return * / public static string transford (정수 MS, 문자열 형식) {String str = ""; if (ms! = null) {long msl = (long) ms*1000; simpledateformat sdf = 새로운 simpledateformat (형식); if (! ms.equals (0)) {try {str = sdf.format (msl); } catch (예외 e) {e.printstacktrace (); }}} return str; } / ** * bigdecimal 유형 번호의 정수 또는 소수점을 취하십시오 (httpl 템플릿의 경우) * * @param b value * @param mode mode 0 라운드 1 소수 부품 * @return * / public string splitbigdecimal (bigdecimal b, int mode) {decimalformat df = new decimalformat ( "0.00"); 문자열 s = df. format (b); if (mode == 0) {return s.split ( "//.") [0]; } else {return "."+s.split ( "//.") [1]; }} / ** * 두 날짜의 차이 (HTTP 템플릿) * @param ts1 timestamp 1 * @param ts2 timestamp 2 * @return * / public static int caculate2days (Integer TS1, Integer TS2) {날짜 firstDate = datateformatil.transforde (ts1); 날짜 seconddate = dateformatutil.transfordate (TS2); 캘린더 캘린더 = calendar.getInstance (); calendar.settime (FirstDate); int daynum1 = calendar.get (calendar.day_of_year); calendar.settime (SecondDate); int daynum2 = calendar.get (calendar.day_of_year); return math.abs (daynum1 -Daynum2); }/** * 휴대 전화의 중간 4 자리에 별표를 추가 * @param mobile * @return */public string mobileserect (String Mobile) {if (! stringUtils.isblank (mobile)) {int ty = mobile.length ()/2; mobile = mobile.substring (0, 2)+"****"+mobile.substring (+2, mobile.length ()); } 반환 모바일; } / ** * 이메일 주소에 별표를 추가 * * @param email * @return * / public String emailSerect (문자열 이메일) {if (! stringUtils.isblank (email))) {int longth.lend.lastindexof ( " @"); 이메일 = email.substring (0, 2)+"****"+email.substring (longth-2, email.length ()); } 반환 이메일; } / ** * bigdecimal 유형의 데이터 추가 * * @param bigdecimal source * @param bigdecimal target * @return * / public bigdecimal sumbigdicimal (bigdecimal source, bigdecimal target) {source = source.add (target); 리턴 소스; } / ** * bigdecimal 유형 데이터 추가 * * @param bigdecimal source * @param bigdecimal target * @return * / public bigdecimal sumbigdicimalanddouble (bigdecimal source, double target) {bigdecimal new_target = new bigdecimal (target); source = source.add (new_target); 리턴 소스; } / ** * bigdecimal 유형 데이터의 뺄셈 * * @param bigdecimal source * @param bigdecimal target * @return * / public bigdecimal subbigdicimal (bigdecimal source, bigdecimal target) {source = source.subtract (target); 리턴 소스; } / *** 들어오는 시간과 현재 시간 사이의 시차를 가져옵니다* @return* / public static long gettimediff (int timestamp) {date d1 = dateformatutil.transfordate (timestamp); 오늘 날짜 = 새 날짜 (); if (d1.gettime () <way.gettime ()) {return null; } return (d1.gettime ()-오늘 .gettime ())/1000; } / *** 특정 주의 첫날 날짜를 얻으십시오* @Param Week 0 이번 주 1 주 1 주 전-1 차 주* / public static string weekfirstday (int week) {calendar c1 = calendar.getInstance (); int dow = c1.get (calendar.day_of_week); c1.add (calendar.date, -dow-7*(주 1) -5); 문자열 d1 = new simpledateformat ( "yyyy-mm-dd"). 형식 (c1.gettime ()); 반환 d1+"00:00:00"; } / *** 현재 시간 + 1 년* / public static string addyear (int starttime) {date firstDate = dateFormatUtil.Transfordate (startTime); 캘린더 캘린더 = calendar.getInstance (); calendar.settime (FirstDate); calendar.add (calendar.year, 1); 문자열 d1 = new simpledateformat ( "yyyy-mm-dd hh : mm : ss"). 형식 (calendar.gettime ()); 반환 d1; } / ** * 특정 주의 마지막 날 날짜를 얻으십시오 * @Param Week * @return * / public static string weeklastday (int Week) {Calendar C1 = Calendar.getInstance (); int dow = c1.get (calendar.day_of_week); c1.add (calendar.date, -dow-7*(주 1) +1); 문자열 d1 = new simpledateformat ( "yyyy-mm-dd"). 형식 (c1.gettime ()); 반환 d1+"23:59:59"; } / *** 현재 시간과의 비교* @return* / public static boolean Greattannow (int timestamp) {date d1 = dateformatutil.transfordate (timestamp); 오늘 날짜 = 새 날짜 (); if (d1.gettime ()> = today.gettime ()) {return true; } false를 반환합니다. } / ** * hh : mm : ss 형식 시간은 1970-01-01 일의 타임 스탬프로 변환됩니다 (즉, 시간과 날짜 만 있으면 시간을 나타내기 위해 시간 스탬프를 사용해야합니다) * @author dingjiacheng * * / public static intfromtime (strancmillisecond) {1970-01-01- ""1970-01-01- " HH : MM : SS "); } / ** * HH : MM : SS 형식 시간 (날짜 제거) * @Author Dingjiacheng * / public static String transtotime (int time) {string s = new String (transfordate1 (time)); 문자열 ss [] = s.split ( ""); 반환 ss [1]; } public static int transtochuo (String datestring) {simpledateformat simpledateformat = new SimpledateFormat ( "yyyy-mm-dd"); int res = 0; try {date date = simpledateformat .parse (datestring); res = (int) date.gettime (); } catch (parseException e) {e.printstacktrace (); } return res; } public static void main (String [] args) {//system.out.println (getIntegralTimeend ()); System.out.println (TransfordIte2 (TransformIllisecond ( "2015-02-25 00:00:00"))); //system.out.println(transformillisecond("2016-01-25","yyyy-mm-dd ")); //system.out.println(transfordate1(transformillisecond("1970-01-01 00:00:00 ","yyyy-mm-dd hh : mm : ss "))); //system.out.println (currentTimestamp ()); //system.out.println (Transfordate (currentTimestamp ()); //system.out.println(new date ()); //system.out.println (dateUtils.getDate ()); System.out.println (transfromtime ( "00:00:01")); System.out.println (Transtotime (Transfromtime ( "15:01:13"))); }} 위는이 기사의 모든 내용입니다. 모든 사람의 학습에 도움이되기를 바랍니다. 모든 사람이 wulin.com을 더 지원하기를 바랍니다.