根据Long类型时间戳获取当前年份并得到当前年份最后一天的时间戳
程序员文章站
2022-05-05 14:49:00
...
package com.it.test;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class Timer2Test {
public static void main(String[] args) {
Date date = new Date(1578021723000L);//传过来的具体时间的时间戳
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy");
String format = simpleDateFormat.format(date);
Integer year = Integer.valueOf(format);
System.out.println(year);
Calendar calendar = Calendar.getInstance();
calendar.clear();
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.HOUR_OF_DAY, 23);
calendar.set(Calendar.MINUTE, 59);
calendar.set(Calendar.SECOND, 59);
calendar.set(Calendar.MILLISECOND, 999);
calendar.roll(Calendar.DAY_OF_YEAR, -1);
long timeInMillis = calendar.getTimeInMillis();
System.out.println(timeInMillis);
//时间戳转成String类型的时间
SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date2 = new Date(timeInMillis);
String format1 = simpleDateFormat2.format(date2);//将时间调整为yyyy-MM-dd HH:mm:ss时间样式
System.out.println("时间戳转成format"+format1);
}
}
结果
下一篇: JSoup爬虫技术