本人之前做的项目中积累常用的时间方法 Java代码 4. import java.text.DateFormat; 5. import java.text.ParsePosition; 6. import java.text.SimpleDateFormat; 7. import java.util.Calendar; 8. import java.util.Date; 9. import java.util.GregorianCalendar; 10. 11. public class TimeTest { 12. //用来全局控制 上一周,本周,下一周的周数变化 13. private int weeks = 0; 14. private int MaxDate;//一月最大天数 15. private int MaxYear;//一年最大天数 16. 17. 18. /** 19. * @param args 20. */ 21. public static void main(String[] args) { 22. TimeTest tt = new TimeTest(); 23. System.out.println("获取当天日期:"+tt.getNowTime("yyyy-MM-dd")); 24. System.out.println("获取本周一日期:"+tt.getMondayOFWeek()); 25. System.out.println("获取本周日的日期~:"+tt.getCurrentWeekday()); 26. System.out.println("获取上周一日期:"+tt.getPreviousWeekday()); 27. System.out.println("获取上周日日期:"+tt.getPreviousWeekSunday()); 28. System.out.println("获取下周一日期:"+tt.getNextMonday()); 29. System.out.println("获取下周日日期:"+tt.getNextSunday()); 30. System.out.println("获得相应周的周六的日期:"+tt.getNowTime("yyyy-MM-dd")); 31. System.out.println("获取本月第一天日期:"+tt.getFirstDayOfMonth()); 32. System.out.println("获取本月最后一天日期:"+tt.getDefaultDay()); 33. System.out.println("获取上月第一天日期:"+tt.getPreviousMonthFirst()); 34. System.out.println("获取上月最后一天的日期:"+tt.getPreviousMonthEnd()); 35. System.out.println("获取下月第一天日期:"+tt.getNextMonthFirst()); 36. System.out.println("获取下月最后一天日期:"+tt.getNextMonthEnd()); 37. System.out.println("获取本年的第一天日期:"+tt.getCurrentYearFirst()); 38. System.out.println("获取本年最后一天日期:"+tt.getCurrentYearEnd()); 39. System.out.println("获取去年的第一天日期:"+tt.getPreviousYearFirst()); 40. System.out.println("获取去年的最后一天日期:"+tt.getPreviousYearEnd()); 41. System.out.println("获取明年第一天日期:"+tt.getNextYearFirst()); 42. System.out.println("获取明年最后一天日期:"+tt.getNextYearEnd()); 43. System.out.println("获取本季度第一天到最后一天:"+tt.getThisSeasonTime(11)); 44. System.out.println("获取两个日期之间间隔天数2008-12-1~2008-9.29:"+TimeTest.getTwoDay("2008-12-1","2008-9-29")); 45. } 46. 47. 48. /** 49. * 得到二个日期间的间隔天数 50. */ 51. public static String getTwoDay(String sj1, String sj2) { 52. SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd"); 53. long day = 0; 54. try { 55. java.util.Date date = myFormatter.parse(sj1); 56. java.util.Date mydate = myFormatter.parse(sj2); 57. day = (date.getTime() - mydate.getTime()) / (24 * 60 * 60 * 1000); 58. } catch (Exception e) { 59. return ""; 60. } 61. return day + ""; 62. } 63. 64. 65. /** 66. * 根据一个日期,返回是星期几的字符串 67. * 68. * @param sdate 69. * @return 70. */ 71. public static String getWeek(String sdate) { 72. // 再转换为时间 73. Date date = TimeTest.strToDate(sdate); 74. Calendar c = Calendar.getInstance(); 75. c.setTime(date); 76. // int hour=c.get(Calendar.DAY_OF_WEEK); 77. // hour中存的就是星期几了,其范围 1~7 78. // 1=星期日 7=星期六,其他类推 79. return new SimpleDateFormat("EEEE").format(c.getTime()); 80. } 81. 82. /** 83. * 将短时间格式字符串转换为时间 yyyy-MM-dd 84. * 85. * @param strDate 86. * @return 87. */ 88. public static Date strToDate(String strDate) { 89. SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); 90. ParsePosition pos = new ParsePosition(0); 91. Date strtodate = formatter.parse(strDate, pos); 92. return strtodate; 93. } 94. 95. /** 96. * 两个时间之间的天数 97. * 98. * @param date1 99. * @param date2 100. * @return 101. */ 102. public static long getDays(String date1, String date2) { 103. if (date1 == null || date1.equals("")) 104. return 0; 105. if (date2 == null || date2.equals("")) 106. return 0; 107. // 转换为标准时间 108. SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd"); 109. java.util.Date date = null; 110. java.util.Date mydate = null; 111. try { 112. date = myFormatter.parse(date1); 113. mydate = myFormatter.parse(date2); 114. } catch (Exception e) { 115. } 116. long day = (date.getTime() - mydate.getTime()) / (24 * 60 * 60 * 1000); 117. return day; 118. } 119. 120. 121. 122. 123. // 计算当月最后一天,返回字符串 124. public String getDefaultDay(){ 125. String str = ""; 126. SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); 127. 128. Calendar lastDate = Calendar.getInstance(); 129. lastDate.set(Calendar.DATE,1);//设为当前月的1号 130. lastDate.add(Calendar.MONTH,1);//加一个月,变为下月的1号 131. lastDate.add(Calendar.DATE,-1);//减去一天,变为当月最后一天 132. 133. str=sdf.format(lastDate.getTime()); 134. return str; 135. } 136. 137. // 上月第一天 138. public String getPreviousMonthFirst(){ 139. String str = ""; 140. SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); 141. 142. Calendar lastDate = Calendar.getInstance(); 143. lastDate.set(Calendar.DATE,1);//设为当前月的1号 144. lastDate.add(Calendar.MONTH,-1);//减一个月,变为下月的1号 145. //lastDate.add(Calendar.DATE,-1);//减去一天,变为当月最后一天 146. 147. str=sdf.format(lastDate.getTime()); 148. return str; 149. } 150. 151. //获取当月第一天 152. public String getFirstDayOfMonth(){ 153. String str = ""; 154. SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); 155. 156. Calendar lastDate = Calendar.getInstance(); 157. lastDate.set(Calendar.DATE,1);//设为当前月的1号 158. str=sdf.format(lastDate.getTime()); 159. return str; 160. } 161. 162. // 获得本周星期日的日期 163. public String getCurrentWeekday() { 164. weeks = 0; 165. int mondayPlus = this.getMondayPlus(); 166. GregorianCalendar currentDate = new GregorianCalendar(); 167. currentDate.add(GregorianCalendar.DATE, mondayPlus+6); 168. Date monday = currentDate.getTime(); 169. 170. DateFormat df = DateFormat.getDateInstance(); 171. String preMonday = df.format(monday); 172. return preMonday; 173. } 174. 175. 176. //获取当天时间 177. public String getNowTime(String dateformat){ 178. Date now = new Date(); 179. SimpleDateFormat dateFormat = new SimpleDateFormat(dateformat);//可以方便地修改日期格式 180. String hehe = dateFormat.format(now); 181. return hehe; 182. } 183. 184. // 获得当前日期与本周日相差的天数 185. private int getMondayPlus() { 186. Calendar cd = Calendar.getInstance(); 187. // 获得今天是一周的第几天,星期日是第一天,星期二是第二天...... 188. int dayOfWeek = cd.get(Calendar.DAY_OF_WEEK)-1; //因为按中国礼拜一作为第一天所以这里减1 189. if (dayOfWeek == 1) { 190. return 0; 191. } else { 192. return 1 - dayOfWeek; 193. } 194. } 195. 196. //获得本周一的日期 197. public String getMondayOFWeek(){ 198. weeks = 0; 199. int mondayPlus = this.getMondayPlus(); 200. GregorianCalendar currentDate = new GregorianCalendar(); 201. currentDate.add(GregorianCalendar.DATE, mondayPlus); 202. Date monday = currentDate.getTime(); 203. 204. DateFormat df = DateFormat.getDateInstance(); 205. String preMonday = df.format(monday); 206. return preMonday; 207. } 208. 209. //获得相应周的周六的日期 210. public String getSaturday() { 211. int mondayPlus = this.getMondayPlus(); 212. GregorianCalendar currentDate = new GregorianCalendar(); 213. currentDate.add(GregorianCalendar.DATE, mondayPlus + 7 * weeks + 6); 214. Date monday = currentDate.getTime(); 215. DateFormat df = DateFormat.getDateInstance(); 216. String preMonday = df.format(monday); 217. return preMonday; 218. } 219. 220. // 获得上周星期日的日期 221. public String getPreviousWeekSunday() { 222. weeks=0; 223. weeks--; 224. int mondayPlus = this.getMondayPlus(); 225. GregorianCalendar currentDate = new GregorianCalendar(); 226. currentDate.add(GregorianCalendar.DATE, mondayPlus+weeks); 227. Date monday = currentDate.getTime(); 228. DateFormat df = DateFormat.getDateInstance(); 229. String preMonday = df.format(monday); 230. return preMonday; 231. } 232. 233. // 获得上周星期一的日期 234. public String getPreviousWeekday() { 235. weeks--; 236. int mondayPlus = this.getMondayPlus(); 237. GregorianCalendar currentDate = new GregorianCalendar(); 238. currentDate.add(GregorianCalendar.DATE, mondayPlus + 7 * weeks); 239. Date monday = currentDate.getTime(); 240. DateFormat df = DateFormat.getDateInstance(); 241. String preMonday = df.format(monday); 242. return preMonday; 243. } 244. 245. // 获得下周星期一的日期 246. public String getNextMonday() { 247. weeks++; 248. int mondayPlus = this.getMondayPlus(); 249. GregorianCalendar currentDate = new GregorianCalendar(); 250. currentDate.add(GregorianCalendar.DATE, mondayPlus + 7); 251. Date monday = currentDate.getTime(); 252. DateFormat df = DateFormat.getDateInstance(); 253. String preMonday = df.format(monday); 254. return preMonday; 255. } 256. 257. // 获得下周星期日的日期 258. public String getNextSunday() { 259. 260. int mondayPlus = this.getMondayPlus(); 261. GregorianCalendar currentDate = new GregorianCalendar(); 262. currentDate.add(GregorianCalendar.DATE, mondayPlus + 7+6); 263. Date monday = currentDate.getTime(); 264. DateFormat df = DateFormat.getDateInstance(); 265. String preMonday = df.format(monday); 266. return preMonday; 267. } 268. 269. 270. 271. private int getMonthPlus(){ 272. Calendar cd = Calendar.getInstance(); 273. int monthOfNumber = cd.get(Calendar.DAY_OF_MONTH); 274. cd.set(Calendar.DATE, 1);//把日期设置为当月第一天 275. cd.roll(Calendar.DATE, -1);//日期回滚一天,也就是最后一天 276. MaxDate=cd.get(Calendar.DATE); 277. if(monthOfNumber == 1){ 278. return -MaxDate; 279. }else{ 280. return 1-monthOfNumber; 281. } 282. } 283. 284. //获得上月最后一天的日期 285. public String getPreviousMonthEnd(){ 286. String str = ""; 287. SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); 288. 289. Calendar lastDate = Calendar.getInstance(); 290. lastDate.add(Calendar.MONTH,-1);//减一个月 291. lastDate.set(Calendar.DATE, 1);//把日期设置为当月第一天 292. lastDate.roll(Calendar.DATE, -1);//日期回滚一天,也就是本月最后一天 293. str=sdf.format(lastDate.getTime()); 294. return str; 295. } 296. 297. //获得下个月第一天的日期 298. public String getNextMonthFirst(){ 299. String str = ""; 300. SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); 301. 302. Calendar lastDate = Calendar.getInstance(); 303. lastDate.add(Calendar.MONTH,1);//减一个月 304. lastDate.set(Calendar.DATE, 1);//把日期设置为当月第一天 305. str=sdf.format(lastDate.getTime()); 306. return str; 307. } 308. 309. //获得下个月最后一天的日期 310. public String getNextMonthEnd(){ 311. String str = ""; 312. SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); 313. 314. Calendar lastDate = Calendar.getInstance(); 315. lastDate.add(Calendar.MONTH,1);//加一个月 316. lastDate.set(Calendar.DATE, 1);//把日期设置为当月第一天 317. lastDate.roll(Calendar.DATE, -1);//日期回滚一天,也就是本月最后一天 318. str=sdf.format(lastDate.getTime()); 319. return str; 320. } 321. 322. //获得明年最后一天的日期 323. public String getNextYearEnd(){ 324. String str = ""; 325. SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); 326. 327. Calendar lastDate = Calendar.getInstance(); 328. lastDate.add(Calendar.YEAR,1);//加一个年 329. lastDate.set(Calendar.DAY_OF_YEAR, 1); 330. lastDate.roll(Calendar.DAY_OF_YEAR, -1); 331. str=sdf.format(lastDate.getTime()); 332. return str; 333. } 334. 335. //获得明年第一天的日期 336. public String getNextYearFirst(){ 337. String str = ""; 338. SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); 339. 340. Calendar lastDate = Calendar.getInstance(); 341. lastDate.add(Calendar.YEAR,1);//加一个年 342. lastDate.set(Calendar.DAY_OF_YEAR, 1); 343. str=sdf.format(lastDate.getTime()); 344. return str; 345. 346. } 347. 348. //获得本年有多少天 349. private int getMaxYear(){ 350. Calendar cd = Calendar.getInstance(); 351. cd.set(Calendar.DAY_OF_YEAR,1);//把日期设为当年第一天 352. cd.roll(Calendar.DAY_OF_YEAR,-1);//把日期回滚一天。 353. int MaxYear = cd.get(Calendar.DAY_OF_YEAR); 354. return MaxYear; 355. } 356. 357. private int getYearPlus(){ 358. Calendar cd = Calendar.getInstance(); 359. int yearOfNumber = cd.get(Calendar.DAY_OF_YEAR);//获得当天是一年中的第几天 360. cd.set(Calendar.DAY_OF_YEAR,1);//把日期设为当年第一天 361. cd.roll(Calendar.DAY_OF_YEAR,-1);//把日期回滚一天。 362. int MaxYear = cd.get(Calendar.DAY_OF_YEAR); 363. if(yearOfNumber == 1){ 364. return -MaxYear; 365. }else{ 366. return 1-yearOfNumber; 367. } 368. } 369. //获得本年第一天的日期 370. public String getCurrentYearFirst(){ 371. int yearPlus = this.getYearPlus(); 372. GregorianCalendar currentDate = new GregorianCalendar(); 373. currentDate.add(GregorianCalendar.DATE,yearPlus); 374. Date yearDay = currentDate.getTime(); 375. DateFormat df = DateFormat.getDateInstance(); 376. String preYearDay = df.format(yearDay); 377. return preYearDay; 378. } 379. 380. 381. //获得本年最后一天的日期 * 382. public String getCurrentYearEnd(){ 383. Date date = new Date(); 384. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy");//可以方便地修改日期格式 385. String years = dateFormat.format(date); 386. return years+"-12-31"; 387. } 388. 389. 390. //获得上年第一天的日期 * 391. public String getPreviousYearFirst(){ 392. Date date = new Date(); 393. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy");//可以方便地修改日期格式 394. String years = dateFormat.format(date); int years_value = Integer.parseInt(years); 395. years_value--; 396. return years_value+"-1-1"; 397. } 398. 399. //获得上年最后一天的日期 400. public String getPreviousYearEnd(){ 401. weeks--; 402. int yearPlus = this.getYearPlus(); 403. GregorianCalendar currentDate = new GregorianCalendar(); 404. currentDate.add(GregorianCalendar.DATE,yearPlus+MaxYear*weeks+(MaxYear-1)); 405. Date yearDay = currentDate.getTime(); 406. DateFormat df = DateFormat.getDateInstance(); 407. String preYearDay = df.format(yearDay); 408. getThisSeasonTime(11); 409. return preYearDay; 410. } 411. 412. //获得本季度 413. public String getThisSeasonTime(int month){ 414. int array[][] = {{1,2,3},{4,5,6},{7,8,9},{10,11,12}}; 415. int season = 1; 416. if(month>=1&&month<=3){ 417. season = 1; 418. } 419. if(month>=4&&month<=6){ 420. season = 2; 421. } 422. if(month>=7&&month<=9){ 423. season = 3; 424. } 425. if(month>=10&&month<=12){ 426. season = 4; 427. } 428. int start_month = array[season-1][0]; 429. int end_month = array[season-1][2]; 430. 431. Date date = new Date(); 432. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy");//可以方便地修改日期格式 433. String years = dateFormat.format(date); 434. int years_value = Integer.parseInt(years); 435. 436. int start_days =1;//years+"-"+String.valueOf(start_month)+"-1";//getLastDayOfMonth(years_value,start_month); 437. int end_days = getLastDayOfMonth(years_value,end_month); 438. String seasonDate = years_value+"-"+start_month+"-"+start_days+";"+years_value+"-"+end_month+"-"+end_days; 439. return seasonDate; 440. 441. } 442. 443. /** 444. * 获取某年某月的最后一天 445. * @param year 年 446. * @param month 月 447. * @return 最后一天 448. */ 449. private int getLastDayOfMonth(int year, int month) { 450. if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 451. || month == 10 || month == 12) { 452. return 31; 453. } 454. if (month == 4 || month == 6 || month == 9 || month == 11) { 455. return 30; 456. } 457. if (month == 2) { 458. if (isLeapYear(year)) { 459. return 29; 460. } else { 461. return 28; 462. } 463. } 464. return 0; 465. } 466. /** 467. * 是否闰年 468. * @param year 年 469. * @return 470. */ 471. public boolean isLeapYear(int year) { 472. return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); 473. } 474. }