ssslinppp

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
主要介绍了Calendar类的使用

输出

  1. * 时间格式化
  2. * 当前时间:2016-12-02 16:46:27.079
  3. *
  4. * 转换:String-->Date-->Calendar
  5. * 设置时间(方式1):2014-08-01 09:18:34.123
  6. * 设置时间(方式2):2016-12-01 19:08:54.123
  7. *
  8. * ----YEAR-MONTH----
  9. * YEAR:2016
  10. * MONTH:11
  11. * ----Day-----
  12. * 本月第几天:1
  13. * 本周几:5
  14. * 今年的第 N :336
  15. * DAY_OF_WEEK_IN_MONTH:1
  16. * ----Hour-----
  17. * Hour:7
  18. * HOUR_OF_DAY:19
  19. * ----MINUTE-----
  20. * MINUTE:8
  21. *
  22. * 向后推迟一个月(方式1):2017-01-01 19:08:54.123
  23. * 向后推迟一个月(方式2):2017-02-01 19:08:54.123
  24. * 向后推迟一小时(方式1):2017-02-01 20:08:54.123
  25. * 向前推迟一小时(方式2):2017-02-01 19:08:54.123
  26. * 设置为30分钟:2017-02-01 19:30:00.123
  27. *
  28. * 开始时间:2014-12-15 16:46:27.116,毫秒数:1418633187116
  29. * 结束时间:2016-12-05 12:15:35.117,毫秒数:1480911335117
  30. * 相差天数:721
  31. *
  32. * 本月最大天数:31

