Date

 1 package test;
 2 
 3 import java.util.Date;
 4 
 5 
 6 
 7 /*
 8  * Date类 : 表示特定的瞬间,精确到毫秒
 9  *                 构造方法
10  *                             Date() :根据当前的毫秒值创建日期对象
11  *                             Date(long date):根据给定的毫秒值创建日期对象
12  *                 成员方法:
13  *                             public long getTime()   获取时间,毫秒   ==System.currentTimeMillis();
14  *                             public void setTime(long time)   设置时间
15  * 
16  *         
17  * 从Date得到一个毫秒值
18  * getTime
19  * 从毫秒值到Date
20  * 构造方法
21  * setTime(long time)
22  * 
23  * 
24  * 
25  * */
26 
27 public class Test01 {
28     public static void main(String[] args) {
29         Date d=new Date();
30         System.out.println(d);
31         
32         long time=System.currentTimeMillis();
33         Date d1=new Date(time);
34         System.out.println(d1);
35         
36         //获取时间
37         long time2=d.getTime();
38         System.out.println(time2);
39         
40         //设置时间
41         System.out.println(d1);
42     }
43 }
 1 package test;
 2 
 3 import java.text.ParseException;
 4 import java.text.SimpleDateFormat;
 5 import java.util.Date;
 6 
 7 
 8 /*
 9  * DateFormat类
10  * 
11  *     构造方法
12  * SimpleDateFormat
13  * 
14  * Date-String  格式化
15  *         format
16  * String-date  解析
17  *             parse
18  * */
19 
20 public class Test01 {
21     public static void main(String[] args) throws Exception {
22         Date d=new Date();
23         SimpleDateFormat sdf=new SimpleDateFormat(); //默认模式
24         SimpleDateFormat sdf1=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
25         String s=sdf.format(d);
26         String s1=sdf1.format(d);
27         System.out.println(s);
28         System.out.println(s1);
29         
30         String s3="2015-02-12 23:21:22";
31         
32         Date ddd=sdf1.parse(s3);
33         System.out.println(ddd+"---");
34     }
35 }
 1 package test;
 2 
 3 import java.text.ParseException;
 4 import java.text.SimpleDateFormat;
 5 import java.util.Date;
 6 
 7 public class Test01 {
 8     public static void main(String[] args) throws Exception {
 9         Date d=new Date();
10         String s=DateUtil.dateToString(d, "yyyy-MM-dd HH:mm:ss");
11         System.out.println(s);
12     }
13 }
 1 package test;
 2 import java.util.Date;
 3 import java.text.ParseException;
 4 import java.text.SimpleDateFormat;
 5 public class DateUtil {
 6     private DateUtil(){
 7         
 8     }
 9     
10     public static String dateToString(Date d,String format){
11         SimpleDateFormat sdf=new SimpleDateFormat(format);
12         
13         return new SimpleDateFormat(format).format(d);
14     }
15     public static Date StringTODate(String s,String format) throws ParseException{
16         return new SimpleDateFormat(format).parse(s);
17     }
18 }
 1 package test;
 2 
 3 import java.text.ParseException;
 4 import java.text.SimpleDateFormat;
 5 import java.util.Date;
 6 import java.util.Scanner;
 7 
 8 public class Test01 {
 9     public static void main(String[] args) throws Exception {
10         Scanner sc=new Scanner(System.in);
11         System.out.println("please input into");
12         String line=sc.nextLine();
13         
14         SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
15         Date d=sdf.parse(line);
16         
17         long time=d.getTime();
18         
19         long timeNow=System.currentTimeMillis()-time;
20         
21         long day=timeNow/1000/60/60/24;
22         
23         System.out.println("天数为:"+day);
24         
25         
26     }
27 }

 

posted @ 2015-08-24 16:05  chengling  阅读(141)  评论(0编辑  收藏  举报