実装コード1:
Import java.text.simpledateformat; import java.util.calendar; import java.util.date; public class dateTeStutil {public static void main(string [] args)throws {simpledateformat sdf = new simpledateformat( "yyyymmdd"); string str = "20110823";日付dt = sdf.parse(str); Calendar rightnow = calendar.getInstance(); rightnow.settime(dt); rightnow.add(calendar.year、-1); //日付マイナス1年右右文字列拘束= sdf.format(dt1); system.out.println(strem); }}注:カレンダーオブジェクトのADDメソッドでは、2番目のパラメーターは「追加」を意味する正の数値であり、「減算」を意味する負の数値です。
コード2:Javaの日付は日数を追加および減算します
テストクラスコード:
java.text.simpledateformatをインポートします。 Import Java.util.date; public class dateTest {public static void main(string [] arg){date now = new date(); addandSubtractDaysBygetTime(Now、-5); addandSubtractDaysBygetTime(現在、5); addandsubtractdaysbycalendar(現在、-5); addandsubtractdaysbycalendar(現在、5); } public static date addandsubtractdaysbygettime(日付dateTime/*保留日*/、int n/*追加および減算日*/){// simpledateformat df = new simpledateformat( "yyyy-mm-dd"); SimpleDateFormat dd = new SimpledateFormat( "yyyy-mm-dd hh:mm:ss"); System.out.println(df.format(new Date(datetime.getTime() + n * 24 * 60 * 60 * 1000L))); //system.out.println(dd.format(new date(datetime.getTime() + n * 24 * 60 * 60 * 1000L))); //ここで長いタイプに変換する必要があることに注意してください。そうしないと、nが25を超えると範囲のオーバーフローが発生し、目的の日付値を取得できないようにします。 } public static date addandsubtractdaysbycalendar(日付dateTime/*保留日*/、int n/*追加および減算日*/){// simpledateformat df = new simpledateformat( "yyyy-mm-dd"); SimpleDateFormat dd = new SimpledateFormat( "yyyy-mm-dd hh:mm:ss"); java.util.calendar calstart = java.util.calendar.getInstance(); calstart.settime(datetime); calstart.add(java.util.calendar.day_of_week、n); system.out.println(df.format(calstart.gettime())); //system.out.println(dd.format(calstart.gettime())); calstart.getTime()を返します。 }}実行結果:
2014-10-06
2014-10-16
2014-10-06
2014-10-16
コード3:
オンラインで情報を確認し、要約した日付クラスに関するツールクラスを追加します。
パッケージcom.data.utils; import java.text.simpledateformat; import java.util.calendar; import java.util.date; public class dateformat { / ***日付は何年ですか* / public static string dateminusyear(string str)Throws Expection日付dt = sdf.parse(str); Calendar rightnow = calendar.getInstance(); rightnow.settime(dt); rightnow.add(calendar.year、-1); //日付マイナス1年日付dt1 = rightnow.gettime();文字列拘束= sdf.format(dt1);抑制を返します。 } / ***これまでに何年追加するか* / public static string dateaddyear(string str)throws exception {simpledateformat sdf = new simpledateFormat( "yyyymm");日付dt = sdf.parse(str); Calendar rightnow = calendar.getInstance(); rightnow.settime(dt); rightnow.add(calendar.year、1); // date plus 1 year date dt1 = rightnow.gettime();文字列拘束= sdf.format(dt1);抑制を返します。 } / ***日付は何ヶ月減少しますか* / public static string dateminusmonth(string str)throws exception {simpledateformat sdf = new simpledateFormat( "yyyymm");日付dt = sdf.parse(str); //文字列calendarを生成しますrightnow = calendar.getInstance(); rightnow.settime(dt); //指定された日付を使用してこのカレンダーの時間を設定します。 rightnow.add(calendar.month、-1); //日付マイナス1か月日付dt1 = rightnow.gettime(); //カレンダー時間値を表す日付オブジェクトを返します。 string strim = sdf.format(dt1); //指定された日付を日付/時刻文字列としてフォーマットし、指定されたstringbufferに結果を追加します。抑制を返します。 } / ** *日付は何ヶ月追加されますか * / public static string dateaddmonth(string str)throws exception {simpledateformat sdf = new simpledateFormat( "yyyymm");日付dt = sdf.parse(str); Calendar rightnow = calendar.getInstance(); rightnow.settime(dt); rightnow.add(calendar.month、1); // 3か月を日付に追加// rightnow.add(calendar.day_of_year、10); //日付プラス10日日付dt1 = rightnow.gettime();文字列拘束= sdf.format(dt1);抑制を返します。 } / ** *今年と月の最初の月にstrを取得 * @param str * 201505 * @return 201501 * @throws例外 * / public static string dateonemonth(string str){str = str.substring(0、str.length()-2); str = str + "01"; strを返します。 } /*** 1月から選択した月が何ヶ月かを計算します。 * @param str 201509 * @return 9 */ public static int datedistancemonth(string str){int i = integer.parseint(str); int j = integer.parseint(dateformat.dateonemonth(str)); system.out.println(i -j); I -J + 1を返します。 } / ** * Millisecondsに正確に2回の時間差を取得 * @param str * @return * / public static string timedifference(long start、long end){long between = end -start;長い日= /(24 * 60 * 60 * 1000);長い時間=( /(60 * 60 * 1000) - 日 * 24);長いmin =(( /(60 * 1000)) - day * 24 * 60 -hour * 60); long s =( / 1000 -day * 24 * 60 * 60-時間 * 60-分 * 60); long ms =(間 - 日 * 24 * 60 * 60 * 1000 -hour * 60 * 60 * 60 * 1000 -min * 60 * 1000 -S * 1000); String Timedifference = day + "day" + hour + "hour" + "minute" + s + "秒" + ms + "milliseconds"; return timedifference; }} / ** * 24時間、1週間、1か月の開始時間を取得 * * @param timeinterval *:day_time_interval week_interval month_time_interval * @return "yyyy-mm-dd hh:mm:ss" * / public static string getStarttime(int timeinterval) SimpleDateFormat sdf = new SimpledateFormat( "yyyy-mm-dd hh:mm:ss"); if(day_time_interval == timeinterval){// 24時間開始時間cal.set(calendar.hour_of_day、0)を取得します; cal.set(calendar.minute、0); cal.set(Calendar.second、0); string starttime = sdf.format(cal.gettime()); StartTimeを返します。 } else if(week_time_interval == timeinterval){int feekday = cal.get(calendar.day_of_week)-1; Cal.Add(calendar.date、-weekday); cal.set(calendar.hour_of_day、0); cal.set(calendar.minute、0); cal.set(Calendar.second、0); string starttime = sdf.format(cal.gettime()); StartTimeを返します。 } else if(month_time_interval == timeinterval){int dayofmonthmin = cal.getactualminimum(calendar.day_of_month); // c.add(calendar.date、-dayofmonth); cal.set(calendar.date、dayofmonthmin); cal.set(calendar.hour_of_day、0); cal.set(calendar.minute、0); cal.set(Calendar.second、0); string starttime = sdf.format(cal.gettime()); StartTimeを返します。 } nullを返します。 } / ** * 24時間、1週間、1か月の終了時間を取得 * * @param timeinterval *:day_time_interval week_interval month_time_interval * @return "yyyy-mm-dd hh:mm:ss" * / public static string getEndtime(int timeinterval){calland.getinstance(); cal.settimezone(timezone.gettimezone( "gmt+8")); SimpleDateFormat sdf = new SimpledateFormat( "yyyy-mm-dd hh:mm:ss"); if(day_time_interval == timeinterval){cal.set(calendar.hour_of_day、23); Cal.set(12、59); Cal.set(13、59); long date = cal.getTimeInmillis(); string endtime = sdf.format(new Date(date)); return endtime; } else if(week_time_interval == timeinterval){int feekday = cal.get(calendar.day_of_week)-1; Cal.Add(calendar.date、-weekday); Cal.Add(calendar.date、6); cal.set(calendar.hour_of_day、23); Cal.set(12、59); Cal.set(13、59); long date = cal.getTimeInmillis(); string endtime = sdf.format(new Date(date)); return endtime; } else if(month_time_interval == timeinterval){int dayofmonthmax = cal.getactualmaximum(calendar.day_of_month); cal.set(calendar.date、dayofmonthmax); cal.set(calendar.hour_of_day、23); cal.set(calendar.minute、59); Cal.set(Calendar.second、59); string endtime = sdf.format(cal.gettime()); return endtime; } nullを返します。 } /*** datestrが開始と終了の間にあるかどうかを判断します。スタートとエンドの両方がnull yyyymmddhhmmssまたはyyyymmdd format * * @author you.xu * @date 2015年8月19日午後3時11:46 * @param datestr * @param start * @param end * @return */ public static boolean checkdateval(string string、String end、String string、String end);日付date = null; date startdate = null;日付enddate = null; SimpleDateFormat sdf = null; //裁判官日付形式if(14 == datestr.length()){sdf = new simpledateFormat( "yyymmddhhmmss"); } else if(8 == datesestr.length()){sdf = new simpledateFormat( "yyyymmdd"); } else falseを返します。 try {//裁判官の日付フォーマット日付= sdf.parse(datestr)を変更します。 } catch(parseexception e){log.error(e、e); } if((start == null)&&(end!= null)){try {enddate = sdf.parse(end); } catch(parseexception ex1){log.error(ex1、ex1); } if(date!= null)&&(enddate!= null))// {if(date.comPareTo(enddate)<= 0)isdateright = true; }} else if((start!= null)&&(end == null)){try {startdate = sdf.parse(start); } catch(parseexception ex1){log.error(ex1、ex1); } if(date!= null)&&(startDate!= null)){if(date.comPareto(startDate)> = 0)isDateright = true; }} else if((start!= null)&&(end!= null)){try {startdate = sdf.parse(start); enddate = sdf.parse(end); } catch(parseexception ex2){system.out.println(ex2.tostring()); } if((startdate!= null)&&(date!= null)&&(enddate!= null)){if((date.compareto(startdate)> = 0)&&(date.comPareto(enddate)<= 0))isdateright = true; }} isdaterightを返します。 } / ** * datestrがスタートとエンドの間にあるかどうかを判断するために、スタートとエンドの両方がnull long format * * @author you.xu * @date 2015年8月19日午後3時12分35 pm * @param datestr * @param start * @param end * @return * / public static boolean checkdatev(String string、String、String、String、String datest、String String長い日付= -1; long fromdate = -1; long todate = -1; date = java.lang.long.parselong(datestr); if((start == null)&&(end == null)){isdateright = true; } else if((start == null)&&(end!= null)){try {todate = java.lang.long.parselong(end); } catch(numberformatexception nfe){log.error(nfe、nfe); } if(date <= todate){isdateright = true; }} else if((start!= null)&&(end == null)){try {fromdate = java.lang.long.parselong(start); } catch(numberformatexception nfe){log.error(nfe、nfe); } if(date> = fromdate){isdateright = true; }} else if((start!= null)&&(end!= null)){try {todate = java.lang.long.parselong(end); fromdate = java.lang.long.parselong(start); } catch(numberformatexception nfe){log.error(nfe、nfe); } if((date <= todate)&&(date> = fromdate)){isdateright = true; }} isdaterightを返します。 }これらはこれまでに使用されており、いつでも追加できます。シンプルで便利なタイムツールがあります。私はあなたと一緒に学びたいと思っています、そして私は彼らをコメントで指摘します。ありがとう! ! !