この記事では、参照のためのJava日付とタイムスタンプ変換の完全なコレクションを共有しています。特定のコンテンツは次のとおりです
パッケージcom.crm.util; java.math.bigdecimalをインポートします。 java.text.decimalformatをインポートします。 java.text.parseexceptionをインポートします。 java.text.simpledateformatをインポートします。 java.util.calendarをインポートします。 Import Java.util.date; / ** * @author dingjiacheng * */ public class dite formatutil {/ ** * Timestamp to to tute * @param ms * @return */ public static date transfordate(integer 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(); }} return temp; } / ** *午後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、hour); 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(){calendary 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 TO TUTE * @PARAM MS * @return * / public static Date transfordate3(integer MS){if(ms == null){ms = 0; } long msl =(long)ms*1000; SimpleDateFormat sdf = new 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(); }} return temp; } / ** * TIMESTAMP TO TUTE * @PARAM MS * @return * / public static Date transfordate(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(); }} return temp; } public static string transfordate1(integer 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(Exception e){e.printstacktrace(); }}} return str; } public static string transfordate2(integer 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(Exception e){e.printstacktrace(); }}} return str; } public static string transfordate4(integer 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(Exception e){e.printstacktrace(); }} return str; } public static string transfordate5(integer 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(Exception e){e.printstacktrace(); }} return str; } public static string transfordateinchinese(integer ms){string str = ""; if(ms!= null){long msl =(long)ms*1000; SimpleDateFormat SDF = new SimpledateFormat( "yyyyy year mm month dd date hh:mm:ss"); if(ms!= null){try {str = sdf.format(msl); } catch(Exception e){e.printstacktrace(); }} return str; } / ** * timestampへの日付 * @param date * @return * / public static integer transformillisecond(date date){if(date == null)return null; return(int)(date.getTime()/1000); }/***現在のタイムスタンプを取得* @return*/public static integer currenttimestamp(){return(int)(system.currenttimemillis()/1000); } / ** * timestampへの日付文字列 * @param datestr * @return * / public static integer transformillisecond(string datestr){date date = dateformatutil.formatdate(datestr); return date == null? null:dateformatutil.transformillisecond(date); } / ** * date string to timestamp * @param datestr * @return * / public static integer transformillisecond(string datestr、string format){date = dateformatutil.formatdate(datestr、format); return date == null? null:dateformatutil.transformillisecond(date); } / ** * date string to timestamp * @param datestr * @param形式「yyyy-mm-dd」 * @return * / public static integer transferlilisecondbytim(string datestr、string tim){simpledateformat sdf = new simpledateformat(tim);日付date = null; try {date = sdf.parse(datestr); } catch(parseexception e){e.printstacktrace(); } return date == null? null:dateformatutil.transformillisecond(date); } / ** * string to to us、format is: "yyyy-mm-dd hh:mm:mm:ss" * @param datestr * @return * / public static formatdate(string datestr){simpledateformat sdf = new simpledateformat( "yyyy-mm-dd hh:mm:ss");日付結果= null; try {result = sdf.parse(datestr); } catch(parseexception e){e.printstacktrace(); } return result; } / ** * string to to nut、format: "yyyy-mm-dd hh:mm:mm:mm:ss" * @param datestr * / public static date formatdate(string datestr、string format){simpledateformat sdf = new simpledateformat(format);日付結果= null; try {result = sdf.parse(datestr); } catch(parseexception e){e.printstacktrace(); } return result; } / ** * stringへの日付 * @param date * @return * / public static string formatdate(date date){simpledateformat sdf = new simpledateFormat( "yyyy-mm-dd hh:mm:ss");文字列結果= null; result = sdf.format(date);返品結果; } / ** * string to string * @param date * @return * / public static string formatdate(date date、string format){simpledateformat sdf = new simpledateFormat(format);文字列結果= null; result = sdf.format(date);返品結果; } / ** *タイムスタンプ形式の出力(httplテンプレート用) * * @param ms timestamp * @param形式形式 * @return * / public static string transfordate(integer ms、string format){string str = ""; if(ms!= null){long msl =(long)ms*1000; SimpleDateFormat sdf = new SimpledateFormat(format); if(!ms.equals(0)){try {str = sdf.format(msl); } catch(Exception e){e.printstacktrace(); }}} return str; } / ** * Bigdecimal Type Number(httplテンプレートの場合)の整数または小数の部分を取得します * * @param b値 * @paramモードMode 0ラウンド1小数パート * @return * / public static 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]; }} / ** * 2つの日付(HTTPテンプレートの場合)の差が計算されます(HTTPテンプレートの場合) * * @param TS1タイムスタンプ1 * @param TS2タイムスタンプ2 * @return * / public static intic caculate2days(integer ts1、integer ts2){date firstddate = dateformatil.fordate(ts1); Date 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)){inter = mobile.length()/2; mobile = mobile.substring(0、bety-2)+"****"+mobile.substring(+2、mobile.length()); }モバイルを返します。 } / ** * ASTERISKを電子メールアドレスに追加 * * @Param Email * @return * / public String EmailSeRect(String Email){if(!stringutils.isblank(email)){int length = email.lastindexof( " @"); email = email.substring(0、2)+"****"+email.substring(length-2、email.length()); }メールを返します。 } / ** * Bigdecimal Type * * @Param BigDecimal Sourceのデータを合計 * @Param BigDecimal Target * / Public BigDecimal Sumbigdicimal(BigDecimal Source、BigDecimal Target){Source = source.Add(ターゲット);返品ソース。 } / ** * Bigdecimal Type Dataの追加 * * @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 Type Dataの減算 * * @param Bigdecimal Source * @param Bigdecimal Target * / public Bigdecimal subbigdicimal(Bigdecimal Source、Bigdecimal Target){Source = Source.Subtract(ターゲット);返品ソース。 } / ***着信時間と現在の時間の時間差を取得* @return* / public static long gettimediff(int timestamp){date d1 = dateformatutil.transfordate(timestamp);今日の日付= new Date(); if(d1.getTime()<today.getTime()){return null; } return(d1.getTime() - TODAY.GETTIME())/1000; } / ***特定の週の最初の日付を取得* int dow = c1.get(calendar.day_of_week); c1.add(calendar.date、-dow-7*(week-1)-5);文字列d1 = new SimpledateFormat( "yyyy-mm-dd")。format(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")。format(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*(week-1)+1);文字列d1 = new SimpledateFormat( "yyyy-mm-dd")。format(c1.gettime()); D1+"23:59:59"を返します。 } / ***現在の時刻との比較* @return* / public static boolean GreaterThannow(int Timestamp){date d1 = dateformatutil.transfordate(timestamp);今日の日付= new Date(); if(d1.getTime()> = today.getTime()){return true; } falseを返します。 } / ** * HH:MM:SSフォーマット時間は1970-01-01日のタイムスタンプに変換されます(つまり、時間と日付のみがある場合、タイムスタンプを使用して時間を表すために使用する必要があります) * @author dingjiacheng * * / public static int transfromtime(String time){transforliseCond( "1970-01-01 HH:mm:ss "); } / ** *タイムスタンプはHHに変換されます:MM:SSフォーマット時間(日付削除) * @Author Dingjiacheng * * / public static string transtotime(int time){string s = new string(transfordate1(time)); string 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(); } RESを返します。 } public static void main(string [] args){//system.out.println(getintegralTimeEnd()); System.out.println(transfordate2(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をもっとサポートすることを願っています。