常用的时间段的时间戳

  1. 获取 昨天这个时间的时间戳
java 复制代码
        Calendar calendar = Calendar.getInstance();     //当前时间
        calendar.add(Calendar.DAY_OF_YEAR,-1);  
        Long dd = calendar.getTime().getTime()/1000;
        System.out.println(dd);
  1. 计算今天0点的时间戳
java 复制代码
       Long  time = System.currentTimeMillis();  //当前时间的时间戳
       long zero = time/(1000*3600*24)*(1000*3600*24) - TimeZone.getDefault().getRawOffset();
       System.out.println(new Timestamp(zero));//今天零点零分零秒
       System.out.println(zero/1000);
  1. 计算今天23点59分59秒的时间戳
java 复制代码
     Calendar calendar = Calendar.getInstance();
     calendar.set(calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH),23,59,59);
     long tt = calendar.getTime().getTime()/1000;
     System.out.println(tt);
  1. 计算昨天0点的时间戳
java 复制代码
     Calendar calendar = Calendar.getInstance();
     calendar.set(calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH)-1,0,0,0);
     long tt = calendar.getTime().getTime()/1000;
     System.out.println(tt);
  1. 计算昨天23:59:59 秒的时间戳
java 复制代码
     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 tt = calendar.getTime().getTime()/1000;
     System.out.println(tt);
  1. 计算近7日 0点的时间戳(不包含当天)
java 复制代码
     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 tt = calendar.getTime().getTime()/1000;
     System.out.println(tt);
  1. 计算近30天 0点的时间戳(不包含当天)
java 复制代码
     Calendar calendar = Calendar.getInstance();
     calendar.set(calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH)-30,0,0,0);
     long tt = calendar.getTime().getTime()/1000;
     System.out.println(tt);
  1. 计算上月第一天 0点的时间戳
java 复制代码
     Calendar calendar = Calendar.getInstance();
     calendar.set(calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH)-1,1,0,0,0);
     long tt = calendar.getTime().getTime()/1000;
     System.out.println(tt);
  1. 计算上月最后一天 23点的时间戳
java 复制代码
    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 tt = calendar.getTime().getTime()/1000;
    System.out.println(tt)
  1. 计算上周周一 0点的时间戳
java 复制代码
     Calendar calendar = Calendar.getInstance();
     calendar.set(calendar.get(Calendar.YEAR),calendar.get(Calendar.MONDAY), calendar.get(Calendar.DAY_OF_MONTH), 0, 0,0);
     calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
     calendar.add(Calendar.DATE,-7);
     System.out.println(calendar.getTime().getTime()/1000);
  1. 计算上周周日23点的时间戳
java 复制代码
  Calendar calendar = Calendar.getInstance();
     calendar.set(calendar.get(Calendar.YEAR),calendar.get(Calendar.MONDAY), calendar.get(Calendar.DAY_OF_MONTH), 23, 59,59);
     calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
     calendar.add(Calendar.DATE,-1);
     System.out.println(calendar.getTime().getTime()/1000);
  1. 计算本月第一天0点的时间戳
java 复制代码
     Calendar calendar = Calendar.getInstance();
     calendar.set(calendar.get(Calendar.YEAR),calendar.get(Calendar.MONDAY), calendar.get(Calendar.DAY_OF_MONTH), 0, 0,0);
     calendar.set(Calendar.DAY_OF_MONTH,calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
     long tt = calendar.getTime().getTime()/1000;
    System.out.println(tt);
  1. 计算本月最后一天23:59:59的时间戳
java 复制代码
    Calendar calendar = Calendar.getInstance();
     calendar.set(calendar.get(Calendar.YEAR),calendar.get(Calendar.MONDAY), calendar.get(Calendar.DAY_OF_MONTH), 23, 59,59);
     calendar.set(Calendar.DAY_OF_MONTH,calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
     long tt = calendar.getTime().getTime()/1000;
     System.out.println(tt);
相关推荐
蓝染-惣右介8 分钟前
【23种设计模式·全精解析 | 行为型模式篇】11种行为型模式的结构概述、案例实现、优缺点、扩展对比、使用场景、源码解析
java·设计模式
秋恬意41 分钟前
IBatis和MyBatis在细节上的不同有哪些
java·mybatis
齐 飞1 小时前
BeanFactory和FactoryBean
java·sprint
大霞上仙1 小时前
lxml 解析xml\html
java·服务器·网络
Xiaoweidumpb2 小时前
tomcat temp临时文件不清空,占用硬盘,jdk字体内存泄漏
java·tomcat
AI人H哥会Java2 小时前
【Spring】控制反转(IoC)与依赖注入(DI)—IoC容器在系统中的位置
java·开发语言·spring boot·后端·spring
不能只会打代码2 小时前
Java并发编程框架之综合案例—— 分布式日志分析系统(七)
java·开发语言·分布式·java并发框架
自律的kkk2 小时前
SpringBoot中使用AOP切面编程实现登录拦截
java·spring boot·aop·切面编程·登录拦截
丁总学Java2 小时前
nohup java -jar productQualification.jar --spring.profiles.active=prod $
java·spring·jar