Introduce the column first
This column is my own journey of learning Java, pure hand-knocked code, I followed the dark horse course to learn, and added some of my own understanding, and made
appropriate modifications to the code and notes. I hope it can be helpful to everyone, and at the same time, please supervise me, give suggestions on the code I wrote, and learn from each other.
Date method
The Date class is a class used to represent dates and times. It provides a series of methods to manipulate dates and times.
common method
1. Construction method
Date(): Creates a Date object representing the current date and time.
Date(long milliseconds): Creates a Date object based on the specified number of milliseconds.
2. How to get the date and time:
getTime(): Returns the number of milliseconds since January 1, 1970 00:00:00 GMT.
example
long milliseconds = specificDate.getTime();
getYear(): Returns the year of the current date (counting from 1900).
getMonth(): Returns the month of the current date (0 means January, 11 means December).
getDate(): Returns the number of days in the current date (1 means the first day).
getDay(): Returns the day of the week of the current date (0 means Sunday, 6 means Saturday).
getHours(): Returns the hours (0-23) of the current time.
getMinutes(): Returns the minutes (0-59) of the current time.
getSeconds(): Returns the seconds (0-59) of the current time.
3. How to set the date and time:
setTime(long milliseconds): Set the date and time represented by the Date object.
setYear(int year): Set the year of the current date.
setMonth(int month): Set the month of the current date.
setDate(int day): Set the number of days in the current date.
setHours(int hours): Set the hours of the current time.
setMinutes(int minutes): Set the minutes of the current time.
setSeconds(int seconds): Set the seconds of the current time.
#### 4. Other methods:
toString(): Returns the string representation of the Date object.
example
String dateString = specificDate.toString();
before(Date date): Determine whether the current date is before the specified date.
example
boolean isBefore = specificDate.before(currentDate);
after(Date date): Determine whether the current date is after the specified date.
example
boolean isAfter = specificDate.after(currentDate);```
equals(Object obj): Determine whether the current date is equal to the specified object.
Comprehensive case
the code
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class crj {
public static void main(String[] args) {
//Date():
Date d1 = new Date();
System.out.println(d1);
//Date(long date): 1000(毫秒)*60*60 一小时
Date d2 = new Date(1000 * 60 * 60);
System.out.println(d2);
Date d = new Date();
// SimpleDateFormat sdf = new SimpleDateFormat();
SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
String s = sdf.format(d);
System.out.println(s);
System.out.println("-------");
//解析
String ss="2021-10-27 11:11:11";
// SimpleDateFormat sdf2=new SimpleDateFormat("2021年10月27日 11:11:11");
SimpleDateFormat sdf2=new SimpleDateFormat("2021-10-27 11:11:11");
Date dd= null;
try {
dd = sdf2.parse(ss);
} catch (ParseException e) {
e.printStackTrace();
}
System.out.println(dd);
}
}
result
Tue Aug 08 07:50:35 CST 2023
Thu Jan 01 09:00:00 CST 1970
2023年08月08日 07:50:35
-------
Thu Jan 01 00:00:00 CST 1970