java实现获取当前年月日 小时 分钟 秒 毫秒

╰+哭是因爲堅強的太久メ 2022-09-07 12:12 366阅读 0赞
  1. package org.fiend.swing.test.filesync;
  2. import java.text.SimpleDateFormat;
  3. import java.util.Calendar;
  4. import java.util.Date;
  5. /**
  6. * @author pelang 2021-08-23 9:46:57
  7. */
  8. public class DateUtil2 {
  9. private static final Calendar calendar = Calendar.getInstance();
  10. /**
  11. * 英文简写(默认)如:2010-12-01
  12. */
  13. public static String FORMAT_SHORT = "yyyy-MM-dd";
  14. /**
  15. * 英文全称 如:2010-12-01 23:15:06
  16. */
  17. public static String FORMAT_LONG = "yyyy-MM-dd HH:mm:ss";
  18. /**
  19. * 精确到毫秒的完整时间 如:yyyy-MM-dd HH:mm:ss.S
  20. */
  21. public static String FORMAT_FULL = "yyyy-MM-dd HH:mm:ss.S";
  22. /**
  23. * 中文简写 如:2010年12月01日
  24. */
  25. public static String FORMAT_SHORT_CN = "yyyy年MM月dd";
  26. /**
  27. * 中文全称 如:2010年12月01日 23时15分06秒
  28. */
  29. public static String FORMAT_LONG_CN = "yyyy年MM月dd日 HH时mm分ss秒";
  30. /**
  31. * 精确到毫秒的完整中文时间
  32. */
  33. public static String FORMAT_FULL_CN = "yyyy年MM月dd日 HH时mm分ss秒SSS毫秒";
  34. /**
  35. * 获取时间戳
  36. */
  37. public static String getTimeString() {
  38. SimpleDateFormat df = new SimpleDateFormat(FORMAT_FULL);
  39. return df.format(calendar.getTime());
  40. }
  41. /**
  42. * 获取日期年份
  43. * @param date 日期
  44. * @return
  45. */
  46. public static String getYear(Date date) {
  47. SimpleDateFormat df = new SimpleDateFormat(FORMAT_FULL);
  48. return df.format(date).substring(0, 4);
  49. }
  50. /**
  51. * 功能描述:返回月
  52. * @param date
  53. * Date 日期
  54. * @return 返回月份
  55. */
  56. public static int getMonth(Date date) {
  57. calendar.setTime(date);
  58. return calendar.get(Calendar.MONTH) + 1;
  59. }
  60. /**
  61. * 功能描述:返回日
  62. *
  63. * @param date
  64. * Date 日期
  65. * @return 返回日份
  66. */
  67. public static int getDay(Date date) {
  68. calendar.setTime(date);
  69. return calendar.get(Calendar.DAY_OF_MONTH);
  70. }
  71. /**
  72. * 功能描述:返回小
  73. * @param date
  74. * 日期
  75. * @return 返回小时
  76. */
  77. public static int getHour(Date date) {
  78. calendar.setTime(date);
  79. return calendar.get(Calendar.HOUR_OF_DAY);
  80. }
  81. /**
  82. * 功能描述:返回分
  83. * @param date
  84. * 日期
  85. * @return 返回分钟
  86. */
  87. public static int getMinute(Date date) {
  88. calendar.setTime(date);
  89. return calendar.get(Calendar.MINUTE);
  90. }
  91. /**
  92. * 返回秒钟
  93. *
  94. * @param date
  95. * Date 日期
  96. * @return 返回秒钟
  97. */
  98. public static int getSecond(Date date) {
  99. calendar.setTime(date);
  100. return calendar.get(Calendar.SECOND);
  101. }
  102. /**
  103. * 功能描述:返回毫
  104. * @param date
  105. * 日期
  106. * @return 返回毫
  107. */
  108. public static long getMillis(Date date) {
  109. calendar.setTime(date);
  110. return calendar.getTimeInMillis();
  111. }
  112. }

发表评论

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

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

相关阅读