当前位置:文档之家› java时间格式大全

java时间格式大全

又收藏到一个好的 java 时间格式大全,这个是做成了类的
又收藏到一个好的 java 时间格式大全,这个是做成了类的 转自:/post/java_string_date_time_class_all.html Java 代码 1. java.util.*; 2. import java.text.*; 3. import java.util.Calendar; 4. 5. public class VeDate { 6. 7. 8. 9. 10. 12. 13. 14. 15. 16. 17. 19. 20. /** 21. 22. 23. 24. 26. 27. 28. 29. 30. 31. 33. 34. /** 35. 36. 37. 38. * 获取现在时间 * * @return 返回字符串格式 yyyy-MM-dd HH:mm:ss */ * 获取现在时间 * * @return 返回短时间格式 yyyy-MM-dd */ Date currentTime = new Date(); SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); String dateString = formatter.format(currentTime); ParsePosition pos = new ParsePosition(8); Date currentTime_2 = formatter.parse(dateString, pos); return currentTime_2; /** * 获取现在时间 * * @return 返回时间类型 yyyy-MM-dd HH:mm:ss */ Date currentTime = new Date(); SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss "); String dateString = formatter.format(currentTime); ParsePosition pos = new ParsePosition(8); Date currentTime_2 = formatter.parse(dateString, pos); return currentTime_2;
137. public static Date getLastDate(long day) {
142. }
149. public static String getStringToday() {
1ic String getHour() {
");
166. }
167. 168. /** 169. 170. 171. 172. 174. 175. 176. 177. 178. 179. 181. 182. /** 183. 184. 185. 186. 187. 188. 190. 191. 192. 193. 195. 196. /** 197. 198. 200. 201. 202. 203. 204. 205. 206. 207. 208. * 二个小时时间间的差值,必须保证二个时间都是"HH:MM"的格式,返回字符型的分钟 */ String[] kk = null; String[] jj = null; kk = st1.split(":"); jj = st2.split(":"); if (Integer.parseInt(kk[0]) < Integer.parseInt(jj[0])) return "0"; else { double y = Double.parseDouble(kk[0]) + Double.parseDouble(kk[1]) / 60; double u = Double.parseDouble(jj[0]) + Double.parseDouble(jj[1]) / 60; * 根据用户传入的时间表示格式,返回当前时间的格式 如果是 yyyyMMdd,注意字母 y 不能 * * @param sformat * * @return */ Date currentTime = new Date(); SimpleDateFormat formatter = new SimpleDateFormat(sformat); String dateString = formatter.format(currentTime); return dateString; yyyyMMddhhmmss 大写。 * 得到现在分钟 * * @return */ Date currentTime = new Date(); SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss String dateString = formatter.format(currentTime); String min; min = dateString.substring(14, 16); return min;
81. } 82. 83. /** 84. 85. 86. 87. 88. 90. 91. 92. 94. 95. /** 96. 97. 98. 99. 100. 101. 103. 104. 105. 107. 108. /** 109. 110. 111. 112. 113. 115. 116. 117. 118. 120. 121. /** 122. 123. * 得到现在时间 * * 将短时间格式字符串转换为时间 yyyy-MM-dd * * @param strDate * @return */ SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); ParsePosition pos = new ParsePosition(0); Date strtodate = formatter.parse(strDate, pos); return strtodate; * 将短时间格式时间转换为字符串 yyyy-MM-dd * * @param dateDate * @param k * @return */ SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); String dateString = formatter.format(dateDate); return dateString; * 将长时间格式时间转换为字符串 yyyy-MM-dd HH:mm:ss * * @param dateDate * @return */ SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss "); String dateString = formatter.format(dateDate); return dateString;
44. }
51. public static String getStringDateShort() {
56. }
63. public static String getTimeShort() {
68. }
76. public static Date strToDateLong(String strDate) {
119. }
124. 125. 127. 128. 130.
* @return */ Date currentTime = new Date(); return currentTime;
126. public static Date getNow() {
129. } 131. /** 132. 133. 134. 135. 136. 138. 139. 140. 141. 143. 144. /** 145. 146. 147. 148. 150. 151. 152. 153. 155. 156. /** 157. 158. 160. 161. 162. 163. 164. 165. * 得到现在小时 */ Date currentTime = new Date(); SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss String dateString = formatter.format(currentTime); String hour; hour = dateString.substring(11, 13); return hour; * 得到现在时间 * * @return 字符串 yyyyMMdd HHmmss */ Date currentTime = new Date(); SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd HHmmss"); String dateString = formatter.format(currentTime); return dateString; * 提取一个月中的最后一天 * * @param day * @return */ Date date = new Date(); long date_3_hm = date.getTime() - 3600000 * 34 * day; Date date_3_hm_date = new Date(date_3_hm); return date_3_hm_date;
89. public static String dateToStrLong(java.util.Date dateDate) {
93. }
相关主题