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

Android处理时间各种方法汇总

程序员文章站 2023-01-03 11:04:30
本文实例为大家分享了android处理时间的各种方法,供大家参考,具体内容如下 /** * 获取当前时间 * * @return 当前时...

本文实例为大家分享了android处理时间的各种方法,供大家参考,具体内容如下

/**
   * 获取当前时间
   * 
   * @return 当前时间
   */
  public static string getdate() {
    simpledateformat formatter = new simpledateformat("hh:mm");
    date curdate = new date(system.currenttimemillis());// 获取当前时间
    string time = formatter.format(curdate);
    return time;
  }
  /**
   * 获取当前秒数
   * @return
   */
  public static string getsecond(){
    calendar calendar = calendar.getinstance();
    int sec = calendar.get(calendar.second);
    string value = string.valueof(sec<10?"0"+sec:sec);
    return value;
  }

  /**
   * 获取当天日期
   * 
   * @return 当前时间
   */
  public static string getriqi() {
    simpledateformat formatter = new simpledateformat("yyyy-mm-dd");
    date curdate = new date(system.currenttimemillis());// 获取当天日期
    string time = formatter.format(curdate);

    return time;
  }
    /**
   * 获取当前全部时间
   * 
   * @return 当前时间
   */
  public static string getalldate() {
    simpledateformat formatter = new simpledateformat("yyyy-mm-dd hh:mm:ss");
    date curdate = new date(system.currenttimemillis());// 获取当前时间
    string time = formatter.format(curdate);
    return time;
  }

  public static date getdate(string time){
    simpledateformat formatter = new simpledateformat("yyyy-mm-dd hh:mm:ss");
    date date = null;
    try {
      date = formatter.parse(time);
    } catch (parseexception e) {
      e.printstacktrace();
    }
    return date;
  }
    /**
   * 天数减一
   */
  public static string yesterday(int data) {
    simpledateformat dft = new simpledateformat("yyyy-mm-dd");
    date begindate = new date();
    calendar date = calendar.getinstance();
    date.settime(begindate);
    date.set(calendar.date, date.get(calendar.date) - data);
    // date enddate = dft.parse(dft.format(date.gettime()));
    string time = dft.format(date.gettime());
    return time;
  }
    /**
   * 获取星期
   * 
   * @param date日期
   *      ****-**-**
   * @return
   */
  public static int getday(date date) {
    calendar cal = calendar.getinstance();
    cal.settime(date);
    return cal.get(calendar.day_of_week);
  }
    /**
   * 时间顺序判断
   * @param data_ruke
   * @param data_choice
   * @return
   */
  public static boolean conpare_data(string data1,string data2){
    if(data1==null||"".equals(data1)){
      return true;
    }
    dateformat dformat = new simpledateformat("yyyy-mm-dd hh:mm:ss");
    try{
      date dt1 = dformat.parse(data1);
      date dt2 = dformat.parse(data2);
      if(dt1.gettime()>dt2.gettime()){
         log.i("time_compare", "dt1在dt2之后");
         return false;
      }else if (dt1.gettime() < dt2.gettime()) {
         log.i("time_compare", "dt1在dt2之前");
         return true;
      }
    }catch(exception e){
       e.printstacktrace();
    }
    return false;
  }
    /**
   * 日期比较
   */
  public static int compare_date(string date1, string date2) {

    dateformat df = new simpledateformat("yyyy-mm-dd");
    try {
      date dt1 = df.parse(date1);
      date dt2 = df.parse(date2);
      if (dt1.gettime() > dt2.gettime()) {
        if (dt1.gettime() - dt2.gettime() == 86400000) {
          return 1;
        } else if (dt1.gettime() - dt2.gettime() == (86400000 * 2)) {
          return 2;
        } else if (dt1.gettime() - dt2.gettime() == (86400000 * 3)) {
          return 3;
        } else {
          return 0;
        }
      } else {
        return 0;
      }
    } catch (exception exception) {
      exception.printstacktrace();
      return 0;
    }

  }
  /**
   * 返回两个string类型日期之间相差的天数
   */
   public static int daysbetween(string smdate,string bdate){ 
     simpledateformat sdf=new simpledateformat("yyyy-mm-dd"); 
     calendar cal = calendar.getinstance();  
     long time1 = 0;
     long time2 = 0;

     try{
       cal.settime(sdf.parse(smdate));  
       time1 = cal.gettimeinmillis();  
       cal.settime(sdf.parse(bdate)); 
       time2 = cal.gettimeinmillis(); 
     }catch(exception e){
       e.printstacktrace();
     }
     long between_days=(time2-time1)/(1000*3600*24); 

    return integer.parseint(string.valueof(between_days));   
   }
   /**
   * 返回两个string类型日期相差的小时数
   */
   public static int daysbetween2(string starttime, string endtime) {
     simpledateformat sdf=new simpledateformat("yyyy-mm-dd hh"); 
     calendar cal = calendar.getinstance();  
     long time1 = 0;
     long time2 = 0;

     try{
        cal.settime(sdf.parse(starttime));  
        time1 = cal.gettimeinmillis();  
        cal.settime(sdf.parse(endtime)); 
        time2 = cal.gettimeinmillis(); 
     }catch(exception e){
       e.printstacktrace();
     }
     long between_days=(time2-time1)/(1000*3600); 

     return integer.parseint(string.valueof(between_days));   
   }
   /**
   * 计算两段日期的重合日期
   */
   /**
   * 计算两段日期的重合日期
   * @param str1 开始日期1
   * @param str2 结束日期1
   * @param str3 开始日期2
   * @param str4 结束日期2
   * @return
   * @throws exception
   */
  public static map<string,object> comparisonrq(string str1, string str2, string str3,
      string str4) throws exception {
    string mesg = "";
    dateformat df = new simpledateformat("yyyy-mm-dd");
    string startdate = "";
    string enddate = "";
    try {
      date dt1 = df.parse(str1);
      date dt2 = df.parse(str2);
      date dt3 = df.parse(str3);
      date dt4 = df.parse(str4);
      if (dt1.gettime()<=dt3.gettime()&&dt3.gettime()<=dt2.gettime()&&dt2.gettime()<=dt4.gettime()) {
        mesg = "f";//重合
        startdate = str3;
        enddate = str2;
      }
      if (dt1.gettime()>=dt3.gettime()&&dt3.gettime()<=dt2.gettime()&&dt2.gettime()<=dt4.gettime()) {
        mesg = "f";//重合
        startdate = str1;
        enddate = str2;
      }

      if (dt3.gettime()<=dt1.gettime()&&dt1.gettime()<=dt4.gettime()&&dt4.gettime()<=dt2.gettime()) {
        mesg = "f";//重合
        startdate = str1;
        enddate = str4;
      }
      if (dt3.gettime()>=dt1.gettime()&&dt1.gettime()<=dt4.gettime()&&dt4.gettime()<=dt2.gettime()) {
        mesg = "f";//重合
        startdate = str3;
        enddate = str4;
      }

      system.out.println(startdate+"----"+enddate);


    }catch (parseexception e) {
      e.printstacktrace();
      throw new parseexception(e.getmessage(), 0);
    }catch(exception e){
      e.printstacktrace();
      throw new exception(e);
    }
    map<string,object> map = new hashmap<string,object>();
    map.put("startdate", startdate);
    map.put("enddate", enddate);
    return map;
 }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。