欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解JAVA常用的時間操作【實用】

 更新時間:2016年12月09日 10:24:07   作者:liliangel  
本文主要介紹了JAVA一些常用的時間操作,很實用,相信大家在開發(fā)項目時會用到,下面就跟小編一起來看下吧

項目中經常有對時間進行處理的需求,下面是一些常用的操作整理,方便以后再次使用以及做相關復習。

1.字符串轉換為日期

/**
   * 字符串轉換為日期
   * @param dateStr 需要轉換的日期
   * @param dateFormat 日期格式y(tǒng)yyy-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;
  }

以上就是本文的全部內容,希望本文的內容對大家的學習或者工作能帶來一定的幫助,同時也希望多多支持腳本之家!

相關文章

  • Java實現(xiàn)生成pdf并解決表格分割的問題

    Java實現(xiàn)生成pdf并解決表格分割的問題

    這篇文章主要為大家詳細介紹了如何利用Java實現(xiàn)生成pdf,并解決表格分割的問題,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下
    2023-11-11
  • spring中actuator監(jiān)視器配置詳解

    spring中actuator監(jiān)視器配置詳解

    這篇文章主要介紹了spring中actuator監(jiān)視器配置詳解,actuator主要是完成微服務的監(jiān)控,完成監(jiān)控治理,可以查看微服務間的數據處理和調用,當它們之間出現(xiàn)了異常,就可以快速定位到出現(xiàn)問題的地方,需要的朋友可以參考下
    2023-09-09
  • SpringBoot項目中的favicon.ico圖標無法顯示問題及解決

    SpringBoot項目中的favicon.ico圖標無法顯示問題及解決

    這篇文章主要介紹了SpringBoot項目中的favicon.ico圖標無法顯示問題及解決,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-01-01
  • java 一個截取字符串的函數

    java 一個截取字符串的函數

    java 編寫一個截取字符串的函數,輸入為一個字符串和字節(jié)數,輸出為按字節(jié)截取的字符串。 要求不能出現(xiàn)截半的情況
    2017-02-02
  • Spring Data JPA實現(xiàn)動態(tài)條件與范圍查詢實例代碼

    Spring Data JPA實現(xiàn)動態(tài)條件與范圍查詢實例代碼

    本篇文章主要介紹了Spring Data JPA實現(xiàn)動態(tài)條件與范圍查詢實例代碼,非常具有實用價值,需要的朋友可以參考下
    2017-06-06
  • IDEA創(chuàng)建方法時如何快速添加注釋

    IDEA創(chuàng)建方法時如何快速添加注釋

    這篇文章主要介紹了IDEA創(chuàng)建方法時如何快速添加注釋問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-02-02
  • 詳解SpringBoot中@SessionAttributes的使用

    詳解SpringBoot中@SessionAttributes的使用

    這篇文章主要通過示例為大家詳細介紹了SpringBoot中@SessionAttributes的使用,文中的示例代碼講解詳細,感興趣的小伙伴可以了解一下
    2022-07-07
  • Java集合之LinkedHashSet詳解

    Java集合之LinkedHashSet詳解

    這篇文章主要介紹了Java集合之LinkedHashSet詳解,LinkedHashSet是HashSet的子類,實現(xiàn)了Set接口,LinkedHashSet底層是一個LinkedHashMap,底層維護了一個數組+雙向鏈表,需要的朋友可以參考下
    2023-09-09
  • 詳解Spring Boot自動裝配的方法步驟

    詳解Spring Boot自動裝配的方法步驟

    這篇文章主要介紹了詳解Spring Boot自動裝配的方法步驟,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-06-06
  • springboot+thymeleaf國際化之LocaleResolver接口的示例

    springboot+thymeleaf國際化之LocaleResolver接口的示例

    本篇文章主要介紹了springboot+thymeleaf國際化之LocaleResolver的示例 ,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-11-11

最新評論