java获得本日,本月,本周,本年第一天0点0分0秒的13位时间戳
时间: 2024-04-30 10:22:40 浏览: 60
获取本日第一天时间戳:
```java
// 获取当前时间
Date now = new Date();
// 获取本日第一天日期
Calendar calendar = Calendar.getInstance();
calendar.setTime(now);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
Date today = calendar.getTime();
// 转换为13位时间戳
long timestamp = today.getTime();
```
获取本月第一天时间戳:
```java
// 获取当前时间
Date now = new Date();
// 获取本月第一天日期
Calendar calendar = Calendar.getInstance();
calendar.setTime(now);
calendar.set(Calendar.DAY_OF_MONTH, 1);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
Date firstDayOfMonth = calendar.getTime();
// 转换为13位时间戳
long timestamp = firstDayOfMonth.getTime();
```
获取本周第一天时间戳:
```java
// 获取当前时间
Date now = new Date();
// 获取本周第一天日期
Calendar calendar = Calendar.getInstance();
calendar.setTime(now);
calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
Date firstDayOfWeek = calendar.getTime();
// 转换为13位时间戳
long timestamp = firstDayOfWeek.getTime();
```
获取本年第一天时间戳:
```java
// 获取当前时间
Date now = new Date();
// 获取本年第一天日期
Calendar calendar = Calendar.getInstance();
calendar.setTime(now);
calendar.set(Calendar.MONTH, Calendar.JANUARY);
calendar.set(Calendar.DAY_OF_MONTH, 1);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
Date firstDayOfYear = calendar.getTime();
// 转换为13位时间戳
long timestamp = firstDayOfYear.getTime();
```
阅读全文