欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

时间处理函数工具分享(时间戳计算)

程序员文章站 2024-02-20 20:28:40
复制代码 代码如下:import java.text.parseexception;import java.text.simpledateformat;import jav...

复制代码 代码如下:

import java.text.parseexception;
import java.text.simpledateformat;
import java.util.calendar;
import java.util.date;
import java.util.gregoriancalendar;
import java.util.timezone;

/**
 * 时间处理函数
 *
 * @20080509 15:50
 */
public class dateutil {

 private static final string default_pattern = "yyyy-mm-dd hh:mm:ss";

 public static final string time_year = "yyyy";

 public static final string time_monen = "mm";

 public static final string time_day = "dd";

 public static string getdate(string interval, date starttime, string pattern) {
  calendar temp = calendar.getinstance(timezone.getdefault());
  temp.settime(starttime);
  temp.add(temp.month, integer.parseint(interval));
  simpledateformat sdf = new simpledateformat(pattern);
  return sdf.format(temp.gettime());
 }

 /**
  * 将字符串类型转换为时间类型
  *
  * @return
  */
 public static date str2date(string str) {
  date d = null;
  simpledateformat sdf = new simpledateformat(default_pattern);
  try {
   d = sdf.parse(str);
  } catch (exception e) {
   e.printstacktrace();
  }
  return d;
 }

 public static date str2date(string str, string pattern) {
  date d = null;
  simpledateformat sdf = new simpledateformat(pattern);
  try {
   d = sdf.parse(str);
  } catch (exception e) {
   e.printstacktrace();
  }
  return d;
 }

 /**
  * 将时间格式化
  *
  * @return
  */
 public static date datepattern(date date) {
  simpledateformat sdf = new simpledateformat(default_pattern);
  try {
   string dd = sdf.format(date);
   date = str2date(dd);
  } catch (exception e) {
   e.printstacktrace();
  }
  return date;
 }

 /**
  * 将时间格式化
  */
 public static date datepattern(date date, string pattern) {
  simpledateformat sdf = new simpledateformat(pattern);
  try {
   string dd = sdf.format(date);
   date = str2date(dd, pattern);
  } catch (exception e) {
   e.printstacktrace();
  }
  return date;
 }

 public static string date2str(date date) {
  simpledateformat sdf = new simpledateformat(default_pattern);
  return sdf.format(date);
 }

 public static string date2str(date date, string format) {
  simpledateformat sdf = new simpledateformat(format);
  return sdf.format(date);
 }

 /**
  * 获取昨天
  *
  * @param date
  * @return
  * @throws exception
  */
 public static date getlastdate(date date) {
  calendar calendar = calendar.getinstance(timezone.getdefault());
  calendar.settime(date);

  calendar.add(calendar.date, -1);

  return str2date(date2str(calendar.gettime()));
 }
 /**
  * 获取前几天
  * @param date
  * @return
  */
 public static date getbeforedate(date date,int dates) {
  calendar calendar = calendar.getinstance(timezone.getdefault());
  calendar.settime(date);

  calendar.add(calendar.date, -dates);

  return str2date(date2str(calendar.gettime()));
 }

 /**
  * 获取上周第一天(周一)
  *
  * @param date
  * @return
  * @throws exception
  */
 public static date getlastweekstart(date date) {
  calendar calendar = calendar.getinstance(timezone.getdefault());
  calendar.settime(date);
  int i = calendar.get(calendar.day_of_week) - 1;
  int startnum = 0;
  if (i == 0) {
   startnum = 7 + 6;
  } else {
   startnum = 7 + i - 1;
  }
  calendar.add(calendar.date, -startnum);

  return str2date(date2str(calendar.gettime()));
 }

 /**
  * 获取上周最后一天(周末)
  *
  * @param date
  * @return
  * @throws exception
  */
 public static date getlastweekend(date date) {
  calendar calendar = calendar.getinstance(timezone.getdefault());
  calendar.settime(date);
  int i = calendar.get(calendar.day_of_week) - 1;
  int endnum = 0;
  if (i == 0) {
   endnum = 7;
  } else {
   endnum = i;
  }
  calendar.add(calendar.date, -(endnum - 1));

  return str2date(date2str(calendar.gettime()));
 }

