Java-时间处理
写在前面,关于Java方面的时间处理,最近有用到,头痛了一定的时间,所以写此文,与君共赏。
(1)指定时间 加上/减去 X天 时间、时间戳
@Test
public void getYMD() throws Exception {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd "); // 日期格式
//日期格式
Date date2 = dateFormat.parse(dateFormat.format(new Date()));
Date newDate2 = addDate(date2,3);
System.out.println("未格式化--->"+newDate2);
System.out.println("格式化后--->" + dateFormat.format(newDate2));//返回时间日期
System.err.println("时间戳--->" + newDate2.getTime());
}
未格式化—>Mon Jul 08 00:00:00 CST 2019
格式化后—>2019-07-08
时间戳—>1562515200000
@Test
public void YMDHMS() throws Exception {
System.out.println("当前时间(年月日-时分秒) 加上3天---->" + addDate(new Date(), 3));
System.out.println("当前时间(年月日-时分秒) 加上3天 时间戳---->" + addDate(new Date(), 3).getTime());
//SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd ");
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("当前时间(年月日-时分秒) 加上3天---->" + addDate(dateFormat.parse(dateFormat.format(new Date())), 3));
System.out.println("当前时间(年月日-时分秒) 加上3天 时间戳---->" +addDate(dateFormat.parse(dateFormat.format(new Date())), 3).getTime());
}
当前时间(年月日-时分秒) 加上3天---->Mon Jul 08 11:11:20 CST 2019
当前时间(年月日-时分秒) 加上3天 时间戳---->1562555480371
当前时间(年月日-时分秒) 加上3天---->Mon Jul 08 11:11:20 CST 2019
当前时间(年月日-时分秒) 加上3天 时间戳---->1562555480000
//公共方法
public static Date addDate(Date date,long day) throws ParseException {
long time = date.getTime(); // 得到指定日期的毫秒数
day = day*24*60*60*1000; // 要加上的天数转换成毫秒数
time+=day; // 相加得到新的毫秒数
return new Date(time); // 将毫秒数转换成日期
}
(2)获取当前时间、时间戳、以及转换为年月日格式、年月日时间戳
@Test
public void testYMD() throws Exception {
Timestamp nowTimestamp = new Timestamp(new Date().getTime());
System.out.println("当前时间:"+ nowTimestamp);
System.out.println("当前时间戳:" + nowTimestamp.getTime());
Date nowDate= dateTest(nowTimestamp);
System.out.println("转换后当前时间,显示年月日"+nowDate);//转换为 年月日格式 时分秒皆为0
System.out.println("转换后当前时间戳,显示年月日"+nowDate.getTime());//转换为 年月日格式 时分秒皆为0
}
//公共方法---时间戳 转换为 Date(年月日)
private static Date dateTest(Timestamp timestamp) throws ParseException {
//判断,时间,当前系统时间 是否 为今天以内的数据有多少条
long time = timestamp.getTime();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String format1 = format.format(time);
Date date = format.parse(format1);//转换 年月日 格式
return date;
}
结果:
当前时间:2019-07-03 11:57:14.874
当前时间戳:1562126234874
转换后当前时间,显示年月日Wed Jul 03 00:00:00 CST 2019
转换后当前时间戳,显示年月日1562083200000
此为(2.1)未抽取原法
@Test
public void testName() throws Exception {
long now1 = System.currentTimeMillis();//当前系统时间戳
System.out.println(now1); //1559050875168
Date date = new Date();
Timestamp nousedate = new Timestamp(date.getTime());//当前系统时间戳 转换格式是 年月日时分秒了
System.out.println(nousedate); //2019-05-28 21:41:15.168
Timestamp nowTime = new Timestamp(new Date().getTime());
System.out.println(nowTime); //2019-05-28 21:41:15.168
long time = nousedate.getTime();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String format1 = format.format(nowTime);
Date date2 = format.parse(format1);//转换 年月日 格式
System.out.println(date2); //Tue May 28 00:00:00 CST 2019 格式化为年月日
}
(3)两种方式,获取当前时间前7天 OR 后 7天
可扩展为 获取当前时间的前X天 OR 后X天
@Test
public void getOldTime() throws Exception {
Date now = new Date();
System.out.println("当前时间:" + now);
System.out.println("当前时间戳:" + now.getTime());
// 使用Calendar
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DAY_OF_MONTH, -7);
Date oldDate = calendar.getTime();
System.out.println("7天前:" + oldDate);
long oldMillis = calendar.getTimeInMillis();
System.out.println("7天前时间戳:" + oldMillis);
// 使用时间戳减毫秒数---第二种方式
long oldMillis2 = now.getTime() - 7 * 24 * 60 * 60 * 1000L;
System.out.println("7天前时间戳:" + oldMillis2);
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");//年与日
// SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");//年月日,时分秒
String format1 = format.format(oldMillis2);
Date date = format.parse(format1);//转换
System.out.println("7天前日期-年月日:" + date);
}
结果:
当前时间:Fri Jul 05 09:26:47 CST 2019
当前时间戳:1562290007979
7天前:Fri Jun 28 09:26:48 CST 2019
7天前时间戳:1561685208010
7天前时间戳:1561685207979
7天前日期-年月日:Fri Jun 28 00:00:00 CST 2019
(3.1)简洁写法
@Test
public void testEasy() throws Exception {
Timestamp startTime = new Timestamp(new Date().getTime());
// Date date = new Date();
System.err.println("startTime--->" + startTime);
long startTime2 = startTime.getTime();//将时间戳转换为毫秒数
// long nowTime = new Date().getTime();//获取当前系统时间的毫秒数
System.out.println("startTime2---->"+startTime2);
long endTime = startTime2 + (7 * 24 * 60 * 60 * 1000L);//L 防溢出
// long endTime = startTime2 - (7 * 24 * 60 * 60 * 1000L);//L 防溢出
System.out.println("endTime 加上7天--->"+endTime);
}
MySql-常用函数,请点击此处查看
My-Sql-时间函数,请点击此处查看
在线转换时间戳工具
Java时间格式转换文章链接