zoukankan      html  css  js  c++  java
  • 【Java基础】java 获得本日,本周,本月的时间戳区间

       如果涉及到统计的话,可能会经常用到。很简单的基础知识。

    1、getTimestampByOffsetDay

    	public static long getTimestampByOffsetDay(int day){
    		
    		Calendar calendar = Calendar.getInstance();
    		calendar.add(Calendar.DAY_OF_MONTH, day);
    		calendar.set(Calendar.HOUR_OF_DAY, 0);
    		calendar.set(Calendar.SECOND, 0);
    		calendar.set(Calendar.MINUTE, 0);
    		calendar.set(Calendar.MILLISECOND, 0);
    		
    		return calendar.getTimeInMillis();
    	}

    2、 getTodayTimestamp

    	public static HashMap<String, Object> getTodayTimestamp(){
    		
    		HashMap<String, Object> hashMap = new HashMap<String, Object>();
    		
    		hashMap.put("startTime", getTimestampByOffsetDay(0));
    		hashMap.put("endTime", getTimestampByOffsetDay(1));
    		
    		return hashMap;
    	}

    3、getWeekTimestamp

    	public static HashMap<String, Object> getWeekTimestamp() {
    
    		HashMap<String, Object> hashMap = new HashMap<String, Object>();
    
    		Calendar calendar = Calendar.getInstance();
    
    		hashMap.put(
    				"startTime",
    				getTimestampByOffsetDay(0 - calendar.get(Calendar.DAY_OF_WEEK) + 2));
    		hashMap.put(
    				"endTime",
    				getTimestampByOffsetDay(calendar
    						.getMaximum(Calendar.DAY_OF_WEEK)
    						- calendar.get(Calendar.DAY_OF_WEEK) + 1));
    
    		return hashMap;
    	}
     
     
    4、getMonthTimestamp
    	public static HashMap<String, Object> getMonthTimestamp() {
    
    		HashMap<String, Object> hashMap = new HashMap<String, Object>();
    
    		Calendar calendar = Calendar.getInstance();
    
    		hashMap.put(
    				"startTime",
    				getTimestampByOffsetDay(0 - calendar.get(Calendar.DAY_OF_MONTH) + 1));
    		hashMap.put(
    				"endTime",
    				getTimestampByOffsetDay(calendar
    						.getMaximum(Calendar.DAY_OF_MONTH)
    						- calendar.get(Calendar.DAY_OF_MONTH)));
    
    		return hashMap;
    	}



     

  • 相关阅读:
    使用Azure CLI实现自动关闭Azure虚拟机的脚本
    Azure自动化部署服务 (1)
    证书相关知识
    Azure上七层负载均衡APP Gateway
    简谈 Java 中的泛型通配符
    表单提交后为什么要重定向?
    eclipse 误删文件的恢复,代码的恢复
    书籍列表
    Mybatis Generator最完整配置详解
    学习spring mvc
  • 原文地址:https://www.cnblogs.com/kiwifly/p/4840676.html
Copyright © 2011-2022 走看看