获取当天的开始时间、结束时间等等的一个工具类

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Date工具类
 */
public class Dateutil {

/**
     * 当天的开始时间
     * @return
     */
    public static long startOfTodDay() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        Date date=calendar.getTime();
        return date.getTime();
    }
    /**
     * 当天的结束时间
     * @return
     */
    public static long endOfTodDay() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        Date date=calendar.getTime();
        return date.getTime();
    }
    /**
     * 昨天的开始时间
     * @return
     */
    public static long startOfyesterday() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.add(Calendar.DATE, -1);
        calendar.set(Calendar.MILLISECOND, 0);
        Date date=calendar.getTime();
        return date.getTime();
    }
    /**
     * 昨天的结束时间
     * @return
     */
    public static long endOfyesterday() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        calendar.add(Calendar.DATE, -1);
        Date date=calendar.getTime();
        return date.getTime();
    }

/**
     * 功能:获取上周的开始时间
     */
    public static long startOfLastWeek() {// 当周开始时间
        return startOfThisWeek() - 7 * 24 * 60 * 60 * 1000;
    }

/**
     * 功能:获取上周的结束时间
     */
    public static long endOfLastWeek() {// 当周开始时间
        return endOfThisWeek() - 7 * 24 * 60 * 60 * 1000;
    }

/**
     * 功能:获取本周的开始时间 示例:2013-05-13 00:00:00
     */
    public static long startOfThisWeek() {// 当周开始时间
        Calendar currentDate = Calendar.getInstance();
        currentDate.setFirstDayOfWeek(Calendar.MONDAY);
        currentDate.set(Calendar.HOUR_OF_DAY, 0);
        currentDate.set(Calendar.MINUTE, 0);
        currentDate.set(Calendar.SECOND, 0);
        currentDate.set(Calendar.MILLISECOND, 0);
        currentDate.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        Date date=currentDate.getTime();
        return date.getTime();
    }

/**
     * 功能:获取本周的结束时间 示例:2013-05-19 23:59:59
     */
    public static long endOfThisWeek() {// 当周结束时间
        Calendar currentDate = Calendar.getInstance();
        currentDate.setFirstDayOfWeek(Calendar.MONDAY);
        currentDate.set(Calendar.HOUR_OF_DAY, 23);
        currentDate.set(Calendar.MINUTE, 59);
        currentDate.set(Calendar.SECOND, 59);
        currentDate.set(Calendar.MILLISECOND, 999);
        currentDate.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
        Date date=currentDate.getTime();
        return date.getTime();
    }

/**
     * 功能:获取本月的开始时间
     */
    public static long startOfThisMonth() {// 当周开始时间
        Calendar currentDate = Calendar.getInstance();
        currentDate.set(Calendar.HOUR_OF_DAY, 0);
        currentDate.set(Calendar.MINUTE, 0);
        currentDate.set(Calendar.SECOND, 0);
        currentDate.set(Calendar.MILLISECOND, 0);
        currentDate.set(Calendar.DAY_OF_MONTH, 1);
        Date date=currentDate.getTime();
        return date.getTime();
    }

public static long endOfThisMonth() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.DAY_OF_MONTH, 1);
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        cal.set(Calendar.MILLISECOND, 999);
        cal.add(Calendar.MONTH, 1);
        cal.add(Calendar.DATE, -1);
        Date date=cal.getTime();
        return date.getTime();
    }
    
    /**
     * 功能:获取上月的开始时间
     */
    public static long startOfLastMonth() {// 当周开始时间
        Calendar currentDate = Calendar.getInstance();
        currentDate.set(Calendar.HOUR_OF_DAY, 0);
        currentDate.set(Calendar.MINUTE, 0);
        currentDate.set(Calendar.SECOND, 0);
        currentDate.set(Calendar.MILLISECOND, 0);
        currentDate.set(Calendar.DAY_OF_MONTH, 1);
        currentDate.add(Calendar.MONTH, -1);
        Date date=currentDate.getTime();
        return date.getTime();
    }
    /**
     * 功能:获取上月的结束时间
     */
    public static long endOfLastMonth() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.DAY_OF_MONTH, 1);
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        cal.set(Calendar.MILLISECOND, 999);
        cal.add(Calendar.DATE, -1);
        Date date=cal.getTime();
        return date.getTime();
    }

