この記事では、リファレンスのための時間変換のためにDateUtilsツールクラスを共有しています。特定のコンテンツは次のとおりです
java.sql.timestampをインポート; java.text.simpledateformat;インポートjava.util.calendar; import java.util.date; import java.util.map; import java.util.map; import java.util.map; Import net.sf.json.jsonob***************************************************************************************************************************のインポート。 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"; public static final string date_compact_format = "yyyymmdd"; public static final string dateTime_compact_format = "yyymmddhhmmss"; public static final string ym_normal_format = "yyyy-mm"; public static final string ym_compact_format = "yyyymm"; / ** STRING TO TIMESTAMP * @PARAM DATESTR * @RETURN * @Author WUL * 2016-1-17 */ public Static Timestamp StringTotimestamp(String DateStram) } return timestamp.valueof(datestr); } catch(Exception e){e.printstacktrace(); nullを返します。 }} / ** * string to date * @param datestr * @param format * @return * @author wul * 2016-1-17 * / public static date stringtodate(string datestr、string format){if(datestr == null || "" .equals(datestr){return null; } date date = null; //形式の形式は、date string simpledateformat sdf = new SimpledateFormat(Format)の形式と一致する必要があることに注意してください。 try {date = sdf.parse(datestr); } catch(Exception e){e.printstacktrace(); }返品日; } / ** * stringへの日付 * @param date * @param format * @return * @author wul * / public static string datetString(日付、文字列形式){simpledateformat sdf = new simpledateformat(format);文字列currentDate = sdf.format(date); currentdateを返します。 } / ** * timestampへの日付 * @param date * @return * @author wul * 2016-1-17 * / public static Timestamp DateTotimestamp(日付){Timestamp TS = new Timestamp(date.getTime()); tsを返します。 } / ** * timestamp to 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(Exception e){e.printstacktrace(); } tsstrを返します。 } / ** * TIMESTAMP TOTUTE * @PARAM TS * @RETURN * @AUTHOR WUL * 2016-1-17 * / public Static Date TimestAmptodate(Timestamp TS){return ts; } / ** *現在の時間と形式を取得:yyyy-mm-dd hh:mm:ss * @return * / public static string getCurrentTimenormal(){SimpledateFormat SDF = new SimpledAteFormat(datetime_normal_format); string currentDate = sdf.format(new date()); currentdateを返します。 } / ** *現在の時間と形式を取得:yyyymmddhhmmss * @return * / public static string getCurrentTimeCompAct(){simpledateFormat sdf = new SimpledateFormat(datetime_compact_format); string currentDate = sdf.format(new date()); currentdateを返します。 } / ** *現在の時間と形式を取得:yyyy-mm-dd * @return * / public static string getCurrentDatenormal(){SimpledateFormat sdf = new SimpledateFormat(date_normal_format); string currentDate = sdf.format(new date()); currentdateを返します。 } / ** *現在の時間と形式を取得:yyyymmdd * @return * / public static string getCurrentDateCompAct(){SimpledAteFormat sdf = new SimpledateFormat(date_compact_format); string currentDate = sdf.format(new date()); currentdateを返します。 } / ** * Format 20101202 2010-12-02への時間 * * @param DateString Time Format:yyyymmdd * @return * / public Static String getDateCompActtonor(String DateString){StringBuilder SB = new StringBuilder(); sb.append(datestring.substring(0,4))。append( " - ")。append(datestring.subsequence(4、6))。append( " - ")。 return sb.tostring(); } / ** * Format 20101202101423 2010-12-02までの時間10:14:23 * * @parammddhhmmss * @return * / public static string getdateTimecompacttonormal(String DateString){StringBuilder SB = New StringBuilder(); sb.append(datestring.substring(0,4))。append( " - ")。append(datestring.subsequence(4、6))。append( " - ")。append(datestring.substring(6、8))。 12))。append( ":")。append(datestring.substring(12)); return sb.tostring(); } / ** *インターフェイスの時間入力を断続的な時間文字列に変換 * 2010-12-02 10:14:23タイムは20101202101423 * @param datenortr string * @return string * / public static string getCompactString(String DateNormalStr){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(Exception ex){//文字列が十分に長くない場合は、前の部分を返します} 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){カレンダーカレンダー=カレンダー=カレンダー.getinstance(); long CurrentTimemillis = Calendar.getTimeInmillis(); long hourmillis = 60 * 60 * 1000; long dval = numval * 24 * hourinmillis; SimpleDateFormat sdf = new SimpledateFormat(dateFormat); string currentdate = sdf.format(currenttimemillis -dval); currentdateを返します。 } / ** *現在の日付の前に日付を取得し、日数を押しますlong CurrentTimemillis = Calendar.getTimeInmillis(); long hourmillis = 60 * 60 * 1000; long dval = numval * 24 * hourinmillis; SimpleDateFormat sdf = new SimpledateFormat(dateFormat); string currentdate = sdf.format(currenttimemillis + dval); currentdateを返します。 } / ** *現在の日付の前に日付を取得し、1時間ごとにプッシュします * @param numval * @param dateformat * @return * @author wul * 2016-1-17 * / public static string getBeforedPeplushour(int numval、string dateformat){Calendar Calendar = Calendar.getInstance(); long CurrentTimemillis = Calendar.getTimeInmillis(); long hourmillis = 60 * 60 * 1000; long dval = numval * hourinmillis; SimpleDateFormat sdf = new SimpledateFormat(dateFormat); string currentdate = sdf.format(currenttimemillis -dval); currentdateを返します。 } / ** *現在の日付の前に日付を取得し、1時間ごとにプッシュします * @param numval * @param dateformat * @return * @author wul * 2016-1-17 * / public static string getafterdateplushour(int numval、string dateformat){calendaryandare = calendar.getinstance(); long CurrentTimemillis = Calendar.getTimeInmillis(); long hourmillis = 60 * 60 * 1000; long dval = numval * hourinmillis; SimpleDateFormat sdf = new SimpledateFormat(dateFormat); string currentdate = sdf.format(currenttimemillis + dval); currentdateを返します。 } / ** * 2つの日付の間で異なる日数 * @param begindate * @param enddate * @return * @author wul * 2016-1-18 * / public static int daysbetween(date begindate、date enddate){calendar cal = calendar.getinstance(); cal.settime(begindate); long time1 = cal.getTimeInmillis(); Cal.settime(enddate); long tigh2 = cal.getTimeInmillis(); long bety_days =(time2 -time1) /(1000 * 3600 * 24); return integer.parseint(string.valueof(bety_days)); } / ** *特定の月の日数を取得 * @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、year); cal.set(calendar.month、month -1); cal.getactualmaximum(calendar.date); } / ** *年を時間に追加または減算 * @param date * @param plustime * @return * @author wul * 2016-1-18 * / public static date getDatePlusyEar(日付、INTプラスティム) Cal.Settime(日付); Cal.Add(calendar.year、lustime);日付d = cal.gettime(); d; } / ** *時間の追加または減算 * @param date * @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 date * @param plustime * @return * @author wul * 2016-1-18 * / public static date getDatePlusDay(日付、INT Plustime) Cal.Settime(日付); Cal.Add(calendar.date、plustime);日付d = cal.gettime(); d; } / ** * time * @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; } / ** * time * @param date * @param plustime * @return * @author wul * 2016-1-18 * / public static date getDatePlusMinute(日付、int plustime){calendar cal = calendar.getinstance(); Cal.Settime(日付); Cal.Add(Calendar.Minute、プラスチム);日付d = cal.gettime(); d; } / ** * sudts sconds to time * @param date * @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 getCurryear(){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 calendar.get(12); } / *: return calendar.get(13); } / ** *現在の年を返します * @return * @author wul * 2016-1-18 * / public static int getyear(日付){calendarycameard = calendar.getInstance(); calendar.settime(date); return Calendar.get(1); } / ** *現在の月を返します * @return * @author wul * 2016-1-18 * / public static intmonth(date date){calendar calendar = calendar.getInstance(); calendar.settime(date); return calendar.get(2) + 1; } / ** *現在の日を返します * @return * @author wul * 2016-1-18 * / public static int getday(date date){calendar calendar = calendar.getinstance(); calendar.settime(date); return calendar.get(5); } / ** *現在の時間を返します * @return * @author wul * 2016-1-18 * / public static int gethour(date date){calendar calendar = calendar.getInstance(); calendar.settime(date); return calendar.get(11); } / ** *現在の分を返します * @return * @author wul * 2016-1-18 * / public static int getminute(date date){calendar calendar = calendar.getInstance(); calendar.settime(date); return calendar.get(12); } / *: calendar.settime(date); 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( "date"、new date()); string json = jsonobject.fromobject(map).toString(); System.out.println(json); }}上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。