プロジェクト開発では、日付は私たちの不可欠な部分です。この記事では、後の使用を容易にするために、コード開発の日付に関するいくつかの一般的な方法を要約します。料理はすぐ下に提供されます。
パッケージcom.example.util; java.text.parseexceptionをインポートします。 java.text.simpledateformatをインポートします。 java.util.arraylistをインポートします。 java.util.calendarをインポートします。 Import Java.util.date; java.util.listをインポートします。 / ***一般的な日付方法** @Author**/ public class dateutils {/ ***共通変数*/ public static final string date_format_full = "yyyy-mm-dd hh:mm:ss"; public static final string date_format_ymd = "yyyy-mm-dd"; public static final string date_format_hms = "hh:mm:ss"; public static final string date_format_hm = "hh:mm"; public static final string date_format_ymdhm = "yyyy-mm-dd hh:mm"; public static final string date_format_ymdhms = "yyyymmddhhmmss"; public static final long one_day_mills = 3600000 * 24; public static final int week_days = 7; private static final int datelength = date_format_ymdhm.length(); / ** * format stringに変換 * * @param time * @param format * @return */ public static string formatdateToString(date time、string format){simpledateFormat sdf = new simpledateFormat(format); sdf.format(time)を返します。 } / ** *文字列をフォーマット日付に変換 *(注:入力した日付が2014-12-21 12:12の場合、対応する形式はyyyy-mm-dd hh:mm *である必要があります。 SimpleDateFormat sdf = new SimpledateFormat(format); try {return sdf.parse(date); } catch(parseexception ex){ex.printstacktrace();新しいruntimeexception(ex.tostring()); }} / ** *日付が2つの期間に属するかどうかを判断します * @param time * @param timerange * @return * / public static boolean istimeinrange(date time、date [] timerange){return(!time.timerange [0])&&!time.after(timerange [1]); } / ** *完全な時間から1分までの時間 * * @param fulldatestr * @return * / public static string getDateTominute(String fulldatestr){return fulldatestr == null? null :( fulldatestr.length()> = datelength?fulldatestr.substring(0、datelength):fulldatestr); } /***指定された年のすべての週を返します。リストには文字列[2]オブジェクト文字列[0]開始日の開始日、文字列[1]は週の終了日です。 *日付はyyyy-mm-ddの形式です。毎年の最初の週には月曜日を含め、完全な7日間でなければなりません。 *例:2009年の最初の週は2009-01-05に始まり、2009-01-11に終了します。月曜日は何年ですか?では、今週はどの週が含まれていますか? *例:2008-12-29は2009-01-04は日曜日であるため、今週は2008年の最後の週です。 list <string []> result = new ArrayList <String []>(weeks); int start = 1; int end = 7; for(int i = 1; i <= week; i ++){string [] tempweek = new String [2]; tempweek [0] = getDateFordAyofWeek(year、i、start); tempweek [1] = getDateFordAyofWeek(year、i、end); result.add(tempweek); } return result; } / ** *指定された年と週の前年と週を計算します * * @param year * @param week * @return @ * / public static int [] getlastyearweek(int year、int week){if(week <= 0){show new IllegalargumentException( "週ぶりの数字は1 !! !!"); } int [] result = {week、year}; if(week == 1){//前年結果[1] - = 1; //先週の結果[0] = GetWeeKnumofyear(result [1]); } else {result [0] - = 1; } return result; } / ** *次の[年、年] * * @param year * @param week * @return @ * / public static int [] getNextyEarweek(int year、int week){if(week <= 0){throw new Illegalargumentexception( "週ごとの数字は1未満ではない!! !!"); } int [] result = {week、year}; int weeks = getweeknumofyear(year); if(week == week){//来年結果[1] += 1; //最初の週の結果[0] = 1; } else {result [0] += 1; } return result; } /***指定された年に何週間あるかを計算します。 (月曜日に開始) * * @param year * @return @ */ public static int getweeknumofyear(final int year){return getweeknumofyear(year、calendar.monday); } /***指定された年に何週間あるかを計算します。 * * @param year * yyyy * @return @ */ public static int getweeknumofyear(final int year、int firstdayofweek){//毎年少なくとも52週間、最大53週間です。 int minweeks = 52; int maxweeks = 53; int result = minweeks; int sindex = 4;文字列date = getDateFordayofWeek(年、Maxweeks、FirstDayofWeek); //年が一致するかどうかを判断するために、それが一致する場合、それは53週間があることを意味します。 if(date.substring(0、sindex).equals(year)){result = maxweeks; } return result; } public static int getweeksofweekyear(final int year){calendar cal = calendar.getInstance(); cal.setfirstdayofweek(calendar.monday); cal.setMinimaldaysInfirstweek(week_days); cal.set(calendar.year、year); return cal.getweeksinweekyear(); } / ** *指定された年の曜日に対応する日付を取得しますweekofyear、dayofweek、calendar.monday); } /** *指定された年の曜日に対応する日付を取得し、yyyy-mm-ddの曜日を指定して、週の最初の日(Firstdayofweek) * * @param weekear * @param dayofweek * @param day firstdayofweek * @param ddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddde format */ public static string getDateFordayofWeek(int year、int weekofyear、int dayofweek、int firstdayofweek){calendar cal = calendar.getinstance(); cal.setfirstdayofweek(firstdayofweek); cal.set(calendar.day_of_week、dayofweek); cal.setMinimaldaysInfirstweek(week_days); cal.set(calendar.year、year); cal.set(calendar.week_of_year、weekofyear); return formatdateTostring(cal.getTime()、date_format_ymd); } / ** *週の指定された日付を取得 * * @param dateTime * @Throws parseexception * @ * / public static int getWeekofDate(string dateTime)throws parseexception {calendar cal = calendar.getInstance(); cal.setfirstdayofweek(calendar.monday); cal.setMinimaldaysInfirstweek(week_days);日付date = formattringtodate(datetime、date_format_ymd); Cal.Settime(日付); return cal.get(calendar.day_of_week); } / ** *特定の年(月曜日から週の初日まで)の特定の週のすべての日付を計算します * * @param Yearnum * @return * / public static List <string> getWeekdays(int yearnum、int weeknum){return getWeekdays(Yearnum、weekeekdays、calenday.monday); } / ** *特定の年の特定の週(7日間)のすべての日付を計算します * * @param Yearnum * @param weeknum * @return yyyy-mm-ddフォーマット日付リスト * / public static list <string> getWeekdays(int year、int weekear、int firstdayofweek) int dayofweek = firstdayofweek; for(int i = 0; i <week_days; i ++){dates.add(getdatefordayofweek(year、weekofyear、dayofweek ++、firstdayofweek)); }日付を返します。 } / ** *前週、今週、または来週の目標日付の年と週の情報を取得 * * @param querydate *着信時間 * @param weekoffset * -1:先週:来週1:@param firstdayofweek week offset、int firstdayofweek)throws parseexception {date date = formatstringtodate(querydate、date_format_ymd);カレンダーカレンダー= calendar.getInstance(); calendar.settime(date); calendar.setfirstdayofweek(FirstDayofweek); calendar.setMinimaldaysInfirstweek(week_days); int year = calendar.getWeekyear(); int week = calendar.get(calendar.week_of_year); int [] result = {week、year}; switch(week offset){case 1:result = get nextyearweek(year、week);壊す;ケース-1:result = getLastyearweek(year、week);壊す;デフォルト:break; } return result; } / ** * 2日間の日数を計算します * * @param startdate * startdate * @param enddate * enddate string * @return * @throws parseexception * / public static int getDaysbetween(string startdate、string enddate)throws parseexception {int daygap = 0; if(startdate!= null && startdate.length()> 0 && enddate!= null && enddate.length()> 0){date end = formatstringtodate(enddate、date_format_ymd);日付start = formattringtodate(startdate、date_format_ymd); daygap = getDaysbetWeen(start、end); } daygapを返します。 } private static int getDaysbetween(date startdate、date enddate){return(int)((enddate.getTime() - startdate.getTime()) /one_day_mills); } / ** * 2つの日付の間の日数の差を計算 * @param startdate * @param enddate * @return * / public static int getDaysgaPofdates(date startdate、date enddate){int date = 0; if(startdate!= null && enddate!= null){date = getDaysbetween(startDate、endDate); }返品日; } / ** * 2つの日付間の年間ギャップを計算します * * @param firstdate * @param seconddate * @return * / public static int getyeargapofdates(date firstdate、date seconddate){if(firstdate == null || seconddate == null){return 0; } calendary helpCalendar = calendar.getInstance(); helpcalendar.settime(firstdate); int firstyear = helpcalendar.get(calendar.year); helpcalendar.settime(seconddate); int secondyear = helpcalendar.get(calendar.year); SecondEy -firstYearを返します。 } / ** * 2つの日付間の月ギャップを計算 * * @param firstDate * @param seconddate * @return * / public static int getMonthgaPofdates(date firstdate、date seconddate){if(firstdate == null || seconddate == null){return 0; } return(int)((seconddate.getTime() - firstDate.getTime()) / one_day_mills / 30); } / ** *現在の日付が含まれているかどうかを計算します * * @param datys * @return * / public static boolean iscontaincurrent(list <string> dates){boolean flag = false; SimpleDateFormat fmt = new SimpledateFormat(date_format_ymd);日付date = new date();文字列datestr = fmt.format(date); for(int i = 0; i <dates.size(); i ++){if(datestr.equals(dates.get(i))){flag = true; }} return flag; } / ** *日付からクイーンの時間の日付を計算 * if(startdate!= null && startdate.length()> 0){date date = formattringtodate(startdate、date_format_ymd);カレンダーc = charend.getInstance(); c.settime(日付); c.set(calendar.day_of_year、time);日付= c.getTime(); resultdate = formatdateToString(date、date_format_ymd); } runter resultdate; } / ** *指定された日付が特定の日付から計算される年の週を取得します * * @param date * @param admitdate * @return * @throws parseexception * @ * / public static int [] getyearandweeks(string、string admitdate)throws pheseexception {calendar c = calendar.getinstance(); c.settime(formatstringtodate(admitdate、date_format_ymd)); int time = c.get(calendar.day_of_week); getWeekandyear(日付、0、時間)を返します。 } / ** *指定された日付refdateを取得します。 * @param refdate *参照日 * @param weaceoffset * -1:先週:今週1:@param startdate *週の最初の日付 * @return yyyy-mm-dd date * @throws parseex( @public static ing> wibrotic static date * @return yyyy-mm-dd Date * refdate、int week offset、string startdate)throws parseexception {// startdate週の最初の日C = calendar.getInstance(); c.settime(formatstringtodate(startdate、date_format_ymd)); int firstdayofweek = c.get(calendar.day_of_week); //対応する週を取得しますint [] weekandyear = getWeekandyear(refdate、week offset、firstdayofweek); //対応する週のすべての日付を取得します。 } / ** *時点に応じて時間間隔を取得 * * @param hours * @return * / public static list <string []> getTimePointsbyhour(int [] hours){list <string []> houraylist <new arraylist <string []>();文字列sbstart = ":00:00"; string sbend = ":59:59"; for(int i = 0; i <hourge.length; i ++){string [] hight = new String [2]; Times [0] = hours [i] + sbstart; times [1] =(hours [(i + 1 + hours.length)%hough.length] -1) + sbend; hourpoints.add(times); } hourpointsを返します。 } / ** *指定された日付に応じて日数を増やすか、減少させます * * @param date * @param lument * / public static date adddays(日付、int額){return add(date、calendar.day_of_month、額); } / ** *指定された日付に応じて数値を増やすか、削減します * * @param Calendarfield * @param lument * @return * / public static Date add(date、int calendaridfield、int boment){if(date == null){throw new IllegalargumentException( "the Null"); }カレンダーc = charend.getInstance(); c.settime(日付); c.add(カレンダーフィールド、金額); c.getTimeを返します(); } / ** *現在の日付2014-12-21 23:59:59 * @return * / public static date getCurdateWithMaxtime(){Calendar C = Calendar.getInstance(); c.set(calendar.hour_of_day、23); c.set(Calendar.minute、59); c.set(Calendar.second、59); c.getTimeを返します(); } / ** *現在の日付の最低日付時間を取得します2014-12-21 00:00:00 c.set(calendar.hour_of_day、0); c.set(calendar.minute、0); c.set(Calendar.second、0); c.set(calendar.millisecond、0); c.getTimeを返します(); }}上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。