码迷,mamicode.com
首页 > 其他好文 > 详细

[19/03/16-星期六] 常用类_Date时间类&DateFormat类

时间:2019-03-20 22:19:34      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:info   它的   bool   OLE   str   exception   tostring   mem   throw   

一、Date时间类

   计算机中 以1970 年 1 月 1 日 00:00:00定为基准时间,每个度量单位是毫秒(1秒的千分之一)

   用ong类型的变量来表示时间,如当前时刻数值:long  now =new System.currentTimeMillis();

技术图片

【常用方法】

     1. Date() 分配一个Date对象,并初始化此对象为系统当前的日期和时间,可以精确到毫秒。

      2. Date(long date) 分配 Date 对象并初始化此对象,以表示自从标准基准时间(称为“历元(epoch)”,即 1970 年 1 月 1 日 00:00:00 GMT)以来的指定毫秒数。

      3. boolean after(Date when)  测试此日期是否在指定日期之后。

      4. booleanbefore(Date when)  测试此日期是否在指定日期之前。

      5. boolean equals(Object obj)   比较两个日期的相等性。

      6. long getTime() 返回自 1970 年 1 月 1 日 00:00:00 GMT 以来此 Date 对象表示的毫秒数。

      7. String toString() 把此 Date 对象转换为以下形式的 String: dow mon dd hh:mm:ss zzz yyyy 其中: dow 是一周中的某一天 (Sun、 Mon、Tue、Wed、 Thu、 Fri、 Sat)。

【代码示例】

 1 /**
 2  * 测试时间类
 3  */
 4 package cn.sxt.test;
 5 
 6 import java.util.Date;
 7 public class Test_0316_DateClass {
 8     public static void main(String[] args) {
 9         Date date=new Date();//根据Java源码可看出,如果这个构造Date类,什么参数都不传,会输出当前时刻的时间
10         System.out.println(date);
11         
12         Date date2=new Date(2000); //这个Date类传进去参数为2000(毫秒),会输出从1970年1月1日00:00  起经过2000毫秒后的时刻
13         System.out.println(date2);//由于中国位于东八区 会在输出时刻的基础上加8个小时(此函数以格林威治时间为标准) 输出8:00:02
14         
15         System.out.println(date.getTime());//getTime()从基准时刻算起,获得当前时刻的毫秒数
16         System.out.println(date2.getTime());
17         
18         System.out.println(date.after(date2));//判断对象date时刻是否在对象date2时刻之后
19         System.out.println(date.before(date2));//判断对象date时刻是否在对象date2时刻之前    
20         
21     }
22     
23 
24 }

 

 二、DateFormat类 (时间格式化类)

     把时间对象转化成指定格式的字符串。反之,把指定格式的字符串转化成时间对象。

     DateFormat是一个抽象类,一般使用它的的子类SimpleDateFormat类来实现。

技术图片

【代码示例】

 1 /*
 2  *测试时间对象和字符串之间的相互转化
 3  * DateFormat类 时间格式类 是个抽象类
 4  */
 5 package cn.sxt.test;
 6 
 7 import java.text.DateFormat;
 8 import java.text.ParseException;
 9 import java.text.SimpleDateFormat;
10 import java.util.Date;
11 public class Test_0316_DateFormatClass {
12     public static void main(String[] args) throws ParseException {
13 
14         //抽象类DateFormat不能通过new一个对象 而是通过实现类SimpleDateFormat来new 一个对象
15 
16         //"把时间对象按照格式字符串输出 "   格式化输出日期    年(年是4位数字,用4个y)月(月2位,2个M)日(日2位,2个d) 以下同理
17         DateFormat dFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); 
18         DateFormat dFormat1 = new SimpleDateFormat("yyyy年MM月dd日  hh时mm分ss秒 ");//yyyy等是特殊标记不要变,其它自定义 
19 
20         String str=dFormat.format(new Date());//new Date() 当前时间的一个对象;dFormat.format()按照自定义的格式输出当前时间
21         System.out.println(str);  
22         System.out.println(dFormat1.format(new Date(4000)));//上面的更加缩写版 表示按格式输出4000毫秒后的时间
23         
24         //把字符串转成相应的时间对象输出
25         
26         String  str1="2019-10-01 10:00:05";//字符串必须按照时间对象dFormat的("yyyy-MM-dd hh:mm:ss")格式书写才会输出
27         Date date = dFormat.parse(str1);
28         System.out.println(date); 
29         
30         //测试其它格式 
31         DateFormat dFormat2 = new SimpleDateFormat("2019年第D天,当月的第d天,当月的第F星期,上/下午:a");
32         System.out.println(dFormat2.format(date));
33         
34           
36     }
37 
38 
39 }

 

 

  

 

[19/03/16-星期六] 常用类_Date时间类&DateFormat类

标签:info   它的   bool   OLE   str   exception   tostring   mem   throw   

原文地址:https://www.cnblogs.com/ID-qingxin/p/10568005.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!