 /**
  * 根据年和月得到天数
  * @param num 月
  * @param year 年
  * @return
  */
 public static int getday(int num,int year){
  if(num==1 || num==3 || num==5 || num==7 || num==8 || num==10 || num==12){
   return 31;
  }else if(num==2){
   //判断是否为闰年
   if(year%400==0 || (year%4==0 && year%100!=0)){
    return 29;
   }else{
    return 28;
   }

  }else{
   return 30;
  }
 }
 /*
  * 计算当前日期距离下个月还有多少天
  */
 public static int getdaymis(date time){
  int year = integer.parseint(
    new simpledateformat(time_year).format(time));//年

  int mm = integer.parseint(
    new simpledateformat(time_monen).format(time));//月

  int dd = integer.parseint(
    new simpledateformat(time_day).format(time));//日

  
  //获取当前年月的总天数
  int sdd = getday(mm,year);

  return sdd-dd;

  
 }
 /**
  * 日期转秒数
  * @param datestring
  * @return
  */
 public static long gettime(string datestring) {
     long time = 0;
     try {
      date ret = null;
      simpledateformat sdf = new simpledateformat("yyyy-mm-dd hh:mm:ss");
      ret = sdf.parse(datestring);
      time = ret.gettime()/1000;
     } catch (exception e) {

     }
     return time;
    }

 
 /**
  * 精确计算时间差,精确到日
  * @param fistill 起始日期
  * @param nowtime 结束日期
  * @param type type为1返回年月日(如:2年3个月零5天) 否则返回总的天数
  * @return
  */
 public static string patienage(date fistill,date nowtime,integer type){

  int fyear = integer.parseint(
    new simpledateformat(time_year).format(fistill));//起始年

  int fmm = integer.parseint(
    new simpledateformat(time_monen).format(fistill));//起始月

  int fdd = integer.parseint(
    new simpledateformat(time_day).format(fistill));//起始日

  
  int nyear = integer.parseint(
    new simpledateformat(time_year).format(nowtime));//结束年

  int nmm = integer.parseint(
    new simpledateformat(time_monen).format(nowtime));//结束月

  int ndd = integer.parseint(
    new simpledateformat(time_day).format(nowtime));//结束日

  int cyear = nyear - fyear;
  int cmm = nmm - fmm;
  int cdd = ndd - fdd;

  
  int zyear = 0;
  int zmm = 0;
  int zdd = 0;

  int countddd = 0;  //年月日累计天数

  if(cdd<0){
   if(cmm<0){
    zyear = cyear - 1;
    zmm = (cmm + 12)-1;
    int dd = getday(zmm,nyear-1);
    zdd = dd + cdd;

    
    countddd = zyear*365+zmm*30+zdd;

   }else if(cmm==0){
    zyear = cyear - 1;
    zmm = 12-1;
    int dd = getday(zmm,nyear-1);
    zdd = dd + cdd;

    countddd = zyear*365+zmm*30+zdd;

   }else{
    zyear = cyear;
    zmm = cmm - 1;
    int dd = getday(zmm,nyear);
    zdd = dd + cdd;

    countddd = zyear*365+zmm*30+zdd;

   }
  }else if(cdd==0){
   if(cmm<0){
    zyear = cyear - 1;
    zmm = cmm + 12;
    zdd = 0;

    countddd = zyear*365+zmm*30;

   }else if(cmm==0){
    zyear = cyear;
    zmm = 0;
    zdd = 0;

    countddd = zyear*365+zmm*30;

   }else{
    zyear = cyear;
    zmm = cmm;
    zdd = 0;

    countddd = zyear*365+zmm*30;
   }
  }else{
   if(cmm<0){
    zyear = cyear - 1;
    zmm = cmm + 12;
    zdd = cdd;

    countddd = zyear*365+zmm*30+zdd;
   }else if(cmm==0){
    zyear = cyear;
    zmm = 0;
    zdd = cdd;

    countddd = zyear*365+zmm*30+zdd;
   }else{
    zyear = cyear;
    zmm = cmm;
    zdd = cdd;

    countddd = zyear*365+zmm*30+zdd;
   }
  }
  string ptime = null;

  if(zdd!=0){
   if(zmm!=0){
    if(zyear!=0){
     ptime = zyear+"年"+zmm+"个月"+"零"+zdd+"天";
    }else{
     ptime = zmm+"个月"+"零"+zdd+"天";
    }
   }else{
    if(zyear!=0){
     ptime = zyear+"年"+"零"+zdd+"天";
    }else{
     ptime = zdd+"天";
    }
   }
  }else{
   if(zmm!=0){
    if(zyear!=0){
     ptime = zyear+"年"+zmm+"个月";
    }else{
     ptime = zmm+"个月";
    }
   }else{
    if(zyear!=0){
     ptime = zyear+"年";
    }else{
     ptime = null;
    }
   }
  }
  if(type==1){
   return ptime;   //返回年月日(如:2年3个月零5天)
  }else{
   return string.valueof(countddd);  //返回总天数
  }

  
 }
 /**
  * 得到月数
  * @param year 年数差
  * @param month 月数差
  * @return
  */
 public static int getcmm(integer year,integer month){
  int zmm = 0;
  if(month < 0){
   zmm = (month + 12)+(year-1)*12;
  }else if(month == 0){
   zmm = year*12;
  }else{
   zmm = year*12+month;
  }
  return zmm;
 }

 

 /**
  * 改更现在时间
  */
 public static date changedate(string type, int value) {
  calendar calendar = calendar.getinstance(timezone.getdefault());
  if (type.equals("month")) {
   calendar.set(calendar.month, calendar.get(calendar.month) + value);
  } else if (type.equals("date")) {
   calendar.set(calendar.date, calendar.get(calendar.date) + value);
  }
  return calendar.gettime();
 }

