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 }
1 import java.text.DateFormat;
2 import java.text.ParseException;
3 import java.text.SimpleDateFormat;
4 import java.util.Date;
5 /**
6 * @ClassName: DateTimeUtil
7 * @Description: Date
8 * @author yanfuchang
9 * @date May 23, 2018 5:59:36 PM
10 */
11 public class DateTimeUtil {
12 private static String pat = "yyyy:MM:dd HH:mm:ss:SSS";
13 private static DateFormat df = null;
14 private static SimpleDateFormat sdf = null;
15
16 /**
17 * @Title: getTimeStamp
18 * @Description: 当前日期时间戳
19 * @return Long 返回类型
20 * @throws
21 */
22 public static Long getTimeStamp() {
23 return new Date().getTime();
24 }
25
26 /**
27 * @Title: getDateTime
28 * @Description: 当前时间默认格式化类型数据(例如:2018-5-23 18:01:43)
29 * @return String 返回类型
30 * @throws
31 */
32 public static String getDateTime() {
33 df = DateFormat.getDateTimeInstance();
34 return df.format(new Date());
35 }
36
37 /**
38 * @Title: getDateComplete
39 * @Description: 获取当前日期的指定格式化后日期(例如:2018:05:23 18:01:43:370)
40 * @return String 返回类型
41 * @throws
42 */
43 public static String getDateComplete() {
44 sdf = new SimpleDateFormat(pat);
45 return sdf.format(new Date());
46 }
47
48 /**
49 * @Title: getDateType
50 * @Description: 获取当前日期是Date类型(Wed May 23 18:01:00 CST 2018)
51 * @param strDate
52 * @throws ParseException
53 * @return Date 返回类型
54 * @throws
55 */
56 public static Date getDateType(String strDate) throws ParseException {
57 sdf = new SimpleDateFormat(pat);
58 return sdf.parse(strDate);
59 }
60
61 public static void main(String[] args) {
62 System.out.println(getTimeStamp());
63 System.out.println(getDateTime());
64 System.out.println(getDateComplete());
65 try {
66 System.out.println(getDateType("2018:05:23 18:01:00:000"));
67 } catch (ParseException e) {
68 e.printStackTrace();
69 }
70 }
71 }