時間工具類

2021-10-08 17:07:13 字數 2900 閱讀 2761

1、根據日期獲取當天是週幾

/**

* 根據日期獲取當天是週幾

* @param datetime 日期

* @return 週幾

* @author lu

* @since 1.0, 2023年7月30日

*/public

static string getweekofdate

(date datetime)

; calendar calendar = calendar.

getinstance()

; calendar.

settime

(datetime)

;int i = calendar.

get(calendar.day_of_week)-1

;if(i<0)

return weekdays[i]

;}

2、獲取某段時間內的周一(二等等)的日期

/**

* 獲取某段時間內的周一(二等等)的日期

* @param databegin 開始日期

* @param dataend 結束日期

* @param weekdays 獲取週幾,1-6代表周一到週六。0代表週日

* @return 返回日期list

* @author lu

* @since 1.0, 2023年7月30日

*/public

static list

getdayofweekwithindateinterval

(string databegin, string dataend,

int weekdays)

; date[

] dates =

newdate

[dateinterval.length]

;for

(int i =

0; i < dateinterval.length; i++

)for

(date date = dates[0]

; date.

compareto

(dates[1]

)<=0;

)}return dateresult;

}

3、獲取時間段內多個星期幾的日期

/**

* 獲取某段時間內的周一(二等等)的日期

* @param databegin 開始日期 例:2020-07-01

* @param dataend 結束日 例:2020-08-12

* @param weekdays 獲取週幾,1-6代表周一到週六。0代表週日 例:"0,2,3"

* @return 返回日期list

* @author lu

* @since 1.0, 2023年7月30日

*/public

static list

getdayofweekwithindateinterval

(string databegin, string dataend, string weekdays)

; date[

] dates =

newdate

[dateinterval.length]

;for

(int i =

0; i < dateinterval.length; i++

)for

(date date = dates[0]

; date.

compareto

(dates[1]

)<=0;

)}return dateresult;

}

4、獲取指定日期所在月份開始的時間戳

/**

* 獲取指定日期所在月份開始的時間戳

* @param date 指定日期

* @return 指定日期所在月份開始的時間戳

*/public

static long getmonthbegin

(date date)

5、獲取指定日期所在月份結束的時間戳

/**

* 獲取指定日期所在月份結束的時間戳

* @param date 指定日期

* @return 指定日期所有月份結束時間戳

*/public

static long getmonthend

(date date)

6、針對時間戳返回問題,返回的帶時區時間戳

/**

* 針對時間戳返回問題,返回的帶時區時間戳

*/public

final

static

long

gettimebyzone

(date date)

7、時間戳轉換成字串

/**

* 時間戳轉換成字串

*/public

static string getdatetostring

(long time)

8、將字串轉為時間戳

/**

* 將字串轉為時間戳

*/public

static

long

getstringtodate

(string time)

catch

(parseexception e)

return date.

gettime()

;}

時間工具類

獲取當前時間 年月日時分秒 test public void nowdate 獲取任意過去或者未來的現在時間 test public void pastorfuturedate 獲取任意兩個時間之間相差多少秒 test public void anytwotimedifference throws ...

時間工具類

public class timeutils 獲取帶連線符的日期型別 return public static string getcurrentnormaldate 獲取當年的年份 return public static int getcurrentyear 獲取當年的月份 return pub...

時間工具類

隨手記錄 string轉date時間 param time 時間字串 param format 格式 return author ydl date 2020 09 17 public static date formatdate string time,string format dateforma...