Java基础 时间相关类[Timestamp,Date,Calendar]
前言
记录一下与时间相关的几个java基础类,这里JDK的版本为1.8
一、Timestamp(时间戳)
- 时间戳是什么?
时间戳是指格林威治时间自1970年1月1日(00:00:00 GMT)至某个时间的总秒数。通俗的讲,时间戳是一份能够表示一份数据在一个特定时间点已经存在的完整的可验证的数据。
- java时间戳的初始化
查看Timestamp的源码,可以知道,Timestamp的构造函数到1.8只剩下了一个,源码如下
public Timestamp(long time) {
super((time/1000)*1000);
nanos = (int)((time%1000) * 1000000);
if (nanos < 0) {
nanos = 1000000000 + nanos;
super.setTime(((time/1000)-1)*1000);
}
}
根据构造函数可知需要使用一个long类型的数字来初始化TimeStamp,
long类型为8字节,每字节8big,能表示-2的64次方 至 2的64次方-1(即-9223372036854775808到9223372036854775807),正数部分转换成年为292271023.05年,所以是足够的
既然初始化TimeStamp需要一个long型的参数,那么我们先从这个long型的参数入手,当然我们不可能一年份一年份的去算这个毫秒数,所以需要通过已有的api来得到这个参数,以下三种方式都可以获得自1970年1月1日到当前的时间的毫秒数。
long t1 = System.currentTimeMillis();
long t2 = new Date().getTime();//等同于第一种不推荐使用
long t3 = System.currentTimeMillis();
那么初始化TimeStamp就可以应用以上三个long值来进行初始化,其中第二种方式使用Date来获取毫秒数,查看Date构造函数源码这个方法的底层依旧是调用的System.currentTimeMillis(),所以不推荐使用,源码如下
public Date() {
this(System.currentTimeMillis());
}
- 时间戳初始化
Timestamp time1 = new Timestamp(System.currentTimeMillis());
Timestamp time2 = new Timestamp(Calendar.getInstance().getTimeInMillis());
二、Date(日期)
Date可能是日常使用比较频繁的一个类,常用语日期按照特定的格式转换成字符串(配合SimpleDateFormat),查看jdk 1.8版本的源码可知
目前初始化Date的构造方法剩两种,其中无参构造生成的Date表示当前时间,有参构造需传入一个long值来得到一个Date(这点与TimeStamp一致)
三、Calendar(日历)
Calender的构造主要依据,TimeZone zone(时区), Locale(语言环境值)
四、Timestamp、Date、Calendar的转换
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class TimeCase {
public static void main(String[] args) throws ParseException {
//字符串转Date
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String timeStr = "2021-07-07 16:30:00";
Date time = sdf.parse(timeStr);
//字符串转Timestamp
Timestamp timestamp = Timestamp.valueOf(timeStr);
//Date转Calender
Calendar calendar = Calendar.getInstance();
calendar.setTime(time);
//Calender转Date
Date date1 = calendar.getTime();
//Date Calender 转Timestamp可使用上述说的,Timestamp构造函数来构造
Timestamp time1 = new Timestamp(System.currentTimeMillis());
Timestamp time2 = new Timestamp(new Date().getTime());//不推荐使用
Timestamp time3 = new Timestamp(Calendar.getInstance().getTimeInMillis());
}
}
五、日期之间的比较
- Date的After,Before函数
- Date的compareTo函数
A.compareTo(B)
如果A和B时间相同,结果为0
A早于B,结果为负数
A晚于B,结果为正数
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class TimeCase {
public static void main(String[] args) throws ParseException {
//日期的比较
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar nowCalender = Calendar.getInstance();
nowCalender.setTime(new Date());
//这里需要注意不可以用nowCalender对象直接赋值到yesterdayCalender,因为这里直接赋值实际上赋的是对象的引用而不是复制
Calendar yesterdayCalender = (Calendar) nowCalender.clone();
yesterdayCalender.add(Calendar.DATE, -1);
Calendar tomorrowCalender = (Calendar) nowCalender.clone();
tomorrowCalender.add(Calendar.DATE, 1);
Date now = nowCalender.getTime();
Date yesterday = yesterdayCalender.getTime();
Date tomorrow = tomorrowCalender.getTime();
System.out.println("now:" + simpleDateFormat.format(now));
System.out.println("yesterday:" + simpleDateFormat.format(yesterday));
System.out.println("tomorrow:" + simpleDateFormat.format(tomorrow));
System.out.println("---------------Date的before,after方法---------------");
System.out.println("now比yesterday早:" + now.before(yesterday));
System.out.println("now比tomorrow早:" + now.before(tomorrow));
System.out.println("now比yesterday晚:" + now.after(yesterday));
System.out.println("now比tomorrow晚:" + now.after(tomorrow));
System.out.println("---------------Date的compareTo方法---------------");
System.out.println("now.compareTo(now):" + now.compareTo(now));
System.out.println("now.compareTo(yesterday):" + now.compareTo(yesterday));
System.out.println("now.compareTo(tomorrow):" + now.compareTo(tomorrow));
}
}
运行结果: