1:DateDemo1类
[java] view plain copy
1. /*
2. * 取得系统的时间
3. */
4. public class DateDemo1 {
5. public static void main(String args[]) {
6. System.out.println(System.currentTimeMillis());
7.
8. }
输出为:1311060393171
执行结果会显示从1970年1月1日开始到取得系统时间为止所经过的毫秒数,例如1115346430703这个数字,但这样的数字没有人确切了解它的意义是什么,您可以使用Date类别来让这个数字变的更有意义一些
2:DateDemo2类
[java] view plain copy
1. public class DateDemo2 {
2. @SuppressWarnings("deprecation")
3. public static void main(String args[]) {
4. Date date = new Date();
5.
6. System.out.println("date.toString: " + date.toString());
7. System.out.println("date: " + date.getDate());
8. System.out.println("day: " + date.getDay());
9. System.out.println("hour: " + date.getHours());
10. System.out.println("minutes: " + date.getMinutes());
11. System.out.println("month: " + (date.getMonth() + 1));
12. System.out.println("seconds: " + date.getSeconds());
13. System.out.println("time: " + date.getTime());
14. System.out.println("timezone: " + date.getTimezoneOffset());
15. System.out.println("year: " + (date.getYear() + 1900));
16.
17. Calendar calendar = Calendar.getInstance();
18. int year = calendar.get(Calendar.YEAR);
19. int month = calendar.get(Calendar.MONTH) + 1;
20. int day = calendar.get(Calendar.DATE);
21. int hour = calendar.get(Calendar.HOUR);
22. int minute = calendar.get(Calendar.MINUTE);
23. int second = calendar.get(Calendar.SECOND);
24.
25. System.out.println("----------Calendar-------");
26. System.out.println("year: " + year);
27. System.out.println("month: " + month);
28. System.out.println("day: " + day);
29. System.out.println("hour: " + hour);
30. System.out.println("minute: " + minute);
31. System.out.println("second: " + second);
32.
33. }
34. }
用date获得时间的值,如时、分、秒、月、日、年得方法已经过时,需要改用Calendar的方法来获取时间的值
输出结果为:
[java] view plain copy
1. date.toString: Tue Jul 19 15:32:42 CST 2011
2. date: 19
3. day: 2
4. hour: 15
5. minutes: 32
6. month: 7
7. seconds: 42
8. time: 1311060762362
9. timezone: -480
10. year: 2011
11. ----------Calendar-------
12. year: 2011
13. month: 7
14. day: 19
15. hour: 3
16. minute: 32
17. second: 42
3:DateDemo3类
[java] view plain copy
1. public class DateDemo3 {
2. public static void main(String args[]) {
3. Date date = new Date();
4.
5. /*
6. * DateFormat会依电脑上的区域设定显示时间格式,EE表示星期,MM表示月份、dd表示日期,而yyyy是西元,每个字元的设定都各有其意义
7. * 从Date-->String
8. */
9. DateFormat dateFormat = new SimpleDateFormat("EE-MM-dd-yyyy");
10. System.out.println(dateFormat.format(date));
11.
12. DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
13. System.out.println(format1.format(date));
14.
15. DateFormat format2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
16. System.out.println(format2.format(date));
17.
18. /*
19. * 从String-->Date
20. */
21. String dateStr = "2011-7-19";
22. try {
23. Date dateTrans = format1.parse(dateStr);
24. //System.out.println(dateTrans.toString());
25. System.out.println(dateTrans.toLocaleString()); //转换为本地的形式
26. } catch (ParseException e) {
27. e.printStackTrace();
28. }
29.
30. }
31. }
使用DateFormat来格式化日期数据,上面部分为从Date-->String,下面的部分为从String-->Date
输出结果为:
[java] view plain copy
1. 星期二-07-19-2011
2. 2011-07-19
3. 2011-07-19 15:33:27
4. 2011-7-19 0:00:00
4:DateDemo4类
[java] view plain copy
1. public class DateDemo4 {
2.
3. public static void main(String[] args) {
4.
5. Date date = new Date();
6. DateFormat shortFormat = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
7. DateFormat mediumFormat = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM);
8. DateFormat longFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG);
9. DateFormat fullFormat = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL);
10.
11. System.out.println(shortFormat.format(date));
12. System.out.println(mediumFormat.format(date));
13. System.out.println(longFormat.format(date));
14. System.out.println(fullFormat.format(date));
15.
16. }
17.
18. }
输出结果为:
[java] view plain copy
1. 11-7-19 下午3:33
2. 2011-7-19 15:33:54
3. 2011年7月19日 下午03时33分54秒
4. 2011年7月19日 星期二 下午03时33分54秒 CST
5:DateDemo5类
[java] view plain copy
1. public class DateDemo5 {
2. public static void main(String args[]) {
3. Date date = new Date();
4.
5. Locale locale = new Locale("en", "US");
6.
7. DateFormat shortDateFormat = DateFormat.getDateInstance(DateFormat.SHORT, locale);
8. DateFormat mediumDateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM, locale);
9. DateFormat longDateFormat = DateFormat.getDateInstance(DateFormat.LONG, locale);
10. DateFormat fullDateFormat = DateFormat.getDateInstance(DateFormat.FULL, locale);
11.
12. System.out.println(shortDateFormat.format(date));
13. System.out.println(mediumDateFormat.format(date));
14. System.out.println(longDateFormat.format(date));
15. System.out.println(fullDateFormat.format(date));
16.
17. }
18. }
输出结果为:
[java] view plain copy
1. 7/19/11
2. Jul 19, 2011
3. July 19, 2011
4. Tuesday, July 19, 2011