java日期时间转换工具类

java日期时间转换工具类

1.实现日期格式和类型转换
2.获得星期,时,分,秒
3.日期比较
4.生成账号和流水号


Java代码 复制代码 收藏代码
  1. import java.text.SimpleDateFormat;
  2. import java.util.Calendar;
  3. import java.util.Date;
  4. import java.util.regex.Pattern;
  5. /**
  6. * 日期时间转换工具类
  7. */
  8. public class DateUtil {
  9. public static String FILE_NAME = "MMddHHmmssSSS";
  10. public static String DEFAULT_PATTERN = "yyyy-MM-dd";
  11. public static String DIR_PATTERN = "yyyy/MM/dd/";
  12. public static String TIMESTAMP_PATTERN = "yyyy-MM-dd HH:mm:ss";
  13. public static String TIMES_PATTERN = "HH:mm:ss";
  14. public static String NOCHAR_PATTERN = "yyyyMMddHHmmss";
  15. /**
  16. * 获取当前时间戳
  17. *
  18. * @param date
  19. *
  20. * @return
  21. */
  22. public static String formatDefaultFileName() {
  23. return formatDateByFormat(new Date(), FILE_NAME);
  24. }
  25. /**
  26. * 日期转换为字符串
  27. *
  28. * @param date
  29. * 日期
  30. * @param format
  31. * 日期格式
  32. * @return 指定格式的日期字符串
  33. */
  34. public static String formatDateByFormat(Date date, String format) {
  35. String result = "";
  36. if (date != null) {
  37. try {
  38. SimpleDateFormat sdf = new SimpleDateFormat(format);
  39. result = sdf.format(date);
  40. } catch (Exception ex) {
  41. ex.printStackTrace();
  42. }
  43. }
  44. return result;
  45. }
  46. /**
  47. * 转换为默认格式(yyyy-MM-dd)的日期字符串
  48. *
  49. * @param date
  50. *
  51. * @return
  52. */
  53. public static String formatDefaultDate(Date date) {
  54. return formatDateByFormat(date, DEFAULT_PATTERN);
  55. }
  56. /**
  57. * 转换为目录格式(yyyy/MM/dd/)的日期字符串
  58. *
  59. * @param date
  60. *
  61. * @return
  62. */
  63. public static String formatDirDate(Date date) {
  64. return formatDateByFormat(date, DIR_PATTERN);
  65. }
  66. /**
  67. * 转换为完整格式(yyyy-MM-dd HH:mm:ss)的日期字符串
  68. *
  69. * @param date
  70. *
  71. * @return
  72. */
  73. public static String formatTimesTampDate(Date date) {
  74. return formatDateByFormat(date, TIMESTAMP_PATTERN);
  75. }
  76. /**
  77. * 转换为时分秒格式(HH:mm:ss)的日期字符串
  78. *
  79. * @param date
  80. *
  81. * @return
  82. */
  83. public static String formatTimesDate(Date date) {
  84. return formatDateByFormat(date, TIMES_PATTERN);
  85. }
  86. /**
  87. * 转换为时分秒格式(HH:mm:ss)的日期字符串
  88. *
  89. * @param date
  90. *
  91. * @return
  92. */
  93. public static String formatNoCharDate(Date date) {
  94. return formatDateByFormat(date, NOCHAR_PATTERN);
  95. }
  96. /**
  97. * 日期格式字符串转换为日期对象
  98. *
  99. * @param strDate
  100. * 日期格式字符串
  101. * @param pattern
  102. * 日期对象
  103. * @return
  104. */
  105. public static Date parseDate(String strDate, String pattern) {
  106. try {
  107. SimpleDateFormat format = new SimpleDateFormat(pattern);
  108. Date nowDate = format.parse(strDate);
  109. return nowDate;
  110. } catch (Exception e) {
  111. e.printStackTrace();
  112. }
  113. return null;
  114. }
  115. /**
  116. * 字符串转换为默认格式(yyyy-MM-dd)日期对象
  117. *
  118. * @param date
  119. *
  120. * @return
  121. *
  122. * @throws Exception
  123. */
  124. public static Date parseDefaultDate(String date) {
  125. return parseDate(date, DEFAULT_PATTERN);
  126. }
  127. /**
  128. * 字符串转换为完整格式(yyyy-MM-dd HH:mm:ss)日期对象
  129. *
  130. * @param date
  131. *
  132. * @return
  133. *
  134. * @throws Exception
  135. */
  136. public static Date parseTimesTampDate(String date) {
  137. return parseDate(date, TIMESTAMP_PATTERN);
  138. }
  139. /**
  140. * 获得当前时间
  141. *
  142. * @return
  143. */
  144. public static Date getCurrentDate() {
  145. Calendar calendar = Calendar.getInstance();
  146. return calendar.getTime();
  147. }
  148. /**
  149. * sql Date 转 util Date
  150. *
  151. * @param date
  152. * java.sql.Date日期
  153. * @return java.util.Date
  154. */
  155. public static Date parseUtilDate(java.sql.Date date) {
  156. return date;
  157. }
  158. /**
  159. * util Date 转 sql Date
  160. *
  161. * @param date
  162. * java.sql.Date日期
  163. * @return
  164. */
  165. public static java.sql.Date parseSqlDate(Date date) {
  166. return new java.sql.Date(date.getTime());
  167. }
  168. /**
  169. * 获取年份
  170. *
  171. * @param date
  172. *
  173. * @return
  174. */
  175. public static int getYear(Date date) {
  176. Calendar c = Calendar.getInstance();
  177. c.setTime(date);
  178. return c.get(Calendar.YEAR);
  179. }
  180. /**
  181. * 获取月份
  182. *
  183. * @param date
  184. *
  185. * @return
  186. */
  187. public static int getMonth(Date date) {
  188. Calendar c = Calendar.getInstance();
  189. c.setTime(date);
  190. return c.get(Calendar.MONTH) + 1;
  191. }
  192. /**
  193. * 获取星期
  194. *
  195. * @param date
  196. *
  197. * @return
  198. */
  199. public static int getWeek(Date date) {
  200. Calendar c = Calendar.getInstance();
  201. c.setTime(date);
  202. int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
  203. dayOfWeek = dayOfWeek - 1;
  204. if (dayOfWeek == 0) {
  205. dayOfWeek = 7;
  206. }
  207. return dayOfWeek;
  208. }
  209. /**
  210. * 获取日期(多少号)
  211. *
  212. * @param date
  213. *
  214. * @return
  215. */
  216. public static int getDay(Date date) {
  217. Calendar c = Calendar.getInstance();
  218. c.setTime(date);
  219. return c.get(Calendar.DAY_OF_MONTH);
  220. }
  221. /**
  222. * 获取当前时间(小时)
  223. *
  224. * @param date
  225. *
  226. * @return
  227. */
  228. public static int getHour(Date date) {
  229. Calendar c = Calendar.getInstance();
  230. c.setTime(date);
  231. return c.get(Calendar.HOUR_OF_DAY);
  232. }
  233. /**
  234. * 获取当前时间(分)
  235. *
  236. * @param date
  237. *
  238. * @return
  239. */
  240. public static int getMinute(Date date) {
  241. Calendar c = Calendar.getInstance();
  242. c.setTime(date);
  243. return c.get(Calendar.MINUTE);
  244. }
  245. /**
  246. * 获取当前时间(秒)
  247. *
  248. * @param date
  249. *
  250. * @return
  251. */
  252. public static int getSecond(Date date) {
  253. Calendar c = Calendar.getInstance();
  254. c.setTime(date);
  255. return c.get(Calendar.SECOND);
  256. }
  257. /**
  258. * 获取当前毫秒
  259. *
  260. * @param date
  261. *
  262. * @return
  263. */
  264. public static long getMillis(Date date) {
  265. Calendar c = Calendar.getInstance();
  266. c.setTime(date);
  267. return c.getTimeInMillis();
  268. }
  269. /**
  270. * 日期增加
  271. *
  272. * @param date
  273. * Date
  274. *
  275. * @param day
  276. * int
  277. *
  278. * @return Date
  279. */
  280. public static Date addDate(Date date, int day) {
  281. java.util.Calendar c = java.util.Calendar.getInstance();
  282. c.setTimeInMillis(getMillis(date) + ((long) day) * 24 * 3600 * 1000);
  283. return c.getTime();
  284. }
  285. /**
  286. * 日期相减(返回天数)
  287. *
  288. * @param date
  289. * Date
  290. *
  291. * @param date1
  292. * Date
  293. *
  294. * @return int 相差的天数
  295. */
  296. public static int diffDate(Date date, Date date1) {
  297. return (int) ((getMillis(date) - getMillis(date1)) / (24 * 3600 * 1000));
  298. }
  299. /**
  300. * 日期相减(返回秒值)
  301. *
  302. * @param date
  303. * Date
  304. * @param date1
  305. * Date
  306. * @return int
  307. * @author
  308. */
  309. public static Long diffDateTime(Date date, Date date1) {
  310. return (Long) ((getMillis(date) - getMillis(date1)) / 1000);
  311. }
  312. private static String[] randomValues = new String[] { "0", "1", "2", "3",
  313. "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f", "g",
  314. "h", "i", "j", "k", "l", "m", "n", "u", "t", "s", "o", "x", "v",
  315. "p", "q", "r", "w", "y", "z" };
  316. public static String getRandomNumber(int lenght) {
  317. StringBuffer str = new StringBuffer();
  318. for (int i = 0; i < lenght; i++) {
  319. Double number = Math.random() * (randomValues.length - 1);
  320. str.append(randomValues[number.intValue()]);
  321. }
  322. return str.toString();
  323. }
  324. /**
  325. * 生成账号
  326. *
  327. * @param acount
  328. * @return
  329. */
  330. public static String nextAcounnt(String acount) {
  331. String newAcc = "";
  332. if (Integer.parseInt(acount) < 10000) {
  333. Integer newAc = Integer.parseInt(acount) + 1;
  334. if (newAc < 1000) {
  335. int count = String.valueOf(newAc).length();
  336. if (count == 1) {
  337. newAcc = "000" + newAc;
  338. } else if (count == 2) {
  339. newAcc = "00" + newAc;
  340. } else if (count == 3) {
  341. newAcc = "0" + newAc;
  342. }
  343. } else {
  344. newAcc = String.valueOf(newAc);
  345. }
  346. } else {
  347. newAcc = acount;
  348. }
  349. return newAcc;
  350. }
  351. public static boolean isNumeric1(String str) {
  352. if (str != null && !"".equals(str) && str.length()<=9) {
  353. Pattern pattern = Pattern.compile("[0-9]*");
  354. return pattern.matcher(str).matches();
  355. } else {
  356. return false;
  357. }
  358. }
  359. /**
  360. * 生成流水号
  361. *
  362. * @param t
  363. * 流水号位数
  364. * @return 流水号
  365. */
  366. public static String getSequenceNumber(int t) {
  367. Date d = new Date();
  368. SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmss");
  369. String str = sdf.format(d);
  370. String haomiao = String.valueOf(System.nanoTime());
  371. str = str + haomiao.substring(haomiao.length() - 6, haomiao.length());
  372. return str.substring(str.length() - t, str.length());
  373. }
  374. }  

转载于:https://www.cnblogs.com/shhaoran/archive/2013/02/08/2924494.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值