java 日期与周数月数互相转换方法

你的名字 2022-05-11 03:20 378阅读 0赞
  1. import java.text.ParseException;
  2. import java.text.SimpleDateFormat;
  3. import java.util.Calendar;
  4. import java.util.Date;
  5. /**
  6. * data:2018-6-22
  7. * author:qixin
  8. * descriptor:日期常用方法
  9. * */
  10. public class DateUtil {
  11. private static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  12. private static SimpleDateFormat dayFormat = new SimpleDateFormat("yyyy-MM-dd");
  13. private static SimpleDateFormat weekFormat = new SimpleDateFormat("yyyy-ww");
  14. private static SimpleDateFormat monthFormat = new SimpleDateFormat("yyyy-MM");
  15. /**
  16. * Turn date into week.
  17. * @author yinfuyuan <yinfuyuan@renrenche.com>
  18. * @param day The date that needs to be converted into the week.
  19. * @return The week corresponding to the date or empty string.
  20. */
  21. public static String dayToWeek(String day) {
  22. try {
  23. Date date = dayFormat.parse(day);
  24. Calendar calendar = Calendar.getInstance();
  25. calendar.setTime(date);
  26. calendar.setFirstDayOfWeek(Calendar.MONDAY);
  27. String year = (new SimpleDateFormat("yyyy")).format(calendar.getTime());
  28. String week = String.valueOf(calendar.get(Calendar.WEEK_OF_YEAR));
  29. return year + "-" + week;
  30. } catch (ParseException pe) {
  31. return "";
  32. }
  33. }
  34. /**
  35. * Turn date into month.
  36. * @author yinfuyuan <yinfuyuan@renrenche.com>
  37. * @param day The date that needs to be converted into the month.
  38. * @return The month corresponding to the date or empty string.
  39. */
  40. public static String dayToMonth(String day) {
  41. try {
  42. Date date = dayFormat.parse(day);
  43. Calendar calendar = Calendar.getInstance();
  44. calendar.setTime(date);
  45. return monthFormat.format(calendar.getTime());
  46. } catch (ParseException pe) {
  47. return "";
  48. }
  49. }
  50. /**
  51. * Turn first day of the week into day.
  52. * @author yinfuyuan <yinfuyuan@renrenche.com>
  53. * @param week The week that needs to be converted into a day.
  54. * @return The day corresponding to first of the week or empty string.
  55. */
  56. public static String weekToFirstDay(String week) {
  57. try {
  58. Date date = weekFormat.parse(week);
  59. Calendar calendar = Calendar.getInstance();
  60. calendar.setTime(date);
  61. calendar.setFirstDayOfWeek(Calendar.MONDAY);
  62. calendar.add(Calendar.DATE, 1);
  63. return dayFormat.format(calendar.getTime());
  64. } catch (ParseException pe) {
  65. return "";
  66. }
  67. }
  68. /**
  69. * Turn last day of the week into day.
  70. * @author yinfuyuan <yinfuyuan@renrenche.com>
  71. * @param week The week that needs to be converted into a day.
  72. * @return The day corresponding to last day of the week or empty string.
  73. */
  74. public static String weekToLastDay(String week) {
  75. try {
  76. Date date = weekFormat.parse(week);
  77. Calendar calendar = Calendar.getInstance();
  78. calendar.setTime(date);
  79. calendar.add(Calendar.DATE, 7);
  80. calendar.setFirstDayOfWeek(Calendar.MONDAY);
  81. return dayFormat.format(calendar.getTime());
  82. } catch (ParseException pe) {
  83. return "";
  84. }
  85. }
  86. /**
  87. * Turn week into month.
  88. * @author yinfuyuan <yinfuyuan@renrenche.com>
  89. * @param week The week that needs to be converted into a month.
  90. * @return The month corresponding to the week or empty string.
  91. */
  92. public static String weekToMonth(String week) {
  93. try {
  94. Date date = weekFormat.parse(week);
  95. Calendar calendar = Calendar.getInstance();
  96. calendar.setTime(date);
  97. calendar.setFirstDayOfWeek(Calendar.MONDAY);
  98. return monthFormat.format(calendar.getTime());
  99. } catch (ParseException pe) {
  100. return "";
  101. }
  102. }
  103. /**
  104. * Turn first day of the month into day.
  105. * @author yinfuyuan <yinfuyuan@renrenche.com>
  106. * @param month The month that needs to be converted into a date.
  107. * @return The first day of the month or empty string.
  108. */
  109. public static String monthToFirstDay(String month) {
  110. try {
  111. Date date = monthFormat.parse(month);
  112. Calendar calendar = Calendar.getInstance();
  113. calendar.setTime(date);
  114. return dayFormat.format(calendar.getTime());
  115. } catch (ParseException pe) {
  116. return "";
  117. }
  118. }
  119. /**
  120. * Turn last day of the month into day.
  121. * @author yinfuyuan <yinfuyuan@renrenche.com>
  122. * @param month The month that needs to be converted into a date.
  123. * @return The last day of the month or empty string.
  124. */
  125. public static String monthToLastDay(String month) {
  126. try {
  127. Date date = monthFormat.parse(month);
  128. Calendar calendar = Calendar.getInstance();
  129. calendar.setTime(date);
  130. calendar.add(Calendar.MONTH, 1);
  131. calendar.add(Calendar.DATE, -1);
  132. return dayFormat.format(calendar.getTime());
  133. } catch (ParseException pe) {
  134. return "";
  135. }
  136. }
  137. /**
  138. * Turn first week of the month into week.
  139. * @author yinfuyuan <yinfuyuan@renrenche.com>
  140. * @param month The month that needs to be converted into a first week.
  141. * @return The first week of the month corresponding to the week or empty string.
  142. */
  143. public static String monthToFirstWeek(String month) {
  144. try {
  145. Date date = monthFormat.parse(month);
  146. Calendar calendar = Calendar.getInstance();
  147. calendar.setTime(date);
  148. return weekFormat.format(calendar.getTime());
  149. } catch (ParseException pe) {
  150. return "";
  151. }
  152. }
  153. /**
  154. * Turn last week of the month into week.
  155. * @author yinfuyuan <yinfuyuan@renrenche.com>
  156. * @param month The month that needs to be converted into a last week.
  157. * @return The last week of the month corresponding to the week or empty string.
  158. */
  159. public static String monthToLastWeek(String month) {
  160. try {
  161. Date date = monthFormat.parse(month);
  162. Calendar calendar = Calendar.getInstance();
  163. calendar.setTime(date);
  164. calendar.add(Calendar.MONTH, 1);
  165. calendar.add(Calendar.DATE, -1);
  166. return weekFormat.format(calendar.getTime());
  167. } catch (ParseException pe) {
  168. return "";
  169. }
  170. }
  171. /**
  172. * 返回天数double类型
  173. * @author qixin
  174. * @param startTime,
  175. * @param endTime,
  176. * @return 返回天数double类型 天数小于1的都归为1(默认1天即汇总)
  177. */
  178. public static Double getDayNum(String startTime,String endTime) {
  179. SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
  180. Double dayNum=1.00d;
  181. try{
  182. Calendar cal = Calendar.getInstance();
  183. cal.setTime(sdf.parse(startTime));
  184. long time1 = cal.getTimeInMillis();
  185. cal.setTime(sdf.parse(endTime));
  186. long time2 = cal.getTimeInMillis();
  187. long between_days=(time2-time1)/(1000*3600*24);
  188. if (between_days>1){
  189. String day=between_days+"";
  190. dayNum=Double.valueOf(day);
  191. }else {
  192. dayNum=1.00d;
  193. }
  194. }catch (Exception e){
  195. e.printStackTrace();
  196. System.out.println(e.getMessage());
  197. }
  198. return dayNum;
  199. }
  200. }

感谢观看

发表评论

表情:
评论列表 (有 0 条评论,378人围观)

还没有评论,来说两句吧...

相关阅读