/**
     *  根据long返回year
     * @param milliseconds
     * @return
     */
    public static Object[] theYearOfTime(long milliseconds){
        Calendar cal = Calendar.getInstance();
        Date date=cal.getTime();
        int thisYear=date.getYear()+1900;
        cal.setTimeInMillis(milliseconds);
        date=cal.getTime();
        int regirsterYear=date.getYear()+1900;
        if(regirsterYear<thisYear){
            List<Integer> yearL=new ArrayList<Integer>();
            for(int i=regirsterYear;i<=thisYear;i++){
                yearL.add(i);
            }
            return yearL.toArray();
        }else{
            return new Object[]{thisYear};
        }
        
    }
    
    /**
     * 功能:获取本年的开始时间
     */
    public static long startOfTheYear(int year) {// 当周开始时间
        Calendar currentDate = Calendar.getInstance();
        currentDate.set(Calendar.YEAR, year);
        currentDate.set(Calendar.MONTH, 0);
        currentDate.set(Calendar.HOUR_OF_DAY, 0);
        currentDate.set(Calendar.MINUTE, 0);
        currentDate.set(Calendar.SECOND, 0);
        currentDate.set(Calendar.MILLISECOND, 0);
        currentDate.set(Calendar.DAY_OF_MONTH, 1);
        Date date=currentDate.getTime();
        return date.getTime();
    }
    /**
     * 功能:获取本年的开始时间
     */
    public static long endOfTheYear(int year) {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH,11);
        cal.set(Calendar.DAY_OF_MONTH, 31);
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        cal.set(Calendar.MILLISECOND, 999);
        Date date=cal.getTime();
        return date.getTime();
    }
}

原文地址:https://www.cnblogs.com/austinspark-jessylu/p/8406891.html

时间: 2024-10-12 19:08:13

获取当天的开始时间、结束时间等等的一个工具类的相关文章

php获取当天的开始时间和结束时间

php获取当天的开始时间和结束时间 //当天开始时间 $start_time=strtotime(date("Y-m-d",time())); //当天结束之间 $end_time=$start_time+60*60*24; echo $start_time; 1541001600 echo $end_time; 1541088000 $start_time=date("Y-m-d H:i:s",$start_time); $end_time=date("

JS时间戳比较大小:对于一组时间戳(开始时间~结束时间)和另一组时间戳进行比较,用于判断被比较时间戳组是否在要求范围内

/* *JS时间戳比较大小:对于一组时间戳(开始时间~结束时间)和另一组时间戳进行比较,用于判断被比较时间戳组是否在要求范围内 *@param date1 date2(形如:'2015-01-01'类型字符串) */ function compareDate(date1,date2){ //对获得的时间戳区间与既定的时间戳进行比对 var baseDate1='2015-01-01'; var baseDate2='2015-03-31'; baseDate1=new Date(baseDate

选定起始时间和结束时间,打印选定时间段类的每一年,每一月,每一天(我自己写的,转载注明版权哦)

最近做一个控件画图项目,需要提取和处理选定起止年月日时间段内的数据,那首先一步当然是要能编程实现访问选定时间段内的每一年的每个月的每一天,下面是我写的源码,说明一点,每个月我指定31天的,需要用的改一下闰年平年,还有月份的判断. 选定起始时间和结束时间,打印选定时间段类的每一年,每一月,每一天, String in_begin_date=request.getParameter("begin_date"); //起始日期 String in_end_date=request.getPa

java根据开始时间结束时间计算中间间隔日期

import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Calendar;import java.util.Date;import java.util.List; public class Test { /** * @title 根据开始时间,结束时间获取期间所有日期yyyy-MM-dd * 例:开始时间:2019-05-01 结束时

Extjs2.2 开始时间,结束时间,工期 联动(选二补一),包含日期,天数的互转

选择任意两个, 则第三个自动填写 文本框代码: var downtime_root_simple = { xtype : 'numberfield', fieldLabel : '工期(天)', name : 'plan_hours', id : 'plan_hours', allowBlank : true, readOnly : flag == 4 ? true : false, maxLength : 11, anchor : '95%', listeners : { change : f

bootstrap-datepicker 开始时间-结束时间 thinkphp

<!DOCTYPE html> <head> <title>开始-结束时间测试</title> </head> <body> <div> <p>方式一</p> <input type="text" name="datetimeStart" id="datetimeStart" value="{$map.datetimeSta

16、根据年月日获取周几 以及整理的日期常用的工具类

1 /** 2 * @param year 3 * @param month 4 * @param day 5 * @return 6 */ 7 public static String CaculateWeekDay(int y,int m,int d) 8 { 9 10 String strDate = y+"-"+m+"-"+d;// 定义日期字符串 11 SimpleDateFormat format = new SimpleDateFormat("

sql 获取当天开始时间 结束时间

2010-06-21 00:00:00.000 SELECT dateadd(ms,0,DATEADD(dd, DATEDIFF(dd,0,getdate()), 0)) 2010-06-21 23:59:59.997 SELECT dateadd(ms,-3,DATEADD(dd, DATEDIFF(dd,-1,getdate()), 0)) ms步频是3 当月第一天 SELECT DATEADD(mm, DATEDIFF(mm,0,getdate()), 0) 当月最后一天 SELECT  

javascript 中的时间戳转换时间 根据时间字符判断星期几 根据开始时间结束时间获取中间间隔时间 来自转发

//时间戳转换时间      function timedat(res){   //res 为传入的时间戳   例:1509091800000 var time = new Date(res); var y = time.getFullYear(); var m = time.getMonth()+1; var d = time.getDate(); return y+'-'+m+'-'+d;    //返回格式  "2017-10-27" 字符串    }; //根据时间判断星期几