日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java中封装日期加时间_java日期处理简单封装

發布時間:2025/4/5 编程问答 17 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java中封装日期加时间_java日期处理简单封装 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1 packageluojing.date;2

3 importjava.io.Serializable;4 importjava.text.ParseException;5 importjava.text.SimpleDateFormat;6 importjava.util.Calendar;7 importjava.util.Date;8 importjava.util.TimeZone;9

10 /**

11 * 日期時間處理封裝12 *13 *@authorluojing14 */

15 public class DateTime implements Comparable, Serializable {16

17 private static final long serialVersionUID = 4715414577633839197L;18 private Calendar calendar =Calendar.getInstance();19 private SimpleDateFormat sdf = newSimpleDateFormat();20

21 private final String DEFAULT_DATETIME_PATTERN = "yyyy-MM-dd HH:mm:ss";22 private final String DEFAULT_DATE_PATTERN = "yyyy-MM-dd";23 private final String DEFAULT_TIME_PATTERN = "HH:mm:ss";24

25 publicDateTime() {26 }27

28 publicDateTime(String dateStr) {29 try{30 parse(dateStr);31 } catch(Exception e) {32 e.printStackTrace();33 }34 }35

36 publicDateTime(String dateStr, TimeZone timeZone) {37 this(dateStr);38 calendar.setTimeZone(timeZone);39 }40

41 publicDateTime(String dateStr, String pattern) {42 try{43 parse(dateStr, pattern);44 } catch(Exception e) {45 e.printStackTrace();46 }47 }48

49 publicDateTime(String dateStr, String pattern, TimeZone timeZone) {50 this(dateStr, pattern);51 calendar.setTimeZone(timeZone);52 }53

54 public DateTime(int year, int month, int day, int hour, int minute, intsecond) {55 calendar.set(year, month, day, hour, minute, second);56 }57

58 public DateTime(int year, int month, int day, int hour, int minute, intsecond, TimeZone timeZone) {59 this(year, month, day, hour, minute, second);60 calendar.setTimeZone(timeZone);61 }62

63 public DateTime(longmilliSeconds) {64 calendar.setTimeInMillis(milliSeconds);65 }66

67 publicCalendar getCalendar() {68 returncalendar;69 }70

71 public voidsetCalendar(Calendar calendar) {72 this.calendar =calendar;73 }74

75 publicDate getDate() {76 returncalendar.getTime();77 }78

79 public voidsetDate(Date date) {80 calendar.setTime(date);81 }82

83 public intgetYear() {84 returncalendar.get(Calendar.YEAR);85 }86

87 public void setYear(intyear) {88 calendar.set(Calendar.YEAR, year);89 }90

91 public intgetMonth() {92 returncalendar.get(Calendar.MONTH);93 }94

95 public void setMonth(intmonth) {96 calendar.set(Calendar.MONTH, month);97 }98

99 public intgetDay() {100 returncalendar.get(Calendar.DAY_OF_MONTH);101 }102

103 public void setDay(intdayOfMonth) {104 calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);105 }106

107 public intgetHour() {108 returncalendar.get(Calendar.HOUR_OF_DAY);109 }110

111 public void setHour(inthour) {112 calendar.set(Calendar.HOUR_OF_DAY, hour);113 }114

115 public intgetMinute() {116 returncalendar.get(Calendar.MINUTE);117 }118

119 public void setMinute(intminute) {120 calendar.set(Calendar.MINUTE, minute);121 }122

123 public intgetSecond() {124 returncalendar.get(Calendar.SECOND);125 }126

127 public void setSecond(intsecond) {128 calendar.set(Calendar.SECOND, second);129 }130

131 public longgetTimeInMilliSeconds() {132 returncalendar.getTimeInMillis();133 }134

135 public void setTimeInMilliSeconds(longmilliSeconds) {136 calendar.setTimeInMillis(milliSeconds);137 }138

139 publicTimeZone getTimeZone() {140 returncalendar.getTimeZone();141 }142

143 public voidsetTimeZone(TimeZone timeZone) {144 calendar.setTimeZone(timeZone);145 }146

147 /**

148 * 使用默認格式解析日期字符串149 *150 *@paramdateStr151 *@throwsException152 */

153 public void parse(String dateStr) throwsException {154 try{155 parse(dateStr, DEFAULT_DATETIME_PATTERN);156 } catch(Exception e) {157 try{158 parse(dateStr, DEFAULT_DATE_PATTERN);159 } catch(Exception e1) {160 try{161 parse(dateStr, DEFAULT_TIME_PATTERN);162 } catch(Exception e2) {163 throw new Exception("the date string [" + dateStr + "] could not be parsed");164 }165 }166 }167

168 }169

