-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDateDemo.java
More file actions
65 lines (53 loc) · 1.95 KB
/
DateDemo.java
File metadata and controls
65 lines (53 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package com.date;
import javax.xml.crypto.Data;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateDemo {
private static void formatCurrentTime() {
Date date1=new Date();
long currentTime = System.currentTimeMillis();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy年-MM月dd日-HH时mm分ss秒");
Date date2 = new Date(currentTime);
System.out.println(date1);
System.out.println(formatter.format(date2));
}
public static void dateToString(Date date) {
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
String str=sdf.format(date);
System.out.println(date+"转化为字符串是:"+str);
}
public static void stringToDate() {
//试着用SimpleDateFormat的parse方法将字符串转化为date类型
//MM必须大写。。区分月份和时间
DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String str="2016-5-11 11:06:59";
Date date=null;
try{
/*Date类型的parse方法已经过时了,不可用.一般用SimpleDateFormat的parse */
date=dateFormat.parse(str);
System.out.println("今天的日期是:"+date);
}catch(ParseException e) {
e.printStackTrace();
}
}
public static void stringToDate2() {
String str="2016-07-21 15:41:52";
try {
Date date=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(str);
System.out.println(DateFormat.getDateInstance().format(date));
System.out.println(new SimpleDateFormat("yyyy年MM月dd日").format(date));
System.out.println(new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss").format(date));
System.out.println(new SimpleDateFormat("yyyy/MM/dd HH/mm/ss").format(date));
System.out.println(new SimpleDateFormat("yyyy-MM-dd").format(date));
}catch(Exception e) {
e.printStackTrace();
}
}
public static void compareDate(Date date1,Date date2) {
if (date1.compareTo(date2) == 0) {
System.out.println(date1+"等于"+date2);
}
}
}