この記事では、Java開発プロセス中に日付関連操作を紹介します。
java.text.text.simpledateformat使用日付操作 * * * @author puck * createdate 2015-07-30 22:54:38 */public class testdate {public static void main(string [] args)throws parseeeexceation、unsupport edencodingexception {calendar cal = calendar.getinstance( ); / / cal.add(calendar.day_of_month、-48); ){// SimpleDateFormat Format = new SimpleDateFormat( "Y年MM月DD s "second"、local.china); simpledateformat sdf = new simpledateformat( "yyyy-mm-dd hh:ss"、locale.china); @param date * @return * / public static string datetstring(calendar cal){return dateTostring(cal.getTime());静的カレンダーDateString Colocalendar(String DateStr)スローParseExcept {// caln dar.getinstance(); parseint(date.substring(5、7))-1、// integer.parseint(8、10)); mm:ss "); date date = sdf.parse(datestr); calendar cal = callearn.getinstance(); cal.settime(date); cal;} / ** * datestring to date * * @param date *フォーマット: yyyy-mm-dd hh:mm:ss date * @return carer * @throws parseexception */ public static datestrowtodate(string date)throws parseeexception {simpledateformat sdf = new simpledateformat( "yyyy-mm-dd hh:mm:mm:ss")) ; return sdf.parse(date);} / ** * @param date * / public staticar dateTocalendar(日付){calendar c1 = calendar.getInstance(); )リターンC1;日付(日付D1、カレンダーC1.GETINSTANCE() 2つの日付の違いを計算します。 ] p2 = {calendar.year)、c2.get(calendar.month)、c2.get(day_of_month)}; [1] = " + p1 [1] +" p1 [2] = " + p1 [2]); system.out.println(" p2 [0] = " + p2 [0] +" p2 [1] = " + p2 [1] +" p2 [2] = " + p2 [2]); int year = p2 [0] -p1 [0]; int month =(p2 [0] * 12) + p2 [1] - (p1 [0] * 12) + p1 [1]); int [] {year、month、day};}/ ** *取得日の最初の日 * * * * * @param c * @return */ public staticカレンダーgetlastdayofweek(カレンダーC){/ / SimpleDateFormat2 =新しいSimpledate Format( "y year moon d d ehがs秒に分割されたとき」、// system.out.println(" + format2.format(c.gettime()) ) @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ return * / public static calendar addordecreaday(calendar.day.day_of_month、day); **月の最終日 * @param * @param month * / public static int getlastdayofmonth(int year、int month){calendar c = calendar.getinstance(); 1); day_of_month);} / ***月の最終日を取得 * @param cal * / public static int getlastdayofmonth(calendar cal){return cal.getactualmaxumum(calendar.day_month);}}}