java中日期类(Calendar)

た 入场券 2022-08-18 03:24 378阅读 0赞

日期类

1、Date

• 从 JDK 1.1 开始,应该使用Calendar 类实现日期和时间字段之间转换,使用DateFormat 类来格式化和解析日期字符串。Date 中的相应方法已废弃。

2、DateFormat(抽象类)->子类SimpleDateFormat

• 它允许进行格式化(日期 -> 文本)、解析(文本 -> 日期)和规范化。

• Date->String, String->Date

3、Calendar

• Calendar 类是一个抽象类,它为特定瞬间与一组诸如 YEAR、MONTH、DAY_OF_MONTH、HOUR 等。日历字段之间的转换提供了一些方法,并为操作日历字段(例如获得下星期的日期)提供了一些方法 .

• Calendar 提供了一个类方法 getInstance,以获得此类型的一个通用的对象。Calendar 的 getInstance 方法返回一个 Calendar 对象。

4、SimpleDateFormat定义的字母代表的含义




























































































































字母 日期或时间元素 表示 示例
G Era 标志符 Text AD
y Year 1996; 96
M 年中的月份 Month July; Jul; 07
w 年中的周数 Number 27
W 月份中的周数 Number 2
D 年中的天数 Number 189
d 月份中的天数 Number 10
F 月份中的星期 Number 2
E 星期中的天数 Text Tuesday; Tue
a Am/pm 标记 Text PM
H 一天中的小时数(0-23) Number 0
k 一天中的小时数(1-24) Number 24
K am/pm 中的小时数(0-11) Number 0
h am/pm 中的小时数(1-12) Number 12
m 小时中的分钟数 Number 30
s 分钟中的秒数 Number 55
S 毫秒数 Number 978
z 时区 General time zone Pacific Standard Time; PST; GMT-08:00
Z 时区 RFC 822 time zone -0800

5、long、Date、String、Calendar之间的转换图如下:

Center

例题一:

打印输出2012年3月11日是星期几?

  1. package com.jcxy.demo09;
  2. import java.text.ParseException;
  3. import java.text.SimpleDateFormat;
  4. import java.util.Date;
  5. /*
  6. * 打印输出2015年12月18日是星期几?
  7. * 分析:
  8. * 1-String -> Date(使用SimpleDateFormat的parse方法)
  9. * 2-Date -> String (使用SimpleDateFormat的format方法 E)
  10. */
  11. public class CalendarDemo
  12. {
  13. public static void main(String[] args) throws ParseException
  14. {
  15. //1-将数据存放在字符串中
  16. String str = "2015年12月18日";
  17. //2-定义两种日期转换格式
  18. SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy年MM月dd日");
  19. SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy年MM月dd日 E");
  20. //3-将指定的日期转换为与之相匹配的格式
  21. Date d1 = sdf1.parse(str); //这里存在异常,抛出暂时不处理
  22. //4-将日期转换为我们需要的格式,即可以输出星期的格式
  23. String str1 = sdf2.format(d1);
  24. //输出结果
  25. System.out.println(str1);
  26. }
  27. <span style="font-size:18px;">}
  28. </span>

例题二:

计算2013年11月1号11:9:20 123到2008年7月3号12:19:22 777,中间隔了多少年多少月多少天多少小时多少分钟多少秒多少毫秒?

  1. package com.jcxy.demo09;
  2. import java.text.ParseException;
  3. import java.text.SimpleDateFormat;
  4. import java.util.Calendar;
  5. import java.util.Date;
  6. /*
  7. * 计算2013年11月1号11:9:20 123到2008年7月3号12:19:22 777,中间隔了多少年多少月多少天多少小时多少分钟多少秒多少毫秒
  8. */
  9. public class CalendarDemo1
  10. {
  11. public static void main(String[] args) throws ParseException
  12. {
  13. //1-将两个年份保存在字符串中
  14. String str1 = "2013年11月1号 11:9:20 123";
  15. String str2 = "2008年7月3号 12:19:22 777";
  16. //2-定义与之相对应的日期转换格式
  17. SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd号 HH:mm:ss SSS");
  18. //3-将字符串形式转换为日期形式
  19. Date d1 = sdf.parse(str1);
  20. Date d2 = sdf.parse(str2);
  21. //打印两者的时间
  22. System.out.println(sdf.format(d1));
  23. System.out.println(sdf.format(d2));
  24. //4-将 date转换为calendar
  25. Calendar ca1 = Calendar.getInstance();
  26. Calendar ca2 = Calendar.getInstance();
  27. ca1.setTime(d1);
  28. ca2.setTime(d2);
  29. //5-获取两段时间的毫秒之差
  30. long lca1 = ca1.getTimeInMillis();
  31. long lca2 = ca2.getTimeInMillis();
  32. long differ = lca1 - lca2;
  33. //6-将两者之间赋给新的Calendar对象
  34. Calendar ca3 = Calendar.getInstance();
  35. ca3.setTimeInMillis(differ);
  36. //7-获取具体的时间
  37. int year = ca3.get(Calendar.YEAR) - 1970;//时间是从1970年开始算的,所以减去原点时间。
  38. int mon = ca3.get(Calendar.MONTH);
  39. int day = ca3.get(Calendar.DAY_OF_MONTH);
  40. int hour = ca3.get(Calendar.HOUR_OF_DAY);
  41. int min = ca3.get(Calendar.MINUTE);
  42. int sec = ca3.get(Calendar.SECOND);
  43. int mill = ca3.get(Calendar.MILLISECOND);
  44. System.out.println("相隔了"+year+"年"+mon+"月"+day+"天 "+hour+"时"+min+"分"+sec+"秒 "+mill+"毫秒");
  45. }
  46. }

发表评论

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

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

相关阅读