The Java date and time classes

A, Date class

  1 Overview

    java.util.Date class represents a specific moment, with millisecond precision.

  2, the configuration method

public Date (): assignment Date object and initializes the object to indicate its assigned time (accurate to a millisecond) 
public Date (Long DATE): distribution Date object and initializes the object to represent the time since the reference standard (called "Calendar yuan (epoch) ", which specifies the number of milliseconds January 1 1970 00:00:00 GMT) since.

    Tips : Because we are in the East eight districts, so our benchmark time at 8:00:00 on January 1, 1970.

    Using the constructor with no arguments, can automatically set the current system time in milliseconds time; designated long type of configuration parameters may be defined msec time.

   Demo:

. 1  Import java.util.Date;
 2  
. 3  public  class Demo01Date {
 . 4      public  static  void main (String [] args) {
 . 5          // creation date for objects, the current time 
. 6          System.out.println ( new new a Date ()); // Tue Jan 16 14:37:35 2018 CST
 . 7          // creation date for objects, the current millisecond value converted into a date object 
. 8          System.out.println ( new new a date (0L)); // Thu Jan 01 08:00 : 00 CST 1970 
9      }
 10      System.out.println (System.currentTimeMillis ()); // get the current system time to January 1, 1970 00:00:00 experienced a number of milliseconds
11 }

   Tips : When using println method automatically toString methods of the Date class called.

     Date classes Object class toString method was overwritten, the output string will be in a specified format.

  3, commonly used method

public long getTime () `to date object is converted into a value corresponding to the time in milliseconds

Two, DateFormat class

  1 Overview

    java.text.DateFormat is the date / time formatting subclasses of the abstract class, through this class can help us complete the conversion between the text and the date that the conversion between a Date object and String objects.

    •  Format : according to the specified format, the conversion from the object Date String object;
    •     Analytical : according to the specified format, the conversion from the object to a String Date object.

  2, the configuration method

    Since DateFormat is an abstract class, it can not be directly used, it is necessary to use a subclass java.text.SimpleDateFromat.

   This class requires a pattern (format) to specify the format or parse standard.

   Constructor :

public long getTime () to convert the date into a target value corresponding to the time in milliseconds

    Tips : Parameters pattern is a string that represents the date and time of the custom format.

  3, formatting rules

    Common rules for the format:

Identification letters (case sensitive) meaning
Y year
M month
d day
H Time
m Minute
s second

    Tips : More detailed formatting rules, please refer to the API documentation SimpleDateFormat class.

   Demo:

1 import java.text.DateFormat;
2 import java.text.SimpleDateFormat;
3 
4 public class Demo02SimpleDateFormat {
5     public static void main(String[] args) {
6         // 对应的日期格式如:2019-08-26 15:06:38
7         DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
8     }    
9 }

  4、常用方法

public String format(Date date):      将Date对象格式化为字符串
public Date parse(String source):     将字符串解析为Date对象。

    format 方法Demo:

 1 import java.text.DateFormat;
 2 import java.text.SimpleDateFormat;
 3 import java.util.Date;
 4 /*
 5  把Date对象转换成String
 6 */
 7 public class DateFormatMethod1 {
 8     public static void main(String[] args) {
 9         Date date = new Date();
10         // 创建日期格式化对象,在获取格式化对象时可以指定风格
11         DateFormat df = new SimpleDateFormat("yyyy年MM月dd日");
12         String str = df.format(date);
13         System.out.println(str); // 2019年8月26日
14     }
15 }

    parse 方法 Demo:

 1 import java.text.DateFormat;
 2 import java.text.ParseException;
 3 import java.text.SimpleDateFormat;
 4 import java.util.Date;
 5 /*
 6  把String转换成Date对象
 7 */
 8 public class DateFormatMethod2 {
 9     public static void main(String[] args) throws ParseException {
10         DateFormat df = new SimpleDateFormat("yyyy年MM月dd日");
11         String str = "2018年12月11日";
12         Date date = df.parse(str);
13         System.out.println(date); // Tue Dec 11 00:00:00 CST 2018
14     }
15 }

 

  注意:在使用 parse 方法的时候声明了一个异常叫做 ParseException,如果字符串和构造方法的模式不一样,那么就会抛出此异常。这里使用 throws 继续抛出由 JVM 虚拟机来处理。

三、Calendar 类

  1、概述

    java.util.Calendar 是日历类,在 Date 后出现,替换掉了许多 Date 的方法。

    该类将所有可能用到的时间信息封装为静态成员变量,方便获取。日历类就是方便获取各个时间属性的。

  2、获取方式

    Calendar 为抽象类,由于语言敏感性,Calendar 类在创建对象时并非直接创建,而是通过静态方法创建,返回子类对象。

    Calendar 静态方法:

public static Calendar getInstance()`:使用默认时区和语言环境获得一个日历

     Demo

