1. 用到的包
2. //import java.util.Locale;
3. import
4. import
5.
6. 1.-----------------------------------------
7. 得到系统当前时间:
8.
9. java.util.Date dt=new
10. System.out.print(dt); //输出结果是:Wed Aug 10 11:29:11 CST 2005
11.
12. SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
13. System.out.print(sdf.format(dt)); //输出结果是:2005-08-10
14.
15. 2.-----------------------------------------
16. 把字符串转化为java.util.Date
17. 方法一:
18. SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
19. java.util.Date dt=sdf.parse("2005-2-19");
20. System.out.print(sdf.format(dt)); //输出结果是:2005-2-19
21.
22. 方法二:
23. java.util.Date dt=null;
24. DateFormat df=DateFormat.getDateInstance();
25. dt=df.parse("2005-12-19");
26. System.out.println(dt); //输出结果为:Mon Dec 19 00:00:00 CST 2005
27. System.out.println(df.format(dt)); //输出结果为:2005-2-19
28.
29. 3.-----------------------------------------
30. 把字符串转化为java.sql.Date
31. 字符串必须是"yyyy-mm-dd"格式,否则会抛出IllegalArgumentException异常
32. java.sql.Date sdt=java.sql.Date.valueOf("2005-9-6");
33. System.out.println(sdt); //输出结果为:2005-9-6
34.
35. 4.-----------------------------------------
36. TestApp.java
37.
38. public class
39.
40. public static void
41. "Hello World!");
42. new
43.
44. //System.out.println(d.toLocaleString());
45.
46. //Calendar cld=Calendar.getInstance();
47. "Calendar.get(Calendar.DATE)"+(Calendar.getInstance().get(Calendar.DATE)));
48.
49. new Date();//Date(103,-5,-6);
50. "getNowYear(Date dt)"+getYear(dt));
51. "getNowMonth(Date dt)"+getMonth(dt));
52. "getNowDate(Date dt)"+getDate(dt));
53. }
54.
55.
56. /**
57. * 获得当前日期的年份。
58. * @return int 年份
59. */
60. public static int
61. return
62. }
63.
64. /**
65. * 获得给定日期的年份
66. * @param dt 给定日期
67. * @return int 年份
68. * @throws NullPointerException 如果参数年份为null,抛出异常。
69. */
70. public static int getYear(Date dt)throws
71. if(dt==null){
72. throw new NullPointerException("日期参数为null");
73. else{
74. Calendar cld=Calendar.getInstance();
75. cld.setTime(dt);
76. return
77. }
78. }
79.
80. /**
81. * 获得当前日期的月份。
82. * @return int 月份
83. */
84. public static int
85. return 1+Calendar.getInstance().get(Calendar.MONTH);
86. }
87.
88. /**
89. * 获得给定日期的月份
90. * @param dt 给定日期
91. * @return int 月份(1-12)
92. * @throws NullPointerException 如果参数年份为null,抛出异常。
93. */
94. public static int getMonth(Date dt)throws
95. if(dt==null){
96. throw new NullPointerException("日期参数为null");
97. else{
98. Calendar cld=Calendar.getInstance();
99. cld.setTime(dt);
100. return 1+cld.get(Calendar.MONTH);
101. }
102. }
103. /**
104. * 获得当前日期的当月的天数。
105. * @return int 当月的天数
106. */
107. public static int
108. return 1+Calendar.getInstance().get(Calendar.DATE);
109. }
110.
111. /**
112. * 获得给定日期的当月的天数
113. * @param dt 给定日期
114. * @return int 当月的天数
115. * @throws NullPointerException 如果参数年份为null,抛出异常。
116. */
117. public static int getDate(Date dt)throws
118. if(dt==null){
119. throw new NullPointerException("日期参数为null");
120. else{
121. Calendar cld=Calendar.getInstance();
122. cld.setTime(dt);
123. return
124. }
125. }
126. }
-----------------------------------------------------------------------------------------------------------------------------------------------------------
1. Java中日期格式转换
2.
3. /**
4. * 字符串转换为java.util.Date<br>
5. * 支持格式为 yyyy.MM.dd G 'at' hh:mm:ss z 如 '2002-1-1 AD at 22:10:59 PSD'<br>
6. * yy/MM/dd HH:mm:ss 如 '2002/1/1 17:55:00'<br>
7. * yy/MM/dd HH:mm:ss pm 如 '2002/1/1 17:55:00 pm'<br>
8. * yy-MM-dd HH:mm:ss 如 '2002-1-1 17:55:00' <br>
9. * yy-MM-dd HH:mm:ss am 如 '2002-1-1 17:55:00 am' <br>
10. * @param time String 字符串<br>
11. * @return Date 日期<br>
12. */
13. public static
14. SimpleDateFormat formatter;
15. int tempPos=time.indexOf("AD") ;
16. time=time.trim() ;
17. new SimpleDateFormat ("yyyy.MM.dd G 'at' hh:mm:ss z");
18. if(tempPos>-1){
19. 0,tempPos)+
20. "公元"+time.substring(tempPos+"AD".length());//china
21. new SimpleDateFormat ("yyyy.MM.dd G 'at' hh:mm:ss z");
22. }
23. "-");
24. if(tempPos>-1&&(time.indexOf(" ")<0)){
25. new SimpleDateFormat ("yyyyMMddHHmmssZ");
26. }
27. else if((time.indexOf("/")>-1) &&(time.indexOf(" ")>-1)){
28. new SimpleDateFormat ("yyyy/MM/dd HH:mm:ss");
29. }
30. else if((time.indexOf("-")>-1) &&(time.indexOf(" ")>-1)){
31. new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss");
32. }
33. else if((time.indexOf("/")>-1) &&(time.indexOf("am")>-1) ||(time.indexOf("pm")>-1)){
34. new SimpleDateFormat ("yyyy-MM-dd KK:mm:ss a");
35. }
36. else if((time.indexOf("-")>-1) &&(time.indexOf("am")>-1) ||(time.indexOf("pm")>-1)){
37. new SimpleDateFormat ("yyyy-MM-dd KK:mm:ss a");
38. }
39. new ParsePosition(0);
40. java.util.Date ctime = formatter.parse(time, pos);
41.
42. return
43. }
44.
45.
46. /**
47. * 将java.util.Date 格式转换为字符串格式'yyyy-MM-dd HH:mm:ss'(24小时制)<br>
48. * 如Sat May 11 17:24:21 CST 2002 to '2002-05-11 17:24:21'<br>
49. * @param time Date 日期<br>
50. * @return String 字符串<br>
51. */
52.
53.
54. public static
55. SimpleDateFormat formatter;
56. new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss");
57. String ctime = formatter.format(time);
58.
59. return
60. }
61.
62.
63. /**
64. * 将java.util.Date 格式转换为字符串格式'yyyy-MM-dd HH:mm:ss a'(12小时制)<br>
65. * 如Sat May 11 17:23:22 CST 2002 to '2002-05-11 05:23:22 下午'<br>
66. * @param time Date 日期<br>
67. * @param x int 任意整数如:1<br>
68. * @return String 字符串<br>
69. */
70. public static String dateToString(Date time,int
71. SimpleDateFormat formatter;
72. new SimpleDateFormat ("yyyy-MM-dd KK:mm:ss a");
73. String ctime = formatter.format(time);
74.
75. return
76. }
77.
78.
79. /**
80. *取系统当前时间:返回只值为如下形式
81. *2002-10-30 20:24:39
82. * @return String
83. */
84. public static
85. return dateToString(new
86. }
87.
88. /**
89. *取系统当前时间:返回只值为如下形式
90. *2002-10-30 08:28:56 下午
91. *@param hour 为任意整数
92. *@return String
93. */
94. public static String Now(int
95. return dateToString(new
96. }
97.
98.
99. /**
100. *取系统当前时间:返回值为如下形式
101. *2002-10-30
102. *@return String
103. */
104. public static
105. return dateToString(new Date()).substring(0,10);
106.
107. }
108.
109.
110. /**
111. *取系统给定时间:返回值为如下形式
112. *2002-10-30
113. *@return String
114. */
115. public static
116. return date.substring(0,10);
117.
118. }
119.
120. public static
121. SimpleDateFormat formatter;
122. new SimpleDateFormat ("H");
123. new
124. return
125. }
126.
127. public static
128. SimpleDateFormat formatter;
129. new SimpleDateFormat ("d");
130. new
131. return
132. }
133.
134. public static
135. SimpleDateFormat formatter;
136. new SimpleDateFormat ("M");
137. new
138. return
139. }
140.
141. public static
142. SimpleDateFormat formatter;
143. new SimpleDateFormat ("yyyy");
144. new
145. return
146. }
147.
148. public static
149. SimpleDateFormat formatter;
150. new SimpleDateFormat ("E");
151. new
152. return
153. }
154.
155. 在jsp页面中的日期格式和sqlserver中的日期格式不一样,怎样统一?
156.
157. 在页面上显示输出时,用下面的函数处理一下
158.
159. public class
160. public static
161. new SimpleDateFormat("yyyy/MM/dd");
162. String strDate = formatter.format(myDate);
163. return
164. }
165. }
166.
167. new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
168. new java.text.SimpleDateFormat("yyyy-MM-dd")
169. 建议还是把sqlserver的字段类型改成varchar的吧,用字符串处理可以完全按照自己的意愿处理,没有特殊的需求,不要使用date型
170.
171.
172. 字串日期格式转换
173. 用的API是SimpleDateFormat,它是属於java.text.SimpleDateFormat,所以请记得import进来!
174.
175. 用法:
176. SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
177. 这一行最重要,它确立了转换的格式,yyyy是完整的西元年,MM是月份,dd是日期, 至於HH:mm:ss就不需要我再解释了吧!
178. ps:为什麽有的格式大写,有的格式小写,那是怕避免混淆,例如MM是月份,mm是分;HH是24小时制,而hh是12小时制
179.
180. 1.字串转日期:
181. 2002-10-8 15:30:22要把它转成日期,可以用
182. "2002-10-8 15:30:22");
183. 2.日期转字串
184. 假如把今天的日期转成字串可用
185. new
186. 2002-10-08 14:55:38
187.
188. 透过这个API我们便可以随心所欲的将日期转成我们想要的字串格式,例如希望将日期输出成2002年10月08日,
189. 我们可以这麽写:
190. SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日");
191. String datestr=sdf.format(new
192. datestr便会依照我们设定的格式输出
193.
194.
195. //对日期格式的转换成("yyyy-MM-dd")格式的方法
196. public
197. {
198. new java.text.SimpleDateFormat("yyyy-MM-dd");
199. try
200. {
201. java.util.Date d = sdf.parse(str);
202. new
203. return
204. }
205. catch(Exception ex)
206. {
207. ex.printStackTrace();
208. return null;
209. }
210. }
211. 应用如下:
212. ctmt.setDate(7,this.Convert(info.getManBirth())); // @DATETIME
213.
214.
215. 常用日期问题集锦
216.
217.
218. 1、获取服务器端当前日期:
219. <%@ page import="java.util.Date"%>
220. <%
221. Date myDate = new
222. %>
223.
224. 2、获取当前年、月、日:
225. <%@ page import="java.util.Date"%>
226.
227. <%
228. Date myDate = new
229. int thisYear = myDate.getYear() + 1900;//thisYear = 2003
230. int thisMonth = myDate.getMonth() + 1;//thisMonth = 5
231. int thisDate = myDate.getDate();//thisDate = 30
232. %>
233.
234.
235. 3、按本地时区输出当前日期
236. <%@ page import="java.util.Date"%>
237. <%
238. Date myDate = new
239. out.println(myDate.toLocaleString());
240. %>
241. 输出结果为:
242. 2003-5-30
243.
244.
245. 4、获取数据库中字段名为”publish_time“、类型为Datetime的值
246. <%@ page import="java.util.Date"%>
247. <%
248. ...连接数据库...
249. ResultSet rs = ...
250. Date sDate = rs.getDate("publish_time");
251. %>
252. [code]
253.
254. 5、按照指定格式打印日期
255. [code]
256. <%@ page import="java.util.Date"%>
257. <%@ page import="java.text.DateFormat"%>
258. <%
259. Date dNow = new
260.
261. SimpleDateFormat formatter = new SimpleDateFormat("E yyyy.MM.dd 'at' hh:mm:ss a zzz");
262. out.println("It is "
263. %>
264.
265. 输出的结果为:
266. It is 星期五 2003.05.30 at 11:30:46
267. (更为详尽的格式符号请参看SimpleDateFormat类)
268.
269. 6、将字符串转换为日期
270. <%@ page import="java.util.Date"%>
271. <%@ page import="java.text.DateFormat"%>
272. <%
273. String input = "1222-11-11";
274. SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
275. Date t = null;
276. try{
277. t = formatter.parse(input);
278. out.println(t);
279. }catch(ParseException e){
280. out.println("unparseable using "
281. }
282. %>
283. 输出结果为:
284. Fri Nov 11 00:00:00 CST 1222
285.
286. 7、计算日期之间的间隔
287. <%@ page import="java.util.Date"%>
288. <%@ page import="java.text.DateFormat"%>
289. <%
290. String input = "2003-05-01";
291. SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
292. Date d1 = null;
293. try{
294. d1 = formatter.parse(input);
295. }catch(ParseException e){
296. out.println("unparseable using "
297. }
298.
299. Date d2 = new
300.
301. long
302. out.println("Difference is " + (diff/(1000*60*60*24)) + " days.");
303. %>
304. 输出结果为:
305. Difference is 29
306.
307. 8、日期的加减运算
308. 方法:用Calendar类的add()方法
309. <%@ page import="java.util.*"%>
310. <%@ page import="java.text.*"%>
311. <%
312. Calendar now = Calendar.getInstance();
313. SimpleDateFormat formatter = new SimpleDateFormat("E yyyy.MM.dd 'at' hh:mm:ss a zzz");
314. out.println("It is now "
315. now.add(Calendar.DAY_OF_YEAR,-(365*2));
316. out.println("<br>");
317. out.println("Two years ago was "
318. %>
319. 输出结果为:
320. It is now 星期五 2003.05.30 at 01:45:32
321. Two years ago was 星期三 2001.05.30 at 01:45:32
322.
323. 9、比较日期
324. 方法:用equals()、before()、after()方法
325. <%@ page import="java.util.*"%>
326. <%@ page import="java.text.*"%>
327. <%
328. DateFormat df = new SimpleDateFormat("yyy-MM-dd");
329. Date d1 = df.parse("2000-01-01");
330. Date d2 = df.parse("1999-12-31");
331.
332. String relation = null;
333. if(d1.equals(d2))
334. relation = "the same date as";
335. else if(d1.before(d2))
336. relation = "before";
337. else
338. relation = "after";
339. out.println(d1 +" is " + relation + ' '
340. %>
341. 输出结果为:
342. Sat Jan 01 00:00:00 CST 2000 is after Fri Dec 31 00:00:00 CST 1999
343.
344. 10、记录一件事所花费的时间
345. 方法:调用两次System.getTimeMillis()方法,求差值
346. <%@ page import="java.text.*"%>
347. <%
348. long
349. t0 = System.currentTimeMillis();
350. out.println("Cyc starts at "
351. int k = 0;
352. for(int i =0;i<100000;i++){
353. k += i;
354. }
355. t1 = System.currentTimeMillis();
356. out.println("<br>");
357. out.println("Cyc ends at "
358. out.println("<br>");
359. out.println("This run took " + (t1-t0) + "ms.");
360. %>
361.
362. 输出结果为:
363. Cyc starts at 1054275312432
364. Cyc ends at 1054275312442
365. This run took 10ms.
366.
367. 其它:如何格式化小数
368.
369. <%@ page import="java.text.*"%>
370. <%
371. DecimalFormat df = new DecimalFormat(",###.00");
372. double aNumber = 33665448856.6568975;
373. String result = df.format(aNumber);
374. out.println(result);
375. %>
376.
377. 输出结果为:
378. 33,665,448,856.66
379.
380.
381. ======================
382.
383. 日期比较:
384.
385. 在JAVA中日期的计算与比较可以使用Date和DateFormat来解决,下面是一段示例代码:
386.
387. import
388. import
389.
390. public class
391.
392. public static void
393. try{
394. new
395. DateFormat df=DateFormat.getDateTimeInstance();
396. String now=df.format(date);
397. "现在时间:"+now);
398.
399. "现在时间是否在16:00之前:"+date.before(df.parse("2004-12-24 16:00:00")));
400. }
401. catch(ParseException e){System.out.print(e.getMessage());
402. }
403. }
404. }