1、当前时间的换时间戳
System.out.println(System.currentTimeMillis());//精确到毫秒 System.out.println(System.currentTimeMillis()/1000); //精确到秒
2、获取昨天当前时间的时间戳
Calendar calendar = Calendar.getInstance(); calendar.add(Calendar.DAY_OF_YEAR,-1); long millisecond=calendar.getTime().getTime();//精确到毫秒 long timestamp=calendar.getTime().getTime()/1000;//精确到秒 System.out.println(millisecond); System.out.println(timestamp);
3、获取今天零点时间戳
long time=System.currentTimeMillis(); long millisecond=time/(1000*3600*24)*(1000*3600*24)- TimeZone.getDefault().getRawOffset(); System.out.println(millisecond/1000);//精确到秒 System.out.println(millisecond);//精确到毫秒
4、获取昨天零点的时间戳
Calendar calendar = Calendar.getInstance(); calendar.set(calendar.get(Calendar.YEAR),calendar.get(Calendar.MARCH),calendar.get(Calendar.DAY_OF_MONTH)-1,0,0,0); long millisecond=calendar.getTime().getTime();//精确到毫秒 long timestamp = calendar.getTime().getTime()/1000;//精确到秒 System.out.println(millisecond); System.out.println(timestamp);
5、获取今天23点59分59秒的时间戳
Calendar calendar=Calendar.getInstance(); calendar.set(calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH),23,59,59); long millisecond = calendar.getTime().getTime();//精确到毫秒 long timestamp=calendar.getTime().getTime()/1000;//精确到秒 System.out.println(millisecond); System.out.println(timestamp);
6、获取昨天23:59:59秒的时间戳
Calendar calendar=Calendar.getInstance(); calendar.set(calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH)-1,23,59,59); long millisecond = calendar.getTime().getTime();//精确到毫秒 long timestamp=calendar.getTime().getTime()/1000;//精确到秒 System.out.println(millisecond); System.out.println(timestamp);
7、获取七天前零点的时间戳(不包含当天)
Calendar calendar=Calendar.getInstance(); calendar.set(calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH)-7,0,0,0);//时间参数根据需要修改 long millisecond = calendar.getTime().getTime();//精确到毫秒 long timestamp=calendar.getTime().getTime()/1000;//精确到秒 System.out.println(millisecond); System.out.println(timestamp);
8、获取三十天前23点59分59秒的时间戳(不包含当天)
Calendar calendar=Calendar.getInstance(); calendar.set(calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH)-30,23,59,59);//时间参数根据需要修改 long millisecond = calendar.getTime().getTime();//精确到毫秒 long timestamp=calendar.getTime().getTime()/1000;//精确到秒 System.out.println(millisecond); System.out.println(timestamp);
9、获取本月第一天零点的时间戳
Calendar calendar=Calendar.getInstance(); calendar.set(calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),1,0,0,0); long millisecond = calendar.getTime().getTime();//精确到毫秒 long timestamp=calendar.getTime().getTime()/1000;//精确到秒 System.out.println(millisecond); System.out.println(timestamp);
10、获取上月第一天零点的时间戳
Calendar calendar=Calendar.getInstance(); calendar.set(calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH)-1,1,0,0,0); long millisecond = calendar.getTime().getTime();//精确到毫秒 long timestamp=calendar.getTime().getTime()/1000;//精确到秒 System.out.println(millisecond); System.out.println(timestamp);
11、获取本月最后一天23点59分59秒的时间戳
Calendar calendar=Calendar.getInstance(); calendar.set(calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH),23,59,59); calendar.set(Calendar.DAY_OF_MONTH,calendar.getActualMaximum(Calendar.DAY_OF_MONTH)); long millisecond = calendar.getTime().getTime();//精确到毫秒 long timestamp=calendar.getTime().getTime()/1000;//精确到秒 System.out.println(millisecond); System.out.println(timestamp);
12、获取上月最后一天23点59分59秒的时间戳
Calendar calendar=Calendar.getInstance(); calendar.set(calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH)-1, calendar.get(Calendar.DAY_OF_MONTH),23,59,59); calendar.set(Calendar.DAY_OF_MONTH,calendar.getActualMaximum(Calendar.DAY_OF_MONTH)); long millisecond = calendar.getTime().getTime();//精确到毫秒 long timestamp=calendar.getTime().getTime()/1000;//精确到秒 System.out.println(millisecond); System.out.println(timestamp);
13、获取指定时间的时间戳
try { System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS").parse("2021-11-28 13:19:44:111").getTime());//精确到毫秒 System.out.println(new SimpleDateFormat("yyyy/MM/dd HH:mm:ss:SSS").parse("2021/11/28 13:19:44:111").getTime());//精确到毫秒 System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS").parse("2021-11-28 13:19:44:111").getTime());//精确到秒 System.out.println(new SimpleDateFormat("yyyy/MM/dd HH:mm:ss:SSS").parse("2021/11/28 13:19:44:111").getTime());//精确到秒 } catch (ParseException e) { e.printStackTrace(); }