Java date tool class use
Timestamp concept
Timestamp refers to the total number of seconds since January 01, 1970, 00:00:00 GMT (Beijing time, January 1, 1970, 08:00:00 Beijing time). It is essentially an integer of type long, representing a date object. Timestamps in the Java system are in milliseconds.
public class Timechuo {
public static void main(String[] args) {
long t=System.currentTimeMillis();
System.out.println(t); //输出时间戳
System.out.printf("%tF %<tT%n",0L);//格式化时间戳-格林威治时间(中国)
System.out.printf("%tF %<tT%n",t);//格式化时间戳
}
}
运行结果:
1676080307174
1970-01-01 08:00:00
2023-02-11 09:51:47
java.util.Date
The parameterless construction method of the Date class obtains the current time of the system, and the displayed order is week, month, day, hour, minute, second, and year.
The Date class identifies a system-specific timestamp that can be accurate to milliseconds. The default order represented by the Data object is week, month, day, hour, minute, second, year.
The Data class has the following two construction methods:
Date(): This form indicates that the Date object is allocated and initialized to represent the time of allocation (accurate to milliseconds
). The object created using this construction method can obtain the local current time. Date(long date): This form indicates the number of milliseconds specified by the parameter date
from 0:00:00 on January 1, 1970, GMT time (Greenwich Mean Time) .
Date d1=new Date();
Date d2=new Date(System.currentTimeMillis());
Date d3=new Date(1676081510116l);//指定时间戳
System.out.println(d2);
//格式化
System.out.printf("%tF %<tT%n",d1);
System.out.printf("%tF %<tT%n",d2);
System.out.printf("%tF %<tT%n",d3);
//常用方法
long now=d1.getTime();
System.out.println(now);//当前时间戳
d1.setTime(now+(1000*60*60*24*10));//十天后的时间
System.out.printf("%tF %<tT%n",now);//当前时间
System.out.printf("%tF %<tT%n",d1);//十天后的时间
运行结果:
Sat Feb 11 10:39:57 CST 2023
2023-02-11 10:39:57
2023-02-11 10:39:57
2023-02-11 10:11:50
1676083197622
2023-02-11 10:39:57
2023-02-21 10:39:57
Date date1 = new Date(); // 调用无参数构造函数 注:要手动导包
System.out.println(date1.toString()); // 输出:Sat Feb 11 09:37:30 CST 2023
Date date2 = new Date(60000); // 调用含有一个long类型参数的构造函数
System.out.println(date2); // 输出格林威治时间(中国)+60000毫秒后的时间:Thu Jan 01 08:01:00 CST 1970
运行结果:
Sat Feb 11 09:37:30 CST 2023
Thu Jan 01 08:01:00 CST 1970
The parameterless construction method of the Date class obtains the current time of the system, and the displayed order is week, month, day, hour, minute, second, and
year.
The construction method of the Date class with a long type parameter obtains the time specified in milliseconds from GMT, 60000 milliseconds is one minute
, and the difference between GMT (Greenwich Mean Time) and CST (Central Standard Time) is 8 hours, that is to say, 00:00:00 GMT on January 1, 1970 and 08:00:00 CST on January 1, 1970 represent the same time
.
So one minute from January 1, 1970 00:00:00 CST is January 1, 1970 00:01:00 CST, which is represented as Thu Jan 01
08:01:00 CST 1970 using a Date object.
method | describe |
---|---|
booleanafter(Date when) | Determine whether this date is after the specified date |
boolean before(Datewhen) | Determine whether this date is before the specified date |
int compareTo(DateanotherDate) | compares the order of two dates |
boolean equals(Object obj) | Compares two dates for equality |
long getTime() | Returns the number of milliseconds this Date object represents since January 1, 1970 00:00:00 GMT |
String toString() | Convert this Date object to a String of the form: dow mon dd hh:mm:ss zzzyyyy. where dow is the day of the week (Sun, Mon, Tue, Wed, Thu, Fri, and Sat) |
java.util.Calendar
The Calendar class is an abstract class that
provides methods for converting between specific instants and calendar fields such as YEAR, MONTH, DAY_OF_MONTH, HOUR, and for manipulating calendar fields such as getting the next week's date.
The new keyword cannot be used to create a Calendar object, because the Calendar class is an abstract class, but it provides a
getInstance() method to obtain an object of the Calendar class. The getInstance() method returns a Calendar object whose calendar fields
have been initialized with the current date and time.
Calendar c = Calendar.getInstance();
After creating a Calendar object, you can process the date and time through some methods in the Calendar object.
Common methods of the Calendar class are shown in the table.
The Calendar object can call the set() method to turn the calendar to any time. When the parameter year is negative, it means BC.
Call the get() method of the Calendar object to obtain time information such as year, month, day, etc., and the effective value of the parameter field is specified by the static constant of Calendar
.
Many constants are defined in the Calendar class, which represent different meanings.
Calendar.YEAR: Year.
Calendar.MONTH: The month.
Calendar.DATE: Date.
Calendar.DAY_OF_MONTH: Date, which has exactly the same meaning as the above fields.
Calendar.HOUR: The hour in 12-hour format.
Calendar.HOUR_OF_DAY: The hour in 24-hour format.
Calendar. MINUTE: Minutes.
Calendar.SECOND: seconds.
Calendar.DAY_OF_WEEK: The day of the week.
For example, to get the current month use the following code:
int month = Calendar.getInstance().get(Calendar.MONTH);
If the value of the integer variable month is 0, it means that the current calendar is in January; if the value is 11, it means that the current calendar is in December
.
public static void main(String[] args) {
/*Calendar c=Calendar.getInstance();
System.out.println(c);*/
// System.out.printf("%tF %<tT%n",System.currentTimeMillis());
int month = Calendar.getInstance().get(Calendar.MONTH);//当前时间
System.out.println(month);//输出月
}
运行结果:
2023-02-11 11:13:53
1
Calendar calendar=Calendar.getInstance();// 如果不设置时间,则默认为当前时间
calendar.setTime(new Date());// 如果不设置时间,则默认为当前时间
System.out.println("现在时刻:"+calendar.getTime());// 获取当前时间
int year=calendar.get(Calendar.YEAR);// 获取当前年份
System.out.println("现在是"+year+"年");
int month=calendar.get(Calendar.MONTH)+1;// 获取当前月份(月份从 0 开始,所以加 1)
System.out.println(month+"月");
int day=calendar.get(Calendar.DATE);// 获取日
System.out.println(day+"日");
int week = calendar.get(Calendar.DAY_OF_WEEK) - 1; // 获取今天星期几(以星期日第一天)
System.out.println("星期" + week);
int hour = calendar.get(Calendar.HOUR_OF_DAY); // 获取当前小时数(24 小时制)
System.out.println(hour + "时");
int minute = calendar.get(Calendar.MINUTE); // 获取当前分钟
System.out.println(minute + "分");
int second = calendar.get(Calendar.SECOND); // 获取当前秒数
System.out.println(second + "秒");
int millisecond = calendar.get(Calendar.MILLISECOND); // 获取毫秒数
System.out.println(millisecond + "毫秒");
int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH); // 获取今天是本月第几天
System.out.println("今天是本月的第 " + dayOfMonth + " 天");
int dayOfWeekInMonth = calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH);// 获取今天是本月第几周
System.out.println("今天是本月第 " + dayOfWeekInMonth + " 周");
int many = calendar.get(Calendar.DAY_OF_YEAR); // 获取今天是今年第几天
System.out.println("今天是今年第 " + many + " 天");
Calendar c1 = Calendar.getInstance();
c1.set(2012, 8, 8); // 设置年月日,时分秒将默认采用当前值
System.out.println("设置日期为 2012-8-8 后的时间:" + c1.getTime()); // 输出时间上面的示例代码演示了 Calendar 类中的方法与常量的结合使用,从而完成处理日期的操作。
var c = Calendar.getInstance();
// c.set();
// c.get();
System.out.println(c.get(Calendar.HOUR_OF_DAY));//获取小时
//System.out.println(new Date().getHours());
// c.add();
//设置-7天,获取7天前的日期
c.add(Calendar.DATE, -7);
//日历类转换为Date类
Date d = c.getTime();
//日历类获取时间戳
long ts = c.getTimeInMillis();
System.out.printf("%tF %<tT%n", c);
运行结果:
现在时刻:Sat Feb 11 11:40:49 CST 2023
现在是2023年
2月
11日
星期6
11时
40分
49秒
220毫秒
今天是本月的第 11 天
今天是本月第 2 周
今天是今年第 42 天
设置日期为 2012-8-8 后的时间:Sat Sep 08 11:46:02 CST 2012
11
2023-02-04 11:46:02
java8 DateAPI
LocalDate
dk1.8 新的日期API java.time.LocalDate类 java.util.Date Calendar
此类只有年月日
public static void main(String[] args) {
//实例化
LocalDate d1=LocalDate.now();
var d2=LocalDate.of(1985,10,17);
//常用操作
System.out.println(d1);
//今日5天后日期
System.out.println(d1.plusDays(5));
//今日2天前日期
System.out.println(d1.minusDays(3));
//今日25个月后的日期
System.out.println(d1.plus(25, ChronoUnit.MONTHS));
//今日日期的月份 二月
var msn=d1.getMonth().getDisplayName(TextStyle.FULL, Locale.CHINESE);
System.out.println(msn);
//今日 星期六
var wsn = d1.getDayOfWeek().getDisplayName(TextStyle.FULL,Locale.CHINESE);
System.out.println(wsn);
//今日 周六
var sn = d1.getDayOfWeek().getDisplayName(TextStyle.SHORT,Locale.CHINESE);
System.out.println(sn);
//计算两个日期之间的天数
long days = ChronoUnit.DAYS.between(d2,d1);
System.out.printf("%s 到 %s 之间有 %d 天",d2,d1,days);
System.out.println();
//格化输出
System.out.println(d1);
System.out.println(d2);
System.out.printf("%tF%n",d1);
System.out.printf("%tF%n",d2);
System.out.printf("%tY年%<tm月%<td日 %<tA%n",d1);
System.out.printf("%tY年%<tm月%<td日 %<tA%n",d2);
System.out.println(d1.isLeapYear());
//专业的格式工具类java.time.DateTimeFormatter
//var fmt = DateTimeFormatter.ofPattern("yyyy年MM月dd日 E ML");
var fmt = DateTimeFormatter.ofPattern("yyyy年MM月dd日 E M L",Locale.CHINA);
System.out.println(fmt.format(d1));
System.out.println(fmt.format(d2));
}
运行结果:
2023-02-11
2023-02-16
2023-02-08
2025-03-11
二月
星期六
周六
1985-10-17 到 2023-02-11 之间有 13631 天
2023-02-11
1985-10-17
2023-02-11
1985-10-17
2023年02月11日 星期六
1985年10月17日 星期四
false
2023年02月11日 周六 2 2
1985年10月17日 周四 10 10
LocalTime
java.time.LocalTime
public static void main(String[] args) {
//LocalTime 实例化
var t1= LocalTime.now();
var t2=LocalTime.of(14,30,20);
var t3=LocalTime.parse("15:25:35", DateTimeFormatter.ofPattern("HH:mm:ss"));
//简单输出
System.out.println(t1);
System.out.printf("%tT%n",t1);
System.out.println(t2);
System.out.println(t3);
//格式化输出
DateTimeFormatter df=DateTimeFormatter.ofPattern("HH时mm分ss秒");
System.out.println(df.format(t1));
System.out.println(df.format(t2));
System.out.println(df.format(t3));
System.out.println(t1.plusSeconds(1000));
var sns=t1.plusSeconds(1000).format(DateTimeFormatter.ofPattern("HH:mm:ss"));
System.out.println(sns);
}
运行结果:
14:48:39.341209100
14:48:39
14:30:20
15:25:35
14时48分39秒
14时30分20秒
15时25分35秒
15:05:19.341209100
15:05:19
LocalDateTime
java.time.LocalDateTime
This class contains information about the year, month, day, hour, minute, and second, which is equivalent to LocalDateTime = LocalDate + LocalTime. It is
equivalent to the previous java.util.Date, and also equivalent to the java.util.Calendar class
public static void main(String[] args) {
var d1 = LocalDateTime.now();
System.out.println(d1);
System.out.printf("%tF %<tT %n",d1);
System.out.printf("%tF%n",d1);
System.out.printf("%tT%n",d1);
System.out.printf("%tY年%<tm月%<td日 %<tH:%<tM:%<tS %<tA %<tp %s %n",d1,d1.getMonth().getDisplayName(TextStyle.FULL, Locale.CHINA));
//将字符串转换为LocalDateTime
var d2 = LocalDateTime.of(2022,10,17,14,49,50);
System.out.println(d2);
System.out.println(d2.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
System.out.println("--------------------");
String str = "2022-03-15 20:30:40";
var d3 = LocalDateTime.parse(str, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
System.out.println(d3);
}
运行结果:
2023-02-11T15:37:05.435471500
2023-02-11 15:37:05
2023-02-11
15:37:05
2023年02月11日 15:37:05 星期六 下午 二月
2022-10-17T14:49:50
2022-10-17 14:49:50
--------------------
2022-03-15T20:30:40