1, Date class
1. Introduction in this section
(1) Two constructors of Date class (no participation and parameter)
(2) A member method getTime of the Date class
(3) Two member methods of SimpleDateFormat, a subclass of the format class DateFormat (abstract class): format method and parse method
2. Construction method of date class:
(1) No parameter Date (): can be used to access the current system time
import java.util.Date;
public class Date class {
public static void main(String[] args) {
Date date=new Date();
System.out.println(date);
}
}
Operation results:
Wed Nov 10 15:06:13 CST 2021
(2) Have reference Date (long date): you can access the time after a specified number of milliseconds since the base time (January 1, 1970, 00:00:00 GMT)
import java.util.Date;
public class Date class {
public static void main(String[] args) {
Date date=new Date(0L);
System.out.println(date);
}
}
Operation results:
Thu Jan 01 08:00:00 CST 1970
3. getTime method of Date class: returns the number of milliseconds represented by this Date object since 00:00:00 GMT, January 1, 1970
import java.util.Date;
public class Date class {
public static void main(String[] args) {
Date date=new Date();
long time=date.getTime();
System.out.println(time);
}
}
Operation results:
1636528911694
4. Format DateFormat class (abstract class):
(1) Subclass SimpleDateFormat
<1> Format method
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* y year
* M month
* d day
* H Time
* m branch
* s second
*/
public class Date class {
public static void main(String[] args) {
SimpleDateFormat sdf=new SimpleDateFormat("yyyy year MM month dd day HH Time mm branch ss second");
Date date=new Date();
String d=sdf.format(date);
System.out.println(date);
System.out.println(d);
}
}
Operation results:
Wed Nov 10 15:32:21 CST 2021
2021 November 10, 2013 15:32:21
<2> Parse method: parse the string into a Date and return the Date object of the Date
Note: the string format should follow the SimpleDateFormat format, otherwise an error will be reported; To perform exception handling
import java.util.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
public class Date class {
public static void main(String[] args) throws ParseException {
SimpleDateFormat sdf=new SimpleDateFormat("yyyy year MM month dd day HH Time mm branch ss second");
Date date=sdf.parse("2021 November 10, 2013 15:32:21");
System.out.println(date);
}
}
Operation results:
Wed Nov 10 15:32:21 CST 2021
2, Calendar calendar class (abstract class)
1. This section describes:
(1) Calendar abstract class create object method
(2) Fields of the Calendar class
(3) Four member methods of the Calendar class
2. Reason for occurrence: because internationalization was not considered at the beginning of the design of Date class, most methods are outdated. Now most of them use Calendar class
3. Create object (application): use getInstance method
Calendar c=Calendar.getInstance();
4.Calendar class member method
The field s are as follows:
Field name | explain |
DATE | The field numbers of get and set indicate a day of the month |
DAY_OF_MOUTH |
The field numbers of get and set indicate a day of the month |
DAY_OF_WEEK | The field numbers of get and set indicate a day of the week |
DAY_OF_WEEK_IN_MONTH | The field numbers of get and set indicate the week ordinal of the current month |
DAY_OF_YEAR | The field numbers of get and set indicate the number of days in the current year |
HOUR | Field numbers for get and set, indicating the hours in the morning or afternoon |
HOUR_OF_DAY | The field numbers of get and set indicate the hours of the day |
MILLISECOND | Field numbers for get and set, indicating milliseconds in a second |
MINUTE | The field numbers of get and set indicate the minutes in an hour |
MOUNT | get and set numbers indicating the month |
SECOND | The field number of get and set, indicating the second of one minute |
time | The current setting time of the calendar, in milliseconds, represents the elapsed time after 00:00:00 on January 1, 1970 |
WEEK_OF_MONTH | The field numbers of get and set indicate the number of weeks in the current month |
WEEK_OF_YEAR | The field numbers of get and set indicate the number of weeks in the current year |
YEAR | Field number indicating get and set for the year |
(1) public int get(int field) gets the value of the field in the calendar
public class Date class {
public static void main(String[] args) {
Calendar c=Calendar.getInstance();
int year=c.get(Calendar.YEAR);
System.out.println(year);
int month=c.get(Calendar.MONTH);
System.out.println(month);//Month from 0-11
int date=c.get(Calendar.DATE);
System.out.println(date);//10 day
System.out.println("Today is"+year+"year"+(month+1)+"month"+date+"day");
}
}
Operation results:
2021
10
10
Today is November 10, 2021
(2) public void set(int field,int value) sets the time for the calendar
import java.util.Calendar;
public class Date class {
public static void main(String[] args) {
Calendar c=Calendar.getInstance();
c.set(Calendar.YEAR,2001);
c.set(Calendar.MONTH, 10);
c.set(Calendar.DATE,02);
int year=c.get(Calendar.YEAR);
int month=c.get(Calendar.MONTH);
int date=c.get(Calendar.DATE);
System.out.println(year+"year"+month+"month"+date+"day");
}
}
Operation results:
2001 October 2
(3) Public abstract void add (int file D, int amount) adds or subtracts the specified time from the given calendar field
import java.util.Calendar;
public class Date class {
public static void main(String[] args) {
Calendar c=Calendar.getInstance();
c.add(Calendar.YEAR, 5);
int year=c.get(Calendar.YEAR);
System.out.println("The current time is increased by 5 years:"+year);
}
}
Operation results:
The current time is increased by 5 years: 2026
(4) public Date getTime() converts the calendar to a date
public class Date class {
public static void main(String[] args) {
Calendar c=Calendar.getInstance();
Date date=c.getTime();
System.out.println(date);
}
}
Operation results:
Wed Nov 10 16:59:25 CST 2021