程序
  1. package com.sssppp.DateTime;
  2. import java.text.ParseException;
  3. import java.text.SimpleDateFormat;
  4. import java.util.Calendar;
  5. import java.util.Date;
  6. public class CalendarExample {
  7. public static void main(String[] args) {
  8. Calendar cal = Calendar.getInstance();
  9. System.out.println("时间格式化");
  10. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
  11. System.out.println("当前时间:" + sdf.format(cal.getTime()));
  12. System.out.println();
  13. try {
  14. System.out.println("转换:String-->Date-->Calendar");
  15. cal.setTime(sdf.parse("2014-8-1 9:18:34.123"));
  16. System.out.println("设置时间(方式1):" + sdf.format(cal.getTime()));
  17. } catch (ParseException e) {
  18. e.printStackTrace();
  19. }
  20. //2016-12-01 19:08:54
  21. cal.set(2016, Calendar.DECEMBER, 1, 19, 8, 54);
  22. System.out.println("设置时间(方式2):" +sdf.format(cal.getTime()));
  23. System.out.println();
  24. System.out.println("----YEAR-MONTH----");
  25. System.out.println("YEAR:" + cal.get(Calendar.YEAR));
  26. System.out.println("MONTH:" + cal.get(Calendar.MONTH));
  27. System.out.println("----Day-----");
  28. System.out.println("本月第几天:" + cal.get(Calendar.DAY_OF_MONTH));
  29. System.out.println("本周几:" + cal.get(Calendar.DAY_OF_WEEK));
  30. System.out.println("今年的第 N天 :" + cal.get(Calendar.DAY_OF_YEAR));
  31. System.out.println("DAY_OF_WEEK_IN_MONTH:"
  32. + cal.get(Calendar.DAY_OF_WEEK_IN_MONTH));
  33. System.out.println("----Hour-----");
  34. System.out.println("Hour:" + cal.get(Calendar.HOUR));
  35. System.out.println("HOUR_OF_DAY:" + cal.get(Calendar.HOUR_OF_DAY));
  36. System.out.println("----MINUTE-----");
  37. System.out.println("MINUTE:" + cal.get(Calendar.MINUTE));
  38. System.out.println();
  39. cal.set(Calendar.MONTH,cal.get(Calendar.MONTH)+1);
  40. System.out.println("向后推迟一个月(方式1):" + sdf.format(cal.getTime()));
  41. cal.add(Calendar.MONDAY, 1);
  42. System.out.println("向后推迟一个月(方式2):" + sdf.format(cal.getTime()));
  43. cal.set(Calendar.HOUR,cal.get(Calendar.HOUR)+1);
  44. System.out.println("向后推迟一小时(方式1):" + sdf.format(cal.getTime()));
  45. cal.add(Calendar.HOUR, -1);
  46. System.out.println("向前推迟一小时(方式2):" + sdf.format(cal.getTime()));
  47. cal.set(Calendar.MINUTE,30);
  48. cal.set(Calendar.SECOND,00);
  49. System.out.println("设置为30分钟:" + sdf.format(cal.getTime()));
  50. cal.getTimeInMillis();
  51. System.out.println();
  52. Calendar calStart = Calendar.getInstance();
  53. calStart.set(2014, Calendar.DECEMBER, 15);
  54. System.out.println("开始时间:" + sdf.format(calStart.getTimeInMillis())
  55. + ",毫秒数:" + calStart.getTimeInMillis());
  56. Calendar calEnd = Calendar.getInstance();
  57. calEnd.set(2016, Calendar.DECEMBER, 5, 12, 15, 35);
  58. System.out.println("结束时间:" + sdf.format(calEnd.getTimeInMillis())
  59. + ",毫秒数:" + calEnd.getTimeInMillis());
  60. int day =getDaysBetween(calEnd,calStart);
  61. System.out.println("相差天数:" + day);
  62. //计算某一月份的最大天数
  63. System.out.println();
  64. Calendar time=Calendar.getInstance();
  65. time.clear(); // 在使用set方法之前,必须先clear一下,否则很多信息会继承自系统当前时间
  66. time.set(Calendar.YEAR, 2016);
  67. int dayCount = time.getActualMaximum(Calendar.DAY_OF_MONTH);// 本月份的天数
  68. System.out.println("本月最大天数:" + dayCount);
  69. }
  70. /**
  71. * 计算两个时间之间相隔天数(不够精确)
  72. *
  73. * @param startday
  74. * 开始时间
  75. * @param endday
  76. * 结束时间
  77. * @return
  78. */
  79. public static int getIntervalDays(Calendar startday, Calendar endday) {
  80. // 确保startdayendday之前
  81. if (startday.after(endday)) {
  82. Calendar cal = startday;
  83. startday = endday;
  84. endday = cal;
  85. }
  86. // 分别得到两个时间的毫秒数
  87. long sl = startday.getTimeInMillis();
  88. long el = endday.getTimeInMillis();
  89. long ei = el - sl;
  90. // 根据毫秒数计算间隔天数
  91. return (int) (ei / (1000 * 60 * 60 * 24));
  92. }
  93. /**
  94. * 计算两个时间之间相隔天数(不够精确)
  95. *
  96. * @param startday
  97. * 开始时间
  98. * @param endday
  99. * 结束时间
  100. * @return
  101. */
  102. public int getIntervalDays(Date startday, Date endday) {
  103. // 确保startdayendday之前
  104. if (startday.after(endday)) {
  105. Date cal = startday;
  106. startday = endday;
  107. endday = cal;
  108. }
  109. // 分别得到两个时间的毫秒数
  110. long sl = startday.getTime();
  111. long el = endday.getTime();
  112. long ei = el - sl;
  113. // 根据毫秒数计算间隔天数
  114. return (int) (ei / (1000 * 60 * 60 * 24));
  115. }
  116. /**
  117. * 精确计算两个日期之间的天数
  118. *
  119. * @param d1
  120. * @param d2
  121. * @return
  122. */
  123. public static int getDaysBetween(Calendar d1, Calendar d2) {
  124. if (d1.after(d2)) { // swap dates so that d1 is start and d2 is end
  125. java.util.Calendar swap = d1;
  126. d1 = d2;
  127. d2 = swap;
  128. }
  129. int days = d2.get(Calendar.DAY_OF_YEAR) - d1.get(Calendar.DAY_OF_YEAR);
  130. int y2 = d2.get(Calendar.YEAR);
  131. if (d1.get(Calendar.YEAR) != y2) {
  132. d1 = (Calendar) d1.clone();
  133. do {
  134. days += d1.getActualMaximum(Calendar.DAY_OF_YEAR);// 得到当年的实际天数
  135. d1.add(Calendar.YEAR, 1);
  136. } while (d1.get(Calendar.YEAR) != y2);
  137. }
  138. return days;
  139. }
  140. }












posted on 2016-12-02 16:52  ssslinppp  阅读(1354)  评论(0编辑  收藏  举报