获得本年结束时间,本月结束时间,本周结束时间,今日结束时间的时间戳的java代码
时间: 2023-11-14 13:11:54 浏览: 91
以下是获取本年结束时间,本月结束时间,本周结束时间,今日结束时间的时间戳的Java代码:
```java
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class TimeStampExample{
public static void main(String args[]){
//获取本年结束时间
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.MONTH, 11); //12月份
calendar.set(Calendar.DAY_OF_MONTH, 31); //31日
Date yearEnd = calendar.getTime();
long yearEndTimeStamp = yearEnd.getTime();
System.out.println("本年结束时间戳:" + yearEndTimeStamp);
//获取本月结束时间
Calendar calendar2 = Calendar.getInstance();
calendar2.set(Calendar.DAY_OF_MONTH, calendar2.getActualMaximum(Calendar.DAY_OF_MONTH)); //本月最后一天
Date monthEnd = calendar2.getTime();
long monthEndTimeStamp = monthEnd.getTime();
System.out.println("本月结束时间戳:" + monthEndTimeStamp);
//获取本周结束时间
Calendar calendar3 = Calendar.getInstance();
calendar3.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY); //本周第一天为周日
calendar3.add(Calendar.DAY_OF_MONTH, 6); //本周最后一天为周六
Date weekEnd = calendar3.getTime();
long weekEndTimeStamp = weekEnd.getTime();
System.out.println("本周结束时间戳:" + weekEndTimeStamp);
//获取今日结束时间
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
String today = dateFormat.format(new Date());
String todayEndStr = today + " 23:59:59";
SimpleDateFormat dateTimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date todayEnd = dateTimeFormat.parse(todayEndStr);
long todayEndTimeStamp = todayEnd.getTime();
System.out.println("今日结束时间戳:" + todayEndTimeStamp);
} catch (Exception e) {
e.printStackTrace();
}
}
}
```
阅读全文