当前位置: 移动技术网 > IT编程>开发语言>Java > 详解JAVA常用的时间操作【实用】

详解JAVA常用的时间操作【实用】

2019年07月22日  | 移动技术网IT编程  | 我要评论
项目中经常有对时间进行处理的需求,下面是一些常用的操作整理,方便以后再次使用以及做相关复习。 1.字符串转换为日期 /** * 字符串转换为日期

项目中经常有对时间进行处理的需求,下面是一些常用的操作整理,方便以后再次使用以及做相关复习。

1.字符串转换为日期

/**
   * 字符串转换为日期
   * @param datestr 需要转换的日期
   * @param dateformat 日期格式yyyy-mm-dd/yyyy-mm-dd hh:mm:ss
   */
  public static date todate(string datestr, simpledateformat dateformat) throws parseexception{
    date date = null;
    try {
      date = dateformat.parse(datestr);
    } catch (parseexception e) {
      logger.debug("fail to convert string to date, {}", datestr);
    }
    return date;
  }

2.时间戳转日期

/**
   * 时间戳转日期
   * @param date
   * @return
   */
  public static string datetotime(long time, simpledateformat dateformat) throws parseexception{
     string data = null;
     try {
       dateformat.format(new date(time*1000));  
     } catch (exception e) {
       logger.debug("fail to convert long to date, {}", time);
     }
     return data;
  }

3.日期格式化成字符串

/**
   * 日期格式化成字符串
   * @param date
   * @param dateformat
   * @return
   * @throws parseexception
   */
  public static string tostring(date date, simpledateformat dateformat) throws parseexception{
    return dateformat.format(date);
  }

4.获取指定日期之前或之后的日期 ,十分秒为00:00:00

 /**
   * 获取指定日期之前或之后的日期
   * @param date 
   * @param num 正数为之后,负数为之前
   * @return yyyy-mm-dd 00:00:00
   */
  public static date getspecificdate(date date, int num){
    calendar todaycal = calendar.getinstance();
    todaycal.settime(date);
    calendar c = calendar.getinstance();
    c.set(todaycal.get(calendar.year), todaycal.get(calendar.month), todaycal.get(calendar.day_of_month) + num, 0, 0, 0);
    return c.gettime();
  }

5.获取指定日期之前或之后的日期 ,时分秒为当前的

  /**
   * 获取指定日期之前或之后的日期
   * @param date 
   * @param num 正数为之前,负数为之后
   * @return yyyy-mm-dd + 当前的时分秒
   */
  public static date getspecificdateandhhmmss(date date,int num){
    calendar c = calendar.getinstance(); 
    c.settime(date); 
    int day=c.get(calendar.date); 
    c.set(calendar.date,day - num); 
    return c.gettime(); 
  }

6.将time类型的时间字符串 转换成 时、分

  /**
   * 将time类型的时间字符串 转换成 时、分
   * hh-mm-ss -->> hh-mm
   * @param time 
   * @return
   */
  public static string timetohhmm(string time){
    return time.substring(0, time.length() - 3);
  }

7.获取某个日期的时、分

  /**
   * 获取某个日期的时、分
   * @param date
   * @return hh-mm
   */
  public static string gethm(date date){
    calendar ca = calendar.getinstance(); 
    ca.settime(date);
    integer hour = ca.get(calendar.hour_of_day);//小时 
    integer minute = ca.get(calendar.minute);//分
    string rs_hour = hour.tostring();
    string rs_minute = minute.tostring();
    if (rs_hour.length() == 1){
      rs_hour = "0" + hour;
    }
    if(rs_minute.length() == 1){
      rs_minute = "0" + minute;
    }
    return rs_hour + ":" + rs_minute;
  }

8.time类型的时间字符串 -->> 零点开始的秒数

  /**
   * time类型的时间字符串 -->> 零点开始的秒数
   * @param time hh-mm / hh-mm-ss
   * @return
   */
  public static integer timetoseconds(string time){
     string[] timesplit = null;
     int hours = 0,minutes = 0,seconds = 0;
     try {
       timesplit = time.split(":");
       if (timesplit.length == 2) {
         hours = integer.valueof(timesplit[0])*60*60;
         minutes = integer.valueof(timesplit[1])*60;
       }else if(timesplit.length == 3){
         hours = integer.valueof(timesplit[0])*60*60;
         minutes = integer.valueof(timesplit[1])*60;
         seconds = integer.valueof(timesplit[2]);
       }else{
         logger.debug("fail to convert the time, {}", time);
       }
     } catch (exception e) {
       logger.debug("fail to convert the time, {}", time);
       throw e;
     }
    return hours + minutes + seconds;
  }

9.零点开始的秒数转时间 -->> hh-mm-ss

  /**
   * 零点开始的秒数转时间 -->> hh-mm-ss
   * @param durationseconds
   * @return
   */
  public static string getduration(int durationseconds){ 
    int hours = durationseconds /(60*60); 
    int leftseconds = durationseconds % (60*60); 
    int minutes = leftseconds / 60; 
    int seconds = leftseconds % 60; 
    stringbuffer sbuffer = new stringbuffer(); 
    sbuffer.append(addzeroprefix(hours)); 
    sbuffer.append(":"); 
    sbuffer.append(addzeroprefix(minutes)); 
    sbuffer.append(":"); 
    sbuffer.append(addzeroprefix(seconds)); 
    return sbuffer.tostring(); 
  } 
  public static string addzeroprefix(int number){ 
    if(number < 10) 
      return "0"+number; 
    else 
      return ""+number; 
  }

10.比较两个日期相差的秒数

  /**
   * 比较两个日期相差的秒数
   * @param startdate
   * @param enddate
   * @return
   */
  public static int gettimeseconds(date startdate,date enddate) {
     long a = enddate.gettime();
     long b = startdate.gettime();
     return (int)((a - b) / 1000);
  }

11.判断两个时间段是否存在交集

  /**
   * 比较两个日期相差的秒数
   * @param startdate
   * @param enddate
   * @return
   */
  public static int gettimeseconds(date startdate,date enddate) {
     long a = enddate.gettime();
     long b = startdate.gettime();
     return (int)((a - b) / 1000);
  }

12.获取指定日期是星期几(1-7分别代表周一至周日)

  /**
   * 获取指定日期是星期几(1-7分别代表周一至周日)
   * @return
   */
  public static int dayofweek(date date){
    calendar now = calendar.getinstance();
    now.settime(date);
    boolean isfirstday = (now.getfirstdayofweek() == calendar.sunday);
    int weekday = now.get(calendar.day_of_week);
    if(isfirstday){
      weekday = weekday - 1;
      if(weekday == 0){
        weekday = 7;
      }
    }
    return weekday;
  }

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持移动技术网!

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网