The use of Java date and time related classes


The Date class is used to represent time and date. Since internationalization was not considered during development, the Calendar class and DateFormat class were newly designed later .

One, Date class

1.Date class overview

  • The Date class represents a specific instant, accurate to the millisecond.
  • The Date class is passedOrigin of time( 1970.01.01 00:00:00:000) AndMillisecond value( long类型) To calculate time.
  • The Date class comes from the java.util package, and the package needs to be imported when using it.

2. Common methods of Date class

Construction method

public Date()
Allocate a Date object and initialize this object to indicate the time (accurate to the millisecond) when it was allocated.

//打印当前系统日期
Date d = new Date();
System.out.println(d);

public Date(long date)
A Date object is allocated and initialized to represent the specified number of milliseconds since the standard base time (called the "epoch", which is 00:00:00 GMT on January 1, 1970).

//打印从标准基准时间以后的指定毫秒数的对应时间。
Date d = new Date(0L);
System.out.println(d);

Member method

long getTime()
Returns the number of milliseconds represented by this Date object since January 1, 1970 00:00:00 GMT.

//打印从基准时间到当前时间的毫秒数
Date d = new Date();
System.out.println(d.getTime());

Second, the Calendar class

1. Overview of the Calendar class

  • The Calendar class is an abstract class.
  • Provides fields related to the calendar.
  • The getInstance method of Calendar returns a Calendar object whose calendar fields have been initialized with the current date and time.

2.Calendar commonly used methods

Let's take a look at the commonly usedCalendar field constant
Insert picture description here

Member method

int get(int field)
Parameters: Pass a specific calendar field.
Return value: Returns the value of the given calendar field.

//使用getInstance()创建对象
Calendar c = Calendar.getInstance();
//获取当前年份
int year = c.get(Calendar.YEAR);
//获取当前月份(西方月份从 0 开始,因此需要 + 1)
int month = c.get(Calendar.MONTH) + 1;
//获取当前日期
int date = c.get(Calendar.DATE);

System.out.println(year+"年"+month+"月"+date+"日");

void set(int field, int value)
Parameters: Pass the calendar field.
           Pass a value to the specified field

//使用getInstance()创建对象
Calendar c = Calendar.getInstance();
//单独设置年月日
c.set(Calendar.YEAR,2000);
c.set(Calendar.MONTH,02);
c.set(Calendar.DATE,20);
//可以同时设置 年、月、日
c.set(2200,03,05);
//设置完之后可用get方法取出,不在演示

abstract void add(int field, int amount)
Parameters: Pass the calendar field.
           Increase or decrease the value (positive numbers increase, negative numbers decrease)

//使用getInstance()创建对象
Calendar c = Calendar.getInstance();
//减少指定年份
c.add(Calendar.YEAR,-3);
//增加指定月份
c.add(Calendar.MONTH,3);

Date getTime()
Returns a Date object representing the time value of this Calendar (the millisecond offset from the epoch to the present).

//创建日历对象
Calendar c = Calendar.getInstance();
//调用getTime方法
Date date = c.getTime();
System.out.println(date);

Three, DateFormat class

1. Overview of the DateFormat class

  • The DateFormat class is an abstract class of date/time formatting subclasses (必须使用它的子类SimpleDateFormat类 来创建对象)。
  • The DateFormat class can 完成日期和文本之间的转换.
  • The DateFarmat class comes from the java.text package, and the package needs to be imported when using it.

2. Date and time format

A little understanding, there will be specific use in the following code

Insert picture description here
Insert picture description here

3.[Simple]DateFormat class commonly used methods

SimpleDateFormat construction method

SimpleDateFormat(String pattern)
Construct SimpleDateFormat with the given pattern and the date format symbols of the default locale.

//规定需要转换的日期格式
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");

Member methods of DateFormat

String format(Date date)
Format a Date as a date/time string.

//规定需要转换的日期格式
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
//获取当前日期
Date d = new Date();
//日期转文本
String str = sdf.format(d);
System.out.println(str);

Date parse(String source)
Parse the text from the beginning of the given string to generate a date .

//规定需要转换的日期格式
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
//文本转日期
Date date = sdf.parse("2020年07月15日 19时04分40秒");
System.out.println(date);

Guess you like

Origin blog.csdn.net/weixin_44580492/article/details/107367202