 /**
  * 更改时间
  */
 public static date changedate(date date, string type, int value) {
  if (date != null) {
   // calendar calendar = calendar.getinstance(timezone.getdefault());
   calendar calendar = new gregoriancalendar();
   calendar.settime(date);
   // calendar calendar = calendar.
   if (type.equals("month")) {
    calendar.set(calendar.month, calendar.get(calendar.month) + value);
   } else if (type.equals("date")) {
    calendar.set(calendar.date, calendar.get(calendar.date) + value);
   } else if (type.endswith("year")) {
    calendar.set(calendar.year, calendar.get(calendar.year) + value);
   }
   return calendar.gettime();
  }
  return null;
 }

 /**
  * haoxw 比较时间是否在这两个时间点之间
  *
  * @param time1
  * @param time2
  * @return
  */
 public static boolean checktime(string time1, string time2) {
  calendar calendar = calendar.getinstance();
  date date1 = calendar.gettime();
  date date11 = dateutil.str2date(dateutil.date2str(date1, "yyyy-mm-dd") + " " + time1);// 起始时间

  calendar c = calendar.getinstance();
  c.add(calendar.date, 1);
  date date2 = c.gettime();
  date date22 = dateutil.str2date(dateutil.date2str(date2, "yyyy-mm-dd") + " " + time2);// 终止时间

  calendar scalendar = calendar.getinstance();
  scalendar.settime(date11);// 起始时间

  calendar ecalendar = calendar.getinstance();
  ecalendar.settime(date22);// 终止时间

  calendar calendarnow = calendar.getinstance();

  if (calendarnow.after(scalendar) && calendarnow.before(ecalendar)) {
   return true;
  } else {
   return false;
  }

 }

 /**
  * haoxw 比较时间是否在这两个时间点之间
  *
  * @param date11
  * @param date22
  * @return
  */
 public static boolean checktime(date date11, date date22) {

  

  calendar scalendar = calendar.getinstance();
  scalendar.settime(date11);// 起始时间

  calendar ecalendar = calendar.getinstance();
  ecalendar.settime(date22);// 终止时间

  calendar calendarnow = calendar.getinstance();

  if (calendarnow.after(scalendar) && calendarnow.before(ecalendar)) {
   return true;
  } else {
   return false;
  }

 }


 public static boolean checkdate(string date1, string date2) {

  date date11 = dateutil.str2date(date1, "yyyy-mm-dd hh:mm:ss");// 起始时间

  date date22 = dateutil.str2date(date2, "yyyy-mm-dd hh:mm:ss");// 终止时间

  calendar scalendar = calendar.getinstance();
  scalendar.settime(date11);// 起始时间

  calendar ecalendar = calendar.getinstance();
  ecalendar.settime(date22);// 终止时间

  calendar calendarnow = calendar.getinstance();

  system.out.println(date11.tostring());
  system.out.println(date22.tostring());
  system.out.println(scalendar.tostring());
  system.out.println(ecalendar.tostring());
  system.out.println(calendarnow.tostring());

  if (calendarnow.after(scalendar) && calendarnow.before(ecalendar)) {
   return true;
  } else {
   return false;
  }
 }

 /**
  * 获取interval天之前的日期
  *
  * @param interval
  * @param starttime
  * @param pattern
  * @return
  */
 public static date getintervaldate(string interval, date starttime, string pattern) {
  calendar temp = calendar.getinstance();
  temp.settime(starttime);
  temp.add(temp.date, integer.parseint(interval));
  simpledateformat sdf = new simpledateformat(pattern);
  string shijian = sdf.format(temp.gettime());
  return str2date(shijian);
 }

 public static date formatdate(date date){
  simpledateformat bartdateformat =
  new simpledateformat("yyyy-mm-dd");   
  system.out.println(bartdateformat.format(date));
  simpledateformat bartdateformat1 =new simpledateformat("yyyy-mm-dd");    
  try {
   date date1 = bartdateformat1.parse(bartdateformat.format(date));
  } catch (parseexception e) {    
   e.printstacktrace();
  }
  system.out.println(date.gettime());
  return date;

 }

 public static void main(string arf[]) {

  /*string time1 = "2009-05-07 19:20:00";
  string time2 = "2009-05-08 19:30:00";

  dateutil d = new dateutil();
  system.out.println(d.checkdate(time1, time2));
  system.out.println(d.date2str(new date()));*/

  //system.out.println(d.getintervaldate("-3", new date(), default_pattern));
  calendar calendar = calendar.getinstance(timezone.getdefault());
  system.out.println(calendar.tostring());
  system.out.println(dateutil.str2date("20090731","yyyymmdd"));

  system.out.println(dateutil.getbeforedate(new date(),2 ));
  system.out.println(dateutil.datepattern(new date()));

     simpledateformat bartdateformat =
  new simpledateformat("yyyy-mm-dd");
  date date = new date();
  system.out.println("date;"+bartdateformat.format(date));
  simpledateformat bartdateformat1 =new simpledateformat("yyyy-mm-dd");    
  try {
   date date1 = bartdateformat1.parse(bartdateformat.format(date));
   system.out.println("日期:"+date1);
  } catch (parseexception e) {    
   e.printstacktrace();
  }

 }
}