在学习编程的过程中,我觉得不止要获得课本的知识,更多的是通过学习技术知识提高解决问题的能力,这样我们才能走在最前方,本文主要讲述Java各种获取系统当前时间方法和格式,更多Java专业知识,广州疯狂java培训官网与你分享;
如何利用JAVA快速准确提取当前系统时间方法和格式,我们先来看实现代码:
01./**
02. * 返回当前日期时间字符串<br>
03. * 默认格式:yyyy-mm-dd hh:mm:ss
04. *
05. * @return String 返回当前字符串型日期时间
06. */
07. public static String getCurrentTime() {
08. String returnStr = null;
09. SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
10. Date date = new Date();
11. returnStr = f.format(date);
12. return returnStr;
13. }
14.
15. /**
16. * 返回当前日期时间字符串<br>
17. * 默认格式:yyyymmddhhmmss
18. *
19. * @return String 返回当前字符串型日期时间
20. */
21. public static BigDecimal getCurrentTimeAsNumber() {
22. String returnStr = null;
23. SimpleDateFormat f = new SimpleDateFormat("yyyyMMddHHmmss");
24. Date date = new Date();
25. returnStr = f.format(date);
26. return new BigDecimal(returnStr);
27. }
28.
29.
30. /**
31. * 返回自定义格式的当前日期时间字符串
32. *
33. * @param format
34. * 格式规则
35. * @return String 返回当前字符串型日期时间
36. */
37. public static String getCurrentTime(String format) {
38. String returnStr = null;
39. SimpleDateFormat f = new SimpleDateFormat(format);
40. Date date = new Date();
41. returnStr = f.format(date);
42. return returnStr;
43. }
44.
45. /**
46. * 返回当前字符串型日期
47. *
48. * @return String 返回的字符串型日期
49. */
50. public static String getCurDate() {
51. Calendar calendar = Calendar.getInstance();
52. SimpleDateFormat simpledateformat = new SimpleDateFormat("yyyy-MM-dd");
53. String strDate = simpledateformat.format(calendar.getTime());
54. return strDate;
55. }
56.
57. /**
58. * 返回指定格式的字符型日期
59. * @param date
60. * @param formatString
61. * @return
62. */
63. public static String Date2String(Date date, String formatString) {
64. if (G4Utils.isEmpty(date)) {
65. return null;
66. }
67. SimpleDateFormat simpledateformat = new SimpleDateFormat(formatString);
68. String strDate = simpledateformat.format(date);
69. return strDate;
70. }
71.
72. /**
73. * 返回当前字符串型日期
74. *
75. * @param format
76. * 格式规则
77. *
78. * @return String 返回的字符串型日期
79. */
80. public static String getCurDate(String format) {
81. Calendar calendar = Calendar.getInstance();
82. SimpleDateFormat simpledateformat = new SimpleDateFormat(format);
83. String strDate = simpledateformat.format(calendar.getTime());
84. return strDate;
85. }
86.
87. /**
88. * 返回TimeStamp对象
89. *
90. * @return
91. */
92. public static Timestamp getCurrentTimestamp() {
93. Object obj = TypeCaseHelper.convert(getCurrentTime(), "Timestamp", "yyyy-MM-dd HH:mm:ss");
94. if (obj != null)
95. return (Timestamp) obj;
96. else
97. return null;
98. }
99.
100. /**
101. * 将字符串型日期转换为日期型
102. *
103. * @param strDate
104. * 字符串型日期
105. * @param srcDateFormat
106. * 源日期格式
107. * @param dstDateFormat
108. * 目标日期格式
109. * @return Date 返回的util.Date型日期
110. */
111. public static Date stringToDate(String strDate, String srcDateFormat,
String dstDateFormat) {
112. Date rtDate = null;
113. Date tmpDate = (new SimpleDateFormat(srcDateFormat))。parse
(strDate, new ParsePosition(0));
114. String tmpString = null;
115. if (tmpDate != null)
116. tmpString = (new SimpleDateFormat(dstDateFormat))。format
(tmpDate);
117. if (tmpString != null)
118. rtDate = (new SimpleDateFormat(dstDateFormat))。parse
(tmpString, new ParsePosition(0));
119. return rtDate;
120. }
疯狂Java培训专注软件开发培训,提升学员就业能力,重点提升实践动手能力。高薪从IT名企请来项目经理为学员亲自授课,对学员进行实战教学,在没有工作经验的学员,在疯狂java,通过大量全真经典企业级项目进行集中培训,学员通过数月培训都可获得1-2年的工作经验,进而在同类的求职者中脱颖而出。疯狂Java培训让你体会java编程的快乐,项目开发的兴奋,成就感,通过短短几个月的时间,让你在半年的时间内掌握8-10万的代码量,掌握Java核心技术,迅速成为技能型的现代化高端人才,迅速获得高薪就业!
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步