java 时间转化工具类

傷城~ 2022-08-13 11:55 284阅读 0赞
  1. package cn.my.utils;
  2. import java.text.ParseException;
  3. import java.text.ParsePosition;
  4. import java.text.SimpleDateFormat;
  5. import java.util.Calendar;
  6. import java.util.Date;
  7. import java.util.HashMap;
  8. import java.util.Map;
  9. /** 时间工具类<p>
  10. *
  11. * @version 1.0<br>
  12. */
  13. public class DateUtil {
  14. /**
  15. * 获得两个时间字符的时间差
  16. * @param startTime
  17. * @param endTime
  18. * @return
  19. * @throws ParseException
  20. * Integer
  21. */
  22. public static Integer distanceBySE(String startTime,String endTime){
  23. SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
  24. //通过字符串创建两个日期对象
  25. Date firstDate = null;
  26. Date secondDate = null;
  27. try {
  28. firstDate = sdf.parse(endTime);
  29. secondDate = sdf.parse(startTime);
  30. } catch (ParseException e) {
  31. e.printStackTrace();
  32. }
  33. //得到两个日期对象的总毫秒数
  34. long firstDateMilliSeconds = firstDate.getTime();
  35. long secondDateMilliSeconds = secondDate.getTime();
  36. //得到两者之差
  37. long firstMinusSecond = firstDateMilliSeconds - secondDateMilliSeconds;
  38. //毫秒转为秒
  39. long milliSeconds = firstMinusSecond;
  40. int totalSeconds = (int)(milliSeconds / 1000);
  41. //得到总天数
  42. Integer days = totalSeconds / (3600*24);
  43. return days;
  44. }
  45. /**
  46. * 把格式为 年-月-日的时间转成年月日<p>
  47. * @param data
  48. * @return <p>
  49. * String
  50. */
  51. public static String dataStr(String data){
  52. String[] str = data.split("-");
  53. String str1= "";
  54. for(int i =0 ;i<str.length; i++){
  55. str1= str1+str[i];
  56. }
  57. if(str1.length() >0){
  58. return str1.trim();
  59. }
  60. return null;
  61. }
  62. /**
  63. * 把格式为yyyy-MM-dd的转化成yyyyMMdd<p>
  64. * @param date
  65. * @return <p>
  66. * String
  67. */
  68. public static String dateByNum(String date){
  69. SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
  70. return format.format(strToDate(date,"yyyy-MM-dd"));
  71. }
  72. /**
  73. * 把格式为yyyy-MM-dd的转化成yyyyMMdd<p>
  74. * @param date
  75. * @return <p>
  76. * String
  77. */
  78. public static String dateMonthByNum(String date){
  79. SimpleDateFormat format = new SimpleDateFormat("yyyyMM");
  80. return format.format(strToDate(date,"yyyy-MM"));
  81. }
  82. /**
  83. * 把格式为yyyy-MM的时间获取开始时间和结算时间<p>
  84. * @param monthDate
  85. * @return 20140401_20140430<p>
  86. * String
  87. */
  88. public static String monthByStartAndEndTime(String monthDate){
  89. String[] str =monthDate.split("-");
  90. //日历
  91. Calendar c = Calendar.getInstance();
  92. //设置这个月的第一天
  93. c.set(Integer.parseInt(str[0]), Integer.parseInt(str[1])-1, 1);
  94. //获取这个月一共有多少天
  95. int lastDay=c.getActualMaximum(Calendar.DAY_OF_MONTH);
  96. String date =str[0]+str[1]+"01"+"_"+str[0]+str[1]+lastDay+"";
  97. return date;
  98. }
  99. public static String getWeekDate(String date){
  100. String[] str = date.split("-");
  101. return weekGetDate(str[0],str[1],Integer.parseInt(str[2]));
  102. }
  103. /**
  104. * 获得这周的开始时间
  105. * @param year
  106. * @param month
  107. * @param week
  108. * @return
  109. * Date
  110. */
  111. public static Date getProParamDate(String year , String month , int week){
  112. String time = weekGetDate(year, month, week);
  113. String[] times = time.split("_");
  114. SimpleDateFormat formatDate = new SimpleDateFormat("yyyyMMdd");
  115. Date datatime = null;
  116. try {
  117. datatime = formatDate.parse(times[0]);
  118. } catch (ParseException e) {
  119. e.printStackTrace();
  120. }
  121. return datatime;
  122. }
  123. /**
  124. * 根据年月周获取开始时间和结束时间<p>
  125. * @param year 年
  126. * @param month 月
  127. * @param week 周
  128. * @return 开始时间和结算时间的字符串<p>
  129. * String
  130. */
  131. public static String weekGetDate(String year,String month,int week){
  132. // 存储周数所对应的天数
  133. Map<Integer, String> map =new HashMap<Integer, String>();
  134. //日历
  135. Calendar c = Calendar.getInstance();
  136. //设置这个月的第一天
  137. c.set(Integer.parseInt(year), Integer.parseInt(month)-1, 1);
  138. //获取这个月的第一天
  139. int firstWeek= c.get(Calendar.DAY_OF_WEEK)-1;
  140. //获取这个月一共有多少天
  141. int lastDay=c.getActualMaximum(Calendar.DAY_OF_MONTH);
  142. double a;
  143. if(firstWeek ==0){
  144. a =7;
  145. }else{
  146. a=firstWeek;
  147. }
  148. //计算这个月一共有多少周
  149. int weekNum = (int) Math.ceil((lastDay-(7-a+1))/7)+1;
  150. double startDay = 1;
  151. //循环获取这月的日期
  152. for(int i=1;i<=weekNum;i++){
  153. if(i==1){
  154. map.put(i, year+"-"+month+"-"+(int)startDay+"_"+year+"-"+month+"-"+(int)((7-a)+1));
  155. startDay = (7-a)+1;
  156. }else{
  157. map.put(i, year+"-"+month+"-"+(int)(startDay+1)+"_"+year+"-"+month+"-"+(int)(startDay+7));
  158. startDay= startDay+7;
  159. if(startDay>lastDay){
  160. startDay= startDay-7;
  161. map.put(i, year+"-"+month+"-"+(int)(startDay+1)+"_"+year+"-"+month+"-"+(int)lastDay);
  162. break;
  163. }
  164. }
  165. }
  166. //根据周数获取开始时间和结算时间
  167. return map.get(week);
  168. }
  169. /**
  170. * 将指定格式的字符串转换为日期类型,如果格式与字符串不匹配,则返回null。
  171. * @param dateStr 待转换的字符串
  172. * @param format 日期格式
  173. * @return 转换后的日期对象
  174. */
  175. public static Date strToDate(String dateStr, String format) {
  176. //构造制定格式format的SimpleDateFormat对象。
  177. SimpleDateFormat formatter = new SimpleDateFormat(format);
  178. //进行转换,这里要传入new ParsePosition(0),否则是抛出异常,而不是返回null
  179. return formatter.parse(dateStr, new ParsePosition(0));
  180. }
  181. public static String strToString(String str){
  182. String strs =null;
  183. if(str.length()==4){
  184. strs ="00"+str;
  185. }else if(str.length()==5){
  186. strs ="0"+str;
  187. }else if(str.length()==3){
  188. strs ="000"+str;
  189. }else if(str.length()==2){
  190. strs ="0000"+str;
  191. }else if(str.length()==1){
  192. strs ="00000"+str;
  193. }else if(str.length()==0){
  194. strs ="000000";
  195. }
  196. else{
  197. strs =str;
  198. }
  199. return strs;
  200. }
  201. public static Date getSpecifiedDay(Date date){
  202. Calendar c = Calendar.getInstance();
  203. c.setTime(date);
  204. int day=c.get(Calendar.DATE);
  205. c.set(Calendar.DATE,day-1);
  206. Date endDate = c.getTime();
  207. return endDate;
  208. }
  209. public static Date strDate(String str,String format){
  210. Date date =null;
  211. if(str.length()==4){
  212. date = DateUtil.strToDate("00"+str,format);
  213. }else if(str.length()==5){
  214. date = DateUtil.strToDate("0"+str,format);
  215. }
  216. else{
  217. date = DateUtil.strToDate(str,format);
  218. }
  219. return date;
  220. }
  221. /**
  222. * 一个日期加几天后的日期<p>
  223. * @param time 要加以前的日期
  224. * @param addDayNum 要加的天数
  225. * @return 已经加过的日期<p>
  226. * Date
  227. */
  228. public static Date getDateAddDay(String time,int addDayNum){
  229. try {
  230. Date date= (new SimpleDateFormat("yyyyMMdd")).parse(time);
  231. Calendar cal = Calendar.getInstance();
  232. cal.setTime(date);
  233. cal.add(Calendar.DATE, addDayNum);
  234. return cal.getTime();
  235. } catch (ParseException e) {
  236. return null;
  237. }
  238. }
  239. /**
  240. * 根据省份证号码获取出生日期
  241. * @param iDCard 身份证号码
  242. * @return 出生日期
  243. */
  244. public static Date getBirthDayByiDCard(String iDCard){
  245. String strDate = null;
  246. if(iDCard.length()==18){
  247. strDate =iDCard.substring(6,14);
  248. }else{
  249. strDate = iDCard.substring(6,12);
  250. strDate = "19"+strDate;
  251. }
  252. return strDate!= null? strToDate(strDate,"yyyyMMdd"):null;
  253. }
  254. }

发表评论

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

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

相关阅读

    相关 Java 时间工具

    借鉴网上资源与工作中经验整理的时间工具类(主要针对java.util.Date的使用,java8后的时间工具下期整理),欢迎大家一起完善补充。 import

    相关 Java时间工具

    该时间工具类功能:时间戳格式化至毫秒、时间戳格式化至秒、时间戳格式化至日、时间戳格式化成时和分、Date对象格式化至毫秒、Date对象格式化至秒、Date对象格式化至日、Da

    相关 java时间工具

    在项目中,很多地方需要根据时间获取相应的数据,将时间格式化,或者时间比较等相关操作。一个良好的工具类不仅可以减少代码冗余,还能促进业务处理,加快进度。 /