JAVA时间计算方法

时间:2025-03-10 08:24:12
  • /**
  • * 获取当天零点的时间戳
  • * @return
  • */
  • public static Long getTodayZero(){
  • Long currentTimestamps=();
  • Long oneDayTimestamps= (60*60*24*1000);
  • return (currentTimestamps-(currentTimestamps+60*60*8*1000)%oneDayTimestamps)/1000;
  • }
  • /**
  • * 获取次日点的时间戳
  • * @return
  • */
  • public static Long getTomorrowZero(){
  • Long currentTimestamps=();
  • Long oneDayTimestamps= (60*60*24*1000);
  • Long todayZero = (currentTimestamps-(currentTimestamps+60*60*8*1000)%oneDayTimestamps);
  • return (todayZero+oneDayTimestamps)/1000;
  • }
  • /**
  • * 获取昨天零点的时间戳(秒) 返回值单位是秒
  • * @return
  • */
  • public static Long getYesterdayZero(){
  • Long currentTimestamps=();
  • Long oneDayTimestamps= (60*60*24*1000);
  • Long todayZero = (currentTimestamps-(currentTimestamps+60*60*8*1000)%oneDayTimestamps);
  • return (todayZero-oneDayTimestamps)/1000;
  • }
  • /**
  • * 获得当月1号零时零分零秒
  • * @return
  • */
  • public static Long getInitDateByMonth(){
  • Calendar calendar = ();
  • (new Date());
  • (Calendar.DAY_OF_MONTH, 1);
  • (Calendar.HOUR_OF_DAY, 0);
  • (, 0);
  • (, 0);
  • return ()/1000;
  • }
  • /**
  • * 获取指定日期所在年份开始的时间戳 传入 秒
  • * @return
  • */
  • public static Long getYearBegin(Long curentTime) {
  • Date date = new Date(curentTime*1000);
  • Calendar c = ();
  • (date);
  • (,0);
  • (Calendar.DAY_OF_MONTH, 1);
  • (Calendar.HOUR_OF_DAY, 0);
  • (, 0);
  • (, 0);
  • (, 0);
  • return ()/1000;
  • }
  • /**
  • * 获取指定日期所在年份结束的时间戳 传入秒
  • * @return
  • */
  • public static Long getYearEnd(Long curentTime) {
  • Date date = new Date(curentTime*1000);
  • Calendar c = ();
  • (date);
  • (,11);
  • (Calendar.DAY_OF_MONTH, 31);
  • (Calendar.HOUR_OF_DAY, 23);
  • (, 59);
  • (, 59);
  • (, 999);
  • return ()/1000;
  • }
  • /**
  • * 获取指定日期所在月份开始的时间戳 传入秒
  • * @return
  • */
  • public static Long getMonthBegin(Long curentTime) {
  • Date date = new Date(curentTime*1000);
  • Calendar c = ();
  • (date);
  • (Calendar.DAY_OF_MONTH, 1);
  • (Calendar.HOUR_OF_DAY, 0);
  • (, 0);
  • (, 0);
  • (, 0);
  • return ()/1000;
  • }
  • //根据传入数值获取对应年份的开始时间
  • public static Long getYearByYear(int year){
  • //yyyy-MM-dd HH:mm:ss
  • String s=year+"-01-01 00:00:00";
  • Long res;
  • SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  • Date date = null;
  • try {
  • date = (s);
  • } catch (ParseException e) {
  • ("",e);
  • ();
  • }
  • long ts = ();
  • res = (ts)/1000;
  • return res;
  • }
  • //根据传入年月获取年月的开始时间
  • public static Long getYearByYearMonth(int year,int month){
  • //yyyy-MM-dd HH:mm:ss
  • String s=year+"-"+month+"-01 00:00:00";
  • Long res;
  • SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  • Date date = null;
  • try {
  • date = (s);
  • } catch (ParseException e) {
  • ("",e);
  • ();
  • }
  • long ts = ();
  • res = (ts)/1000;
  • return res;
  • }
  • /**
  • * 获取下个月的开始时间
  • * @param curentTime
  • * @return
  • */
  • public static Long getNextMonthBegin(Long curentTime, Integer size) {
  • Date date = new Date(curentTime*1000);
  • Calendar c = ();
  • (date);
  • (Calendar.DAY_OF_MONTH, 1);
  • (, size);
  • (Calendar.HOUR_OF_DAY, 0);
  • (, 0);
  • (, 0);
  • (, 0);
  • return ()/1000;
  • }
  • //获取传入时间 size天后的时间
  • public static Long getNextTodayBegin(Long curentTime, Integer size) {
  • Long compareTime = curentTime + 86400*size;
  • return compareTime;
  • }
  • public static Long getNextYearBegin(Long curentTime,Integer size) {
  • Date date = new Date(curentTime*1000);
  • Calendar c = ();
  • (date);
  • (, size);
  • (,0);
  • (Calendar.DAY_OF_MONTH, 1);
  • (Calendar.HOUR_OF_DAY, 0);
  • (, 0);
  • (, 0);
  • (, 0);
  • return ()/1000;
  • }
  • /**
  • * 获取指定日期所在月份结束的时间戳
  • * @return
  • */
  • public static Long getMonthEnd(Long curentTime) {
  • Date date = new Date(curentTime*1000);
  • Calendar c = ();
  • (date);
  • //设置为当月最后一天
  • (Calendar.DAY_OF_MONTH, (Calendar.DAY_OF_MONTH));
  • //将小时至23
  • (Calendar.HOUR_OF_DAY, 23);
  • //将分钟至59
  • (, 59);
  • //将秒至59
  • (, 59);
  • //将毫秒至999
  • (, 999);
  • // 获取本月最后一天的时间戳
  • return ()/1000;
  • }
  • /**
  • * 获得当年1月1号零时零分零秒
  • * @return
  • */
  • public static Long getInitDateByYears(){
  • Date date = new Date();
  • GregorianCalendar gc = (GregorianCalendar) ();
  • (date);
  • Integer nowYear = ((1));
  • Calendar calendar = ();
  • (new Date());
  • (,nowYear);
  • (,0);
  • (Calendar.DAY_OF_MONTH, 1);
  • (Calendar.HOUR_OF_DAY, 0);
  • (, 0);
  • (, 0);
  • return ()/1000;
  • }
  • public static Long getTodayZero(Long currentTimestamps){
  • Long oneDayTimestamps= (60*60*24*1000);
  • currentTimestamps=currentTimestamps*1000;
  • return (currentTimestamps-(currentTimestamps+60*60*8*1000)%oneDayTimestamps)/1000;
  • }
  • public static Long getTodayEnd(Long currenttime){
  • Date today = new Date(currenttime*1000);
  • Calendar calendar = ();
  • (today);
  • (Calendar.HOUR_OF_DAY, 23);
  • (, 59);
  • (, 59);
  • return ()/1000;
  • }
  • //获取出入时间的昨天时间
  • public static Long getYesterdayZero(Long curentTime){
  • Date date = new Date(curentTime*1000);
  • Calendar c = ();
  • (date);
  • return ()/1000 -86400;
  • }
  • /*
  • * 将时间戳转换为时间
  • */
  • public static String stampToDate(long s){
  • String res;
  • SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  • Date date = new Date(s*1000);
  • res = (date);
  • return res;
  • }
  • /*
  • * 将时间转换为时间戳
  • */
  • public static Long dateToStamp(String s) {
  • Long res;
  • SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  • Date date = null;
  • try {
  • date = (s);
  • } catch (ParseException e) {
  • ("",e);
  • ();
  • }
  • long ts = ();
  • res = (ts)/1000;
  • return res;
  • }