当前位置: 移动技术网 > IT编程>开发语言>Java > Java-Date

Java-Date

2018年10月08日  | 移动技术网IT编程  | 我要评论
 1 import java.text.parseexception;
 2 import java.text.simpledateformat;
 3 import java.util.date;
 4 
 5 /**
 6  * java.util.date   日期类
 7  */
 8 public class testdate {
 9     public static void main(string[] args) {
10         date date1 = new date();
11         system.out.println(date1); // fri mar 23 11:13:32 cst 2018
12         system.out.println(date1.tolocalestring()); // 2018-3-23 11:14:07
13         system.out.println(date1.togmtstring()); // 23 mar 2018 03:14:25 gmt
14         
15         // date类型的对象--->相对时间(毫秒值):
16         long longtime = date1.gettime(); 
17         system.out.println(longtime); // 1521775182193l
18         
19         // date类型的对象之间的比较:
20         date date2 = new date(2018, 3, 23);
21         //返回int类型。如果等于0,则date=date1;如果小于0,则date<date1;
22         system.out.println(date1.compareto(date2)); 
23         
24         // 获取date对象中的信息
25         int year = date1.getyear();
26         int month = date1.getmonth();
27         int date = date1.getdate();
28         int hour = date1.gethours();
29         int minute = date1.getminutes();
30         int second = date1.getseconds();
31         int day = date1.getday();
32         system.out.println(date1.gettimezoneoffset()); //获得本地与格林威治时间的时差
33         
34         // date类型---->格式化成指定格式的字符串:
35         date d = new date();
36         simpledateformat simple = new simpledateformat("yyyy-mm-dd hh:mm:dd");// 指定日期的格式:
37         string time = simple.format(d);
38         system.out.println("date类型格式化成字符串日期:"+time); // 输出格式化之后的时间   2018-03-23 11:26:23
39         
40         // 字符串日期----->格式化成date类型:
41         try {
42             string time2 = "2018-10-08 12:00:00";
43             simpledateformat simple2 = new simpledateformat("yyyy-mm-dd hh:mm:dd");// 指定日期的格式,必须与字符串格式一致
44             date date3 = simple2.parse(time2);
45             system.out.println("字符串日期格式化成date类型:"+date3);
46         } catch (parseexception e) {
47             e.printstacktrace();
48         }
49     }
50 }

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网