Date date = new Date();
// output: Mon Oct 24 03:00:48 CST 2022
System.out.println(date);
}
}
>
> 🍀 打印出来的时间⏰非常国际范(不符合中国人的阅读习惯😀)
> 🍀 Mon:表示星期一
> 🍀 Oct:表示十月份
> 🍀 CST:China Standard Time(中国标准时间)
>
>
>
📝 给 Date 类的构造方法传一个 **long** 类型的参数的话,表示的是:格林尼治时间的`1970年1月1日0时0分0秒`加上参数**毫秒**后的时间
public class TestDemo {
public static void main(String[] args) {
Date date = new Date(1000);
// Thu Jan 01 08:00:01 CST 1970
System.out.println(date);
}
}
>
> 🍀 上面的代码:传入【1000】作为 Date 类的参数,表示的是格林尼治时间的`1970年1月1日0时0分0秒`过**1000毫秒**后的时间
> 🍀 1000毫秒等于1秒,所以是:Thu Jan 01 08:00:01 CST 1970
> 🍀 打印出来的时间是**08**,因为中国比格林尼治时间快八个小时(时区)
>
>
>
📝 `System.currentTimeMillis()`返回的是从格林尼治时间的`1970年1月1日0时0分0秒`到此时此刻所经历的毫秒数(即时间戳)
public class TestDemo {
public static void main(String[] args) {
Date d1 = new Date(System.currentTimeMillis());
Date d2 = new Date();
// true: d1 和 d2 表示的是同一个时间
System.out.println(d1.equals(d2));
}
}
## 二、Date 常用方法
public class TestDemo {
public static void main(String[] args) {
Date d1 = new Date(System.currentTimeMillis());
Date d2 = new Date();
// 设置毫秒数
d1.setTime(1000);
d2.setTime(2000);
// 获取毫秒数
long d1Mills = d1.getTime();
long d2Mills = d2.getTime();
// 比较时间
boolean ret1 = d2.after(d1); // output: true
boolean ret2 = d1.before(d2); // output: true
int ret3 = d1.compareTo(d2); // output: -1
}
}
## 三、SimpleDateFormat
📝 `java.text.SimpleDateFormat`常用来做日期的格式化处理(重要)
public class TestDemo {
public static void main(String[] args) throws ParseException {
String s = “2011-08-13 19:35:38”;
String e = “2023-08-13 19:35:38”;System.out.println(GqDates.inThisTimeRange(s, e));
}
}
class GqDates {
public static final String YMDHMS_24_DATE_STR = “yyy-MM-dd HH:mm:ss”;
public static final String YMDHMS_12_DATE_STR = “yyy-MM-dd hh:mm:ss”;
public static final String MILL_DATE_STR = “yyyyMMddHHmmssS”;/\*\** 生成毫秒时间字符串(如 20221024034702656)
* 该字符串可简单看做一个【唯一字符串】
*/
public static String curMillTimeStr() {
return createCurTimeString(MILL_DATE_STR);
}/\*\** 生成时间字符串(二十四小时制)
*/
public static String curTimeStr24() {
return createCurTimeString(YMDHMS_24_DATE_STR);
}/\*\** 生成时间字符串(十二小时制)
*/
public static String curTimeStr12() {
return createCurTimeString(YMDHMS_12_DATE_STR);
}/\*\** 生成指定格式的, 当前时间的字符串
*
* @param pattern 日期格式
*/
public static String createCurTimeString(String pattern) {
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
return sdf.format(new Date());
}/\*\** 把指定格式的日期字符串转换为日期对象 (Date)
*
* @param str 日期字符串
* @param pattern 日期格式
* @return 日期对象
*/
public static Date string2DateObj(String str, String pattern) throws ParseException {
return new SimpleDateFormat(pattern).parse(str);
}/\*\** 获取某个时间字符串的毫秒值(时间戳)
* 时间戳查询网:http://shijianchuo.wiicha.com/
*/
public static long getMills(String dateStr, String pattern) throws ParseException {
Date date = string2DateObj(dateStr, pattern);
return date.getTime();
}/\*\** 判断当前时间是否在某段时间范围内
*/
public static boolean inThisTimeRange(String start, String end) {
try {
long s = getMills(start, YMDHMS_24_DATE_STR);
long e = getMills(end, YMDHMS_24_DATE_STR);
long cur = System.currentTimeMillis();
return s < cur && cur < e;
} catch (ParseException e) {
e.printStackTrace();
return false; }