常用的时间段的时间戳

  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);
相关推荐
曾经的三心草2 分钟前
基于正倒排索引的Java文档搜索引擎2-实现Index类
java·python·搜索引擎
JienDa1 小时前
JienDa聊PHP:CSDN博客仿站实战中PHP框架的协同架构方略
java·架构·php
大迪吃小迪1 小时前
每秒 400 请求场景下,线程池如何合理配置?
java·开发语言
雨中飘荡的记忆2 小时前
财务对账系统设计与实现
java
0***h9422 小时前
使用 java -jar 命令启动 Spring Boot 应用时,指定特定的配置文件的几种实现方式
java·spring boot·jar
雨中飘荡的记忆2 小时前
布式事务详解:从理论到实践(RocketMQ + Seata)
java·rocketmq
i***48612 小时前
微服务生态组件之Spring Cloud LoadBalancer详解和源码分析
java·spring cloud·微服务
zzlyx992 小时前
用C#采用Avalonia+Mapsui在离线地图上插入图片画信号扩散图
java·开发语言·c#
Aevget2 小时前
MyEclipse全新发布v2025.2——AI + Java 24 +更快的调试
java·ide·人工智能·eclipse·myeclipse
一 乐2 小时前
购物|明星周边商城|基于springboot的明星周边商城系统设计与实现(源码+数据库+文档)
java·数据库·spring boot·后端·spring