当前位置: 代码迷 >> Web前端 >> Java获取各种惯用时间方法
  详细解决方案

Java获取各种惯用时间方法

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