170 /**

171 * 使用給定模式解析日期字符串172 *173 *@paramdateStr174 *@parampattern175 *@throwsException176 */

177 public void parse(String dateStr, String pattern) throwsException {178 if (dateStr == null) {179 throw new NullPointerException("date string could not be null");180 }181

182 if (pattern == null) {183 throw new NullPointerException("the pattern string could not be null");184 }185

186 try{187 sdf.applyPattern(pattern);188 sdf.parse(dateStr);189 } catch(ParseException e) {190 throw new Exception("the date string [" + dateStr + "] could not be parsed with the pattern [" + pattern + "]");191 }192 calendar =sdf.getCalendar();193 }194

195 /**

196 * 格式化當前DateTime對象代表的時間197 *198 *@parampattern199 *@return

200 */

201 publicString format(String pattern) {202 sdf.applyPattern(pattern);203 returnsdf.format(calendar.getTime());204 }205

206 /**

207 * 獲取默認格式日期字符串208 *209 *@return

210 */

211 publicString toDateTimeString() {212 sdf.applyPattern(DEFAULT_DATETIME_PATTERN);213 returnsdf.format(calendar.getTime());214 }215

216 @Override217 public intcompareTo(DateTime otherDateTime) {218 returncalendar.compareTo(otherDateTime.getCalendar());219 }220

221 /**

222 * 是否閏年223 *224 *@return

225 */

226 public booleanisLeapYear() {227 int year =getYear();228 boolean result = false;229 if (year % 100 == 0) {230 if (year % 400 == 0) {231 result = true;232 }233 } else if (year % 4 == 0) {234 result = true;235 }236 returnresult;237 }238

239 /**

240 * 獲取星期241 *242 *@return

243 */

244 public intgetDayOfWeek() {245 returncalendar.get(Calendar.DAY_OF_WEEK);246 }247

248 /**

249 * 是否周末250 *251 *@return

252 */

253 public booleanisWeekend() {254 int dayOfWeek =getDayOfWeek();255 return dayOfWeek == 1 || dayOfWeek == 7;256 }257

258 /**

259 * 當前DateTime對象月份天數260 *261 *@return

262 */

263 public intgetDayNumsInMonth() {264 Calendar c =(Calendar) calendar.clone();265 c.set(Calendar.DAY_OF_MONTH, 1);266 c.roll(Calendar.DAY_OF_MONTH, -1);267 returnc.get(Calendar.DAY_OF_MONTH);268 }269

270 /**

271 * 兩個日期之間間隔天數272 *273 *@paramotherDateTime274 *@return

275 */

276 public intdayNumFrom(DateTime otherDateTime) {277 long millis = Math.abs(getTimeInMilliSeconds() -otherDateTime.getTimeInMilliSeconds());278 int days = (int) Math.floor(millis / 86400000);279 returndays;280 }281

282 public booleanlessThan(DateTime otherDateTime) {283 return compareTo(otherDateTime) < 0;284 }285

286 public booleangreaterThan(DateTime otherDateTime) {287 return compareTo(otherDateTime) > 0;288 }289

290 public booleanequal(DateTime otherDateTime) {291 return compareTo(otherDateTime) == 0;292 }293

294 /**

295 * 當前時間基礎上增加秒數(負數時為減)296 *297 *@paramamount298 */

299 public void plusSecond(intamount) {300 calendar.add(Calendar.SECOND, amount);301 }302

303 public void plusMinute(intamount) {304 calendar.add(Calendar.MINUTE, amount);305 }306

307 public void plusHour(intamount) {308 calendar.add(Calendar.HOUR, amount);309 }310

311 public void plusDays(intamount) {312 calendar.add(Calendar.DAY_OF_MONTH, amount);313 }314

315 public void plusMonth(intamount) {316 calendar.add(Calendar.MONTH, amount);317 }318

319 public void plusYear(intamount) {320 calendar.add(Calendar.YEAR, amount);321 }322

323 public void plus(int year, int month, int day, int hour, int minute, intsecond) {324 plusYear(year);325 plusMonth(month);326 plusDays(day);327 plusHour(hour);328 plusMinute(minute);329 plusSecond(second);330 }331

332 }

總結

以上是生活随笔為你收集整理的java中封装日期加时间_java日期处理简单封装的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。