在 JDK 1.1 以前,类 Date 有两个其余的函数。它容许把日期解释为年、月、日、小时、分钟和秒值。它也容许格式化和解析日期字符串。不过,这些函数的 API 不易于实现国际化。从 JDK 1.1 开始,应该使用 Calendar 类实现日期和时间字段之间转换,使用 DateFormat 类来格式化和解析日期字符串。Date 中的相应方法已废弃。 java
尽管 Date 类打算反映协调世界时 (UTC),但没法作到如此准确,这取决于 Java 虚拟机的主机环境。当前几乎全部操做系统都假定 1 天 = 24 × 60 × 60 = 86400 秒。但对于 UTC,大约每一两年出现一次额外的一秒,称为“闰秒”。闰秒始终做为当天的最后一秒增长,而且始终在 12 月 31 日或 6 月 30 日增长。例如,1995 年的最后一分钟是 61 秒,由于增长了闰秒。大多数计算机时钟不是特别的准确,所以不能反映闰秒的差异。 git
一些计算机标准是按照格林威治标准时 (GMT)(1970-01-01 00:00:00) 定义的,格林威治标准时和世界时 (UT) 是相等的。GMT 是标准的“民间”名称;UT 是相同标准的“科学”名称。UTC 和 UT 的区别是:UTC 是基于原子时钟的,UT 是基于天体观察的,二者在实际应用中难分轩轾。由于地球的旋转不是均匀的(它以复杂的方式减速和加速),因此 UT 始终不是均匀地流过。闰秒是根据须要引入 UTC 的,以便把 UTC 保持在 UT1 的 0.9 秒以内,UT1 是应用了某些更正的 UT 版本。还有其余的时间和日期系统;例如,基于卫星的全球定位系统 (GPS) 使用的时间刻度与 UTC 同步,但没有 针对闰秒进行调整。有关更多信息的一个有趣来源是美国海军天文台,特别是 Directorate of Time 的网址shell
a)System.currentTimeMillis():一个13位的long类型。有一个native方法获取运行环境的数字。函数
b)java.util.Date:java具体的时间类封装。主要是封装了System.currentTimeMills()的,解析其数字,获得想要得到的 年,月、秒、小时、分钟、秒、毫秒,也能够直接传一个lang进去。测试
c)java.text.SimpleDateFormat:用户格式化时间。封装了Calendarspa
d)java.util.Calendar:用于时间的对比、增减、获取月份等具体操做。操作系统
CST时区的缩写:code
美国中部时间:Central Standard Time (USA) UT-6:00
澳大利亚中部时间:Central Standard Time (Australia) UT+9:30
中国标准时间:China Standard Time UT+8:00
古巴标准时间:Cuba Standard Time UT-4:00
格式化orm
字母 日期或时间元素 表示 示例
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
若是须要格式化日期则使用SimpleDateFormat,若是须要进行时间操做则使用Calendarci
public void getDate() { // date: System.out.println("System.currentTimeMillis():" + System.currentTimeMillis());// 1451133176031 System.out.println("toString():" + new Date().toString());// Sat Dec 26 20:29:13 CST /** * 时间:Sat Dec 26 20:29:13 CST 2015 * dow is the day of the week (Sun, Mon, Tue, Wed, Thu, Fri, Sat). (星期) * mon is the month (Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec). (月) * dd is the day of the month (01 through 31), as two decimal digits. (一个月的多少号) * hh is the hour of the day (00 through 23), as two decimal digits. (时间) * mm is the minute within the hour (00 through 59), as two decimal digits. (分钟) * ss is the second within the minute (00 through 61, as two decimal digits. (秒) * zzz(时区) is the time zone (and may reflect daylight saving time). Standard time zone abbreviations include those recognized by the method parse. If time zone information is not available, then zzz is empty - that is, it consists of no characters at all. * yyyy is the year, as four decimal digits. (年份) */ // 格式化 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSSS zzz"); System.out.println("format():" + sdf.format(new Date()));// 2015-12-26 21:43:49 0101 CST // 时间的对比、加减 Calendar cld = Calendar.getInstance(); cld.setTime(new Date()); System.out.println(cld.get(Calendar.DAY_OF_MONTH));// 26号 System.out.println(cld.get(Calendar.DAY_OF_MONTH));// 26号 }
public void testLang() { // 测试格林威治时间(失败,不要去弄了) Date date = new Date(1970, 01, 01, 00, 00, 10); // Calendar cld = Calendar.getInstance(); cld.set(1970, 01, 01, 00, 00, 10); System.out.println(date.getTime()); // 59960880010000 System.out.println(new Date().getTime()); // 1451136454150 System.out.println(cld.getTimeInMillis()); // 2649610482 }
public void testFormat() { Date date = new Date(); SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSSS zzz"); System.out.println(sdf1.format(date));// 2015-12-26 22:00:21 0908 CST SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy年MM月dd号 HH小时mm分钟ss秒 SSSS毫秒 zzz时区"); System.out.println(sdf2.format(date));// 2015年12月26号 22小时00分钟21秒 0908毫秒 // CST时区 SimpleDateFormat sdf3 = new SimpleDateFormat("G"); System.out.println(sdf3.format(date));// 公元 SimpleDateFormat sdf4 = new SimpleDateFormat("周/年:ww/52"); System.out.println(sdf4.format(date));// 周/年:52/52 SimpleDateFormat sdf5 = new SimpleDateFormat("周/月:WW/4"); System.out.println(sdf5.format(date));// 周/月:04/4 SimpleDateFormat sdf6 = new SimpleDateFormat("天/年:DDD/(365|366)"); System.out.println(sdf6.format(date));// 天/年:360/(366|366) SimpleDateFormat sdf7 = new SimpleDateFormat("星期/月:F"); System.out.println(sdf7.format(date));// 星期/月:4/4 SimpleDateFormat sdf8 = new SimpleDateFormat("E"); System.out.println(sdf8.format(date));// 星期六 SimpleDateFormat sdf9 = new SimpleDateFormat("a"); System.out.println(sdf9.format(date));// 下午 SimpleDateFormat sdf10 = new SimpleDateFormat("kk点"); System.out.println(sdf10.format(date));// 22点 SimpleDateFormat sdf11 = new SimpleDateFormat("K点"); System.out.println(sdf11.format(date));// 10点(上午下午的多少点) SimpleDateFormat sdf12 = new SimpleDateFormat("h点"); System.out.println(sdf12.format(date));// 10点(上午下午的多少点) }
public void testCalendar() { Calendar cld = Calendar.getInstance(); cld.set(2015, 12, 26, 22, 12, 14); //能够获取的太多,只列举经常使用的 System.out.println("年:" + cld.get(Calendar.YEAR));//年:2015 System.out.println("月:" + cld.get(Calendar.MONTH));//月:11(月份+1) System.out.println("日:" + cld.get(Calendar.DATE));//日:26 System.out.println("小时:" + cld.get(Calendar.HOUR));//小时:10 System.out.println("分钟:" + cld.get(Calendar.MINUTE));//分钟:12 System.out.println("秒:" + cld.get(Calendar.SECOND));//秒:14 System.out.println("几号:" + cld.get(Calendar.DAY_OF_MONTH));//几号:26 System.out.println("星期:" + cld.get(Calendar.DAY_OF_WEEK));//星期:7(星期须要-1) //对比 Calendar cld2 = Calendar.getInstance(); cld2.set(2015, 12, 26, 22, 12, 15);//比cld多了1秒 Calendar cld3 = Calendar.getInstance(); cld3.set(2015, 12, 26, 22, 12, 15);//比cld多了1秒 //cld.set(2015, 12, 26, 22, 12, 14); System.out.println(cld2.compareTo(cld));//1 System.out.println(cld.compareTo(cld2));//-1 System.out.println(cld3.compareTo(cld2));//0 System.out.println(cld2.before(cld));//false System.out.println(cld2.after(cld));// true //增长 //cld3.set(2015, 12, 26, 22, 12, 15); //cld2.set(2015, 12, 26, 22, 12, 15) cld2.add(Calendar.DATE, 1); System.out.println(cld2.get(Calendar.DATE));//27 cld3.add(Calendar.DATE, -1); System.out.println(cld3.get(Calendar.DATE));//25 }