1 import java.util.Calendar;
2 
3 public class CalendarInit {
4     public static void main(String[] args) {
5         Calendar cal = Calendar.getInstance();
6     }    
7 }

  3、常用方法

public int get(int field):返回给定日历字段的值
public void set(int field, int value):将给定的日历字段设置为给定值
public abstract void add(int field, int amount):根据日历的规则,为给定的日历字段添加或减去指定的时间量
public Date getTime():返回一个表示此Calendar时间值(从历元到现在的毫秒偏移量)的Date对象。

    Calendar 类中提供很多成员常量,代表给定的日历字段:

字段值 含义
YEAR
MONTH 月(从0开始,可以+1使用)
DAY_OF_MONTH 月中的天(几号)
HOUR 时(12小时制)
HOUR_OF_DAY 时(24小时制)
MINUTE
SECOND
DAY_OF_WEEK 周中的天(周几,周日为1,可以-1使用)

   get/set 方法

    get 方法用来获取指定字段的值;set 方法用来设置指定字段的值。

    Demo:

 1 import java.util.Calendar;
 2 
 3 public class CalendarUtil {
 4     public static void main(String[] args) {
 5         // 创建Calendar对象
 6         Calendar cal = Calendar.getInstance();
 7         // 获取年 
 8         int year = cal.get(Calendar.YEAR);
 9         // 获取月
10         int month = cal.get(Calendar.MONTH) + 1;
11         // 获取日
12         int dayOfMonth = cal.get(Calendar.DAY_OF_MONTH);
13         System.out.print(year + "年" + month + "月" + dayOfMonth + "日");
14         // 设置年
15         cal.set(Calendar.YEAR, 2020);
16         System.out.print(year + "年" + month + "月" + dayOfMonth + "日"); // 2020年8月26日
17     }    
18 }

   add 方法

    add 方法可以对指定日历字段的值进行加减操作,如果第二个参数为整数则加上偏移量,如果为负数则减去偏移量。

   Demo:

 1 import java.util.Calendar;
 2 
 3 public class CalendarMethod {
 4     public static void main(String[] args) {
 5         Calendar cal = Calendar.getInstance();
 6         System.out.print(year + "年" + month + "月" + dayOfMonth + "日"); // 2019年8月26日
 7         // 使用add方法
 8         cal.add(Calendar.DAY_OF_MONTH, 2); // 加2天
 9         cal.add(Calendar.YEAR, -3); // 减3年
10         System.out.print(year + "年" + month + "月" + dayOfMonth + "日"); // 2016年8月28日; 
11     }
12 }

   getTime 方法

    Calendar 中的 getTime 方法并不是获取毫秒时刻,而是拿到对应的 Date 对象。

  Demo:

 1 import java.util.Calendar;
 2 import java.util.Date;
 3 
 4 public class Demo09CalendarMethod {
 5     public static void main(String[] args) {
 6         Calendar cal = Calendar.getInstance();
 7         Date date = cal.getTime();
 8         System.out.println(date); // 输出当前日期时间
 9     }
10 }

   Tips:西方星期的开始为周日,在 Calendar 类中,月份的表述是以 0-11 代表1-12 月。

Guess you like

Origin www.cnblogs.com/niujifei/p/11414410.html