Date类和SimpleDateFormat类

Date类

 * Date方法
 *    getTime()  返回自 1970 年 1 月 1 日 00:00:00 GMT 以来此 Date 对象表示的毫秒数。
 *      setTime(long time)
            设置此 Date 对象,以表示 1970 年 1 月 1 日 00:00:00 GMT 以后 time 毫秒的时间点。
 * System方法
 *   currentTimeMillis() 返回以毫秒为单位的当前时间。 long型

 1 package date;
 2 
 3 import java.util.Date;
 4 
 5 /**
 6  * Date方法
 7  *         getTime()  返回自 1970 年 1 月 1 日 00:00:00 GMT 以来此 Date 对象表示的毫秒数。
 8  *      setTime(long time) 
 9                   设置此 Date 对象,以表示 1970 年 1 月 1 日 00:00:00 GMT 以后 time 毫秒的时间点。
10  * System方法
11  *         currentTimeMillis() 返回以毫秒为单位的当前时间。 long型
12  * @author star
13  *
14  */
15 public class DateDemo1 {
16     public static void main(String[] args) {
17         //使用系统当前时间构建date对象
18         
19         Date d = new Date();
20         System.out.println(d);//Thu Apr 19 15:55:54 CST 2018
21         System.out.println(d.getTime());//1524124627002
22         
23         long time =  System.currentTimeMillis();
24         System.out.println(time);//1524126032910
25         
26         Date d2 =new  Date();
27         d2.setTime(1000);//将参数的毫秒值加上零时区的时间构成一个新的时间
28         System.out.println(d2);//Thu Jan 01 08:00:01 CST 1970
29         
30         
31         
32         
33     }
34 }
Date类

 

SimpleDateFormat类

* 方法:

     String pattern = "yyyy年MM月dd日 HH时mm分ss秒";//定义格式
    SimpleDateFormat(String pattern)
           用给定的模式和默认语言环境的日期格式符号构造 SimpleDateFormat。

    format(Date date, StringBuffer toAppendTo, FieldPosition pos)
            将给定的 Date 格式化为日期/时间字符串,并将结果添加到给定的 StringBuffer。
    parse(String text, ParsePosition pos)
           解析字符串的文本,生成 Date

 1 package date;
 2 
 3 import java.text.ParseException;
 4 import java.text.SimpleDateFormat;
 5 import java.util.Date;
 6 
 7 /**
 8  * 类 SimpleDateFormat
 9  * SimpleDateFormat 是一个以与语言环境有关的方式来格式化和解析日期的具体类
10  * 它允许进行格式化(日期 -> 文本)、解析(文本 -> 日期)和规范化。 
11  * 构造方法:   SimpleDateFormat(String pattern) 
12               用给定的模式和默认语言环境的日期格式符号构造 SimpleDateFormat。
13  * 方法:
14  *     format(Date date, StringBuffer toAppendTo, FieldPosition pos) 
15               将给定的 Date 格式化为日期/时间字符串,并将结果添加到给定的 StringBuffer。
16     parse(String text, ParsePosition pos) 
17               解析字符串的文本,生成 Date
18     
19  *     
20  */
21 public class DateDemo2 {
22     private static SimpleDateFormat simpleDateFormat;
23 
24     public static void main(String[] args) throws ParseException {
25         Date date = new Date();
26         String pattern = "yyyy年MM月dd日 HH时mm分ss秒";//定义格式
27         //以给定的模式构造SimpleDateFormat
28         SimpleDateFormat sdf  = new SimpleDateFormat(pattern);
29         String format = sdf.format(date);//格式化
30         System.out.println(format);//2018年04月19日 16时59分04秒
31         
32         String strDate = "2018年02月01日 17时53分12秒";
33         String pattern2 = "yyyy年MM月dd日 HH时mm分ss秒";//模式
34         //以给定的模式构造SimpleDateFormat
35         SimpleDateFormat sdf2  = new SimpleDateFormat(pattern);    
36         Date par = sdf2.parse(strDate);//解析成Date
37         System.out.println(par);
38     }
39 }
SimpleDateFormat 格式化和解析

 

posted @ 2018-04-19 17:06  star521  阅读(144)  评论(0编辑  收藏  举报