DateKit.java 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package com.hywa.banktest.kit;
  2. import java.text.ParseException;
  3. import java.text.SimpleDateFormat;
  4. import java.util.Calendar;
  5. import java.util.Date;
  6. /**
  7. * 日期工具类
  8. * @author gd_xbb
  9. */
  10. public class DateKit {
  11. /**默认格式(2016-08-20)*/
  12. public static final String DEFAULT_FORMAT = "yyyy-MM-dd";
  13. /**详细日期格式(2016-08-20 17:30:30)*/
  14. public static final String DETAIL = "yyyy-MM-dd HH:mm:ss";
  15. /**带/的日期格式(2016/08/20)*/
  16. public static final String FORMAT_SLASH = "yyyy/MM/dd";
  17. /**中文的日期格式(2016年08月20日)*/
  18. public static final String CHINESE_FORMAT = "yyyy年MM月dd日";
  19. public static final String CHINESE_FORMAT_DETAIL = "yyyy年MM月dd日 HH:mm:ss";
  20. /*一天 时间毫秒数*/
  21. public static final long ONE_DAY_MILLIS = 1000*3600*24;
  22. /**
  23. * 将Date类型转换为字符串类型
  24. *
  25. * @param date
  26. * @param format
  27. * @return
  28. */
  29. public static String formatDate(Date date, String format){
  30. if (null == date){
  31. return null;
  32. }
  33. if (StringKit.isEmpty(format)){
  34. format = DEFAULT_FORMAT;
  35. }
  36. SimpleDateFormat sdf = new SimpleDateFormat(format);
  37. return sdf.format(date);
  38. }
  39. /**
  40. * 将String类型转换为date类型
  41. *
  42. * @param dateStr
  43. * @param format
  44. * @return
  45. * @throws ParseException
  46. */
  47. public static Date stringToDate(String dateStr, String format) throws ParseException{
  48. if (StringKit.isEmpty(dateStr)){
  49. return null;
  50. }
  51. if (StringKit.isEmpty(format)){
  52. format = DEFAULT_FORMAT;
  53. }
  54. SimpleDateFormat sdf = new SimpleDateFormat(format);
  55. return sdf.parse(dateStr);
  56. }
  57. /**
  58. * 得到本月最开始的时间
  59. *
  60. * @return
  61. */
  62. public static Date currentMonthStart(){
  63. Calendar calendar = Calendar.getInstance();
  64. calendar.set(Calendar.DAY_OF_MONTH, 1);
  65. calendar.set(Calendar.HOUR_OF_DAY, 0);
  66. calendar.set(Calendar.MINUTE, 0);
  67. calendar.set(Calendar.SECOND, 0);
  68. return calendar.getTime();
  69. }
  70. /**
  71. * 得到本月最后一刻时间
  72. * @return
  73. */
  74. public static Date currentMonthEnd(){
  75. Calendar calendar = Calendar.getInstance();
  76. calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
  77. calendar.set(Calendar.HOUR_OF_DAY, 23);
  78. calendar.set(Calendar.MINUTE, 59);
  79. calendar.set(Calendar.SECOND, 59);
  80. return calendar.getTime();
  81. }
  82. /**
  83. * 得到本年年份
  84. * @return
  85. */
  86. public static int currentYear(){
  87. Calendar calendar = Calendar.getInstance();
  88. return calendar.get(Calendar.YEAR);
  89. }
  90. /**
  91. * 通过生日得到年龄
  92. * @param birthDate
  93. * @return
  94. */
  95. public static int getAge(Date birthDate) {
  96. if (birthDate == null)
  97. throw new RuntimeException("出生日期不能为null");
  98. int age = 0;
  99. Date now = new Date();
  100. SimpleDateFormat format_y = new SimpleDateFormat("yyyy");
  101. SimpleDateFormat format_M = new SimpleDateFormat("MM");
  102. String birth_year = format_y.format(birthDate);
  103. String this_year = format_y.format(now);
  104. String birth_month = format_M.format(birthDate);
  105. String this_month = format_M.format(now);
  106. // 初步,估算
  107. age = Integer.parseInt(this_year) - Integer.parseInt(birth_year);
  108. // 如果未到出生月份,则age - 1
  109. if (this_month.compareTo(birth_month) < 0)
  110. age -= 1;
  111. if (age < 0)
  112. age = 0;
  113. return age;
  114. }
  115. /**
  116. * 通过日期生成惟一主键
  117. * @return
  118. */
  119. public static synchronized String generateId(){
  120. return System.currentTimeMillis()+"";
  121. }
  122. public static synchronized String generateDateId(){
  123. Date date = new Date();
  124. String dateStr = formatDate(date, "yyyyMMddHHmmss");
  125. return dateStr;
  126. }
  127. public static void main(String args[]){
  128. System.out.println(generateDateId());
  129. }
  130. }