多くの場合、プロジェクトで時間を処理する必要があります。以下は、将来的に簡単に使用してレビューを行うために一般的に使用される操作です。
1。文字列を現在まで変換します
/***文字列を日付に変換* @param dateStr変換する必要がある日付* @param dateformat format yyyy-mm-dd/yyyy-mm-dd hh:mm:ss*/public static date todate(string datesestr、simpledateformat dateformat)スローはparseexception {date = null; try {date = dateformat.parse(datestr); } catch(parseexception e){logger.debug( "文字列を{}"、datestr); }返品日; }2。これまでのタイムスタンプ
/ ** * TIMESTAMP TOUST * @PARAM DATE * @RETURN */ public Static String DateToTime(long time、simpledateformat dateformat)throws parseexception {string data = null; try {dateformat.format(new Date(Time*1000)); } catch(Exception e){logger.debug( "長期にわたって変換できない、{}"、time); }データを返します。 }3。文字列に日付をフォーマットします
/ ** *日付は文字列にフォーマットされます}
4。指定された日付の前後に日付を取得し、10秒は00:00:00です
/ ***指定された日付の日付を取得しますTodayCal.Settime(日付);カレンダーc = charend.getInstance(); c.set(TodayCal.get(Calendar.year)、TodayCal.get(Calendar.Month)、TodayCal.get(Calendar.day_of_month) + num、0、0、0); c.getTimeを返します(); }
5。指定された日付の前後に日付を取得します。時間、分、2番目は現在の日です
/ ***指定された日付の日付を取得しますc.settime(日付); 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(String Time){return time.substring(0、time.length()-3); }7.特定の日付の時間と分を取得する
/ ** *日付の時間と議事録 * @param date * @return hh-mm */ public static string gethm(date date){calendar ca = calendar.getinstance(); ca.settime(date); integer hour = ca.get(calendar.hour_of_day); // hour hour integer minute = ca.get(calendar.minute); // minute string rs_hour = hour.toString(); string rs_minute = minute.toString(); if(rs_hour.length()== 1){rs_hour = "0" + hour; } if(rs_minute.length()== 1){rs_minute = "0" + minute; } return rs_hour + ":" + rs_minute; }8。タイプ時間の時間文字列 - >>ゼロから始まる秒数
/ ** *タイムタイムタイプ - >>ゼロポイントでの秒数 * @Param Time HH-MM / HH-MM-SS * @return * / public Static Integer Timetoseconds(String Time){String [] TimeSplit = null; int時間= 0、分= 0、秒= 0; try {timeSplit = time.split( ":"); if(timesplit.length == 2){hours = integer.valueof(timesplit [0])*60*60;分= integer.valueof(timesplit [1])*60; } else if(timesplit.length == 3){hours = integer.valueof(timesplit [0])*60*60;分= integer.valueof(timesplit [1])*60;秒= integer.valueof(timesplit [2]); } else {logger.debug( "時間の変換に失敗、{}"、time); }} catch(Exception e){logger.debug( "時間を変換しない、{}"、time); eを投げる; }時間 +分秒 +秒を返します。 }9。ゼロポイントの開始時の秒数 - >> HH-MM-SS
/** *ゼロポイントの開始時の秒数 - >> hh-mm-ss * @param durationseconds * @return * /public static string getDuration(int durationseconds){int hours = durationseconds /(60 * 60); int leftseconds = durationseconds%(60*60); int minutes = leftseconds / 60; int秒=残りの秒%60; stringbuffer sbuffer = new StringBuffer(); sbuffer.append(addzeroprefix(hours)); sbuffer.append( ":"); sbuffer.append(addzeroprefix(minutes)); sbuffer.append( ":"); sbuffer.append(addzeroprefix(秒)); return sbuffer.tostring(); } public static string addzeroprefix(int number){if(number <10)return "0"+number;それ以外の場合は、 ""+numberを返します。 }10。2つの日付間の秒数の比較
/ ** * 2つの日付間の秒数の比較 * @param startdate * @param enddate * @return */ public static int gettimeseconds(date startdate、date enddate){long a = enddate.gettime(); long b = startdate.getTime(); return(int)((a -b) / 1000); }11。2つの期間の間に交差があるかどうかを判断します
/ ** * 2つの日付間の秒数の比較 * @param startdate * @param enddate * @return */ public static int gettimeseconds(date startdate、date enddate){long a = enddate.gettime(); long b = startdate.getTime(); return(int)((a -b) / 1000); }12。その週の指定日を取得します(1-7はそれぞれ月曜日から日曜日を表します)
/ ** *指定された日付を取得します(1-7は月曜日から日曜日を表す1-7) now.settime(date); boolean isfirstday =(now.getFirstdayofweek()== Calendar.sunday); int feekday = now.get(calendar.day_of_week); if(isfirstday){feekday = weekday -1; if(weekday == 0){weekday = 7; }}平日を返します。 }上記はこの記事のすべての内容です。この記事の内容が、すべての人の勉強や仕事に役立つことを願っています。また、wulin.comをもっとサポートしたいと思っています!