public class TimeStampUtil {
//字符串时间转换为Unix时间戳
public static long dateToStamp(String year){
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy");
Date date;
try {
date = simpleDateFormat.parse(year);
return date.getTime();
} catch (ParseException e) {
e.printStackTrace();
}
return 0L;
}
//获取下一年01-01 00:00:00的时间戳
public static long getNextYear(long year){
Date date = new Date();
date.setTime(year);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.YEAR,1);
return calendar.getTimeInMillis();
}
//将获取的Unix时间戳转换为年份
public static String getStringYear(String time){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
String format = sdf.format(new Date(Long.valueOf(time)));
return format;
}
//将excel中的文本时间yyyy/MM/dd HH:mm:ss转换为Unix时间戳
public static long getLongTime(String time){
long changeTime = 0;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm");
try {
changeTime = simpleDateFormat.parse(time).getTime();
} catch (ParseException e) {
e.printStackTrace();
}
return changeTime;
}
/* public static void main(String[] args) {
System.out.println(getLongTime("2021/1/1 0:25:22"));
}*/
}
时间戳工具类
最新推荐文章于 2023-09-25 22:42:15 发布
这个Java类提供了几个静态方法,用于字符串时间到Unix时间戳的转换,获取下一年01-01 00:00:00的时间戳,以及将Unix时间戳转换回年份。方法包括dateToStamp、getNextYear和getStringYear,适用于时间处理操作。
摘要由CSDN通过智能技术生成