import org.joda.time.DateTime;
import java.io.*;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
public class Test {
public static void main(String[] args) {
Test test = new Test();
Long baseTime = DateUtil.convertStrToDate("2010-01-01 00:00:00:000",DateUtil.DATE_TIME_FULL_DIVISION).getTime();
Date tmp = DateUtil.convertStrToDate("2019-01-03 10:00:00:000",DateUtil.DATE_TIME_FULL_DIVISION);
long start = System.currentTimeMillis();
for (int i = 0; i < 1; i++) {
System.out.println(DateUtil.convertDateToStr(test.getStartTimeOfDay(tmp),DateUtil.DATE_TIME_DIVISION));
System.out.println(DateUtil.convertDateToStr(test.getStartTimeOfDay(tmp,baseTime),DateUtil.DATE_TIME_DIVISION));
System.out.println(DateUtil.convertDateToStr(test.getEndTimeOfDay(tmp),DateUtil.DATE_TIME_FULL_DIVISION));
}
System.out.println("耗时:"+(System.currentTimeMillis()-start));
}
/**
* 获取一天的开始时间,
* 默认基准时间2000-01-01 00:00:00:000
* @param date 某一天时间
* @return 某一天的开始时间,比如2005-01-01 00:00:00.000
*/
public long getStartTimeOfDay(Date date) {
if(date == null){
date = new Date();
}
return this.getStartTimeOfDay(date,null);
}
/**
* 获取一天的开始时间,如果自己指定基准时间,请注意时区问题,
* 推荐使用@see getStartTimeOfDay(Date date)
* @param date 某一天时间
* @return 某一天的开始时间,比如2005-01-01 00:00:00.000
*/
public long getStartTimeOfDay(Date date,Long baseTime){
if(date == null){
date = new Date();
}
if(baseTime == null){
//默认基准时间2000-01-01 00:00:00:000
baseTime = DateUtil.convertStrToDate("2000-01-01 00:00:00:000",DateUtil.DATE_TIME_FULL_DIVISION).getTime();
}
long interval = date.getTime()-baseTime;
return interval/(86400000)*(86400000)+baseTime;
}
/**
* 获取一天的最后时间点
* @param date 某一天时间
* @return 某一天的结束时间,比如2005-01-01 23:59:59.999
*/
public long getEndTimeOfDay(Date date) {
if(date == null){
date = new Date();
}
return this.getStartTimeOfDay(date)+86400000-1;
}
}
某天开始时间和结束时间
最新推荐文章于 2023-11-29 23:41:25 发布