一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - 詳解JAVA常用的時間操作【實用】

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

2020-07-11 14:01liliangel Java教程

本文主要介紹了JAVA一些常用的時間操作,很實用,相信大家在開發項目時會用到,下面就跟小編一起來看下吧

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

1.字符串轉換為日期

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/**
   * 字符串轉換為日期
   * @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.時間戳轉日期

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/**
   * 時間戳轉日期
   * @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.日期格式化成字符串

?
1
2
3
4
5
6
7
8
9
10
/**
   * 日期格式化成字符串
   * @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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
/**
  * 獲取指定日期之前或之后的日期
  * @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.獲取指定日期之前或之后的日期 ,時分秒為當前的

?
1
2
3
4
5
6
7
8
9
10
11
12
13
/**
 * 獲取指定日期之前或之后的日期
 * @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類型的時間字符串 轉換成 時、分

?
1
2
3
4
5
6
7
8
9
/**
 * 將time類型的時間字符串 轉換成 時、分
 * HH-mm-ss -->> HH-mm
 * @param time
 * @return
 */
public static String timeToHHMM(String time){
  return time.substring(0, time.length() - 3);
}

7.獲取某個日期的時、分

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/**
 * 獲取某個日期的時、分
 * @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類型的時間字符串 -->> 零點開始的秒數

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/**
 * 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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/**
 * 零點開始的秒數轉時間 -->> 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.比較兩個日期相差的秒數

?
1
2
3
4
5
6
7
8
9
10
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);
}

11.判斷兩個時間段是否存在交集

?
1
2
3
4
5
6
7
8
9
10
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/**
 * 獲取指定日期是星期幾(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;
}

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

原文鏈接:http://www.cnblogs.com/liliangel/p/5292944.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 亚洲系列国产精品制服丝袜第 | 小小水蜜桃视频高清在线观看免费 | 成人免费影 | 闺蜜的样子小说安沁在线阅读 | 亚洲国产第一区二区三区 | 国产欧美在线播放 | 国产精品拍拍拍福利在线观看 | 歪歪动漫小说sss | 无码人妻精品一区二区蜜桃在线看 | 国产探花在线视频 | 美女脱了内裤让男桶爽 | 亚洲第成色999久久网站 | 国产美女做爰免费视频软件 | 免费看视频的 | 日本高h | a级精品九九九大片免费看 a级动漫 | 99热国产这里只有精品 | 华人亚洲欧美精品国产 | 校花在公车上被内射好舒服 | 久久WWW免费人成一看片 | 男人亚洲天堂 | h肉动漫在线视频无修无遮挡 | 五月天婷婷网亚洲综合在线 | 日韩免费在线视频 | 国产精品吹潮香蕉在线观看 | 俄罗斯女同和女同xx | 国产综合色在线视频区色吧图片 | 欧美高清videosex极品 | 特黄特色大片免费高清视频 | 色综合色狠狠天天久久婷婷基地 | 视频高清在线观看 | 成年人视频在线免费观看 | 国产色视频一区二区三区 | 天天做天天爱天天操 | 亚洲美女aⅴ久久久91 | 日本国产一区二区三区 | 国产成人在线播放视频 | 国产高清经典露脸3p | 天天狠天天透天干天天怕处 | 国产日韩成人 | 香蕉视频久久 |