我的Java日期處理類 簡單

2021-09-01 22:39:51 字數 4820 閱讀 7509

昨天發了篇博文,不想有朋友對標題有些誤解了,所以我提前說明下,本篇博文只是記錄下我的時間處理類,功能非常簡單,主要方法有:

1,格式化日期與顯示日期,包括中文方法友好的顯示,如1年前。

2,給日期加上幾天或者幾個月。

3,得到今天的開始時間與結束時間,如2014-03-05 0:00:00~2014-03-05 23:59:59。

4,得到今天對應的月份的開始時間與結束時間。

5,得到今天對應年的開始時間與結束時間。

6,得到今天星期幾,是否是週六週末

7,得到今天對應月份最大天數。

8,得到2個日期是否是同一天,相差多少年,多少月,多少天。

9,得到2個月份之間的月份,返回值list《字串》。

public class 我的時間工具類 ;

private static string weekdays = ;

public static string formatdate(date date, string pattern)

return returnvalue;

} public static date parsedate(string strdate, string pattern) catch (parseexception e)

} // 得到星期幾

public static string getweekofdate(date date)

// 判斷日期是否是週六週末

public boolean isweekend(date date)

// date所處周的星期一

public static date getfirstdayofweek(date date)

// date所處周的星期天

public static date getlastdayofweek(date date)

// 得到日期月份最大的天數

public static int getmaxdayofmonth(int year, int month)

// 判斷2個日期是否在同一天

public static boolean issameday(date date, date date2)

// yyyy-mm-dd 0:00:00

//days=0 今天,-1昨天,1明天下面的都一樣

public static date getdatestart(date date, int days)

// yyyy-mm-dd 23:59:59

public static date getdateend(date date, int days)

// yyyy-mm-1 00:00:00

public static date getmonthstart(date date, int n)

// yyyy-mm-end 23:59:59

public static date getmonthend(date date, int n)

// yyyy-1-1 00:00:00

public static date getyearstart(date date, int n)

// yyyy-12-31 23:59:59

public static date getyearend(date date, int n)

// 日期加上n個月

public static date addmonths(date date, int n)

// 日期加上n天

public static date adddays(date date, int n)

// 2個日期相差多少天

public static long getdaydiff(date startdate, date enddate)

throws parseexception

// 2個日期相差多少天 不考慮時分秒

public static long getdaydiffignorehhmiss(date startdate, date enddate)

throws parseexception

// 2個日期相差多少年

public static int getyeardiff(date mindate, date maxdate)

calendar calendar = calendar.getinstance();

calendar.settime(mindate);

int year1 = calendar.get(calendar.year);

int month1 = calendar.get(calendar.month);

int day1 = calendar.get(calendar.date);

calendar.settime(maxdate);

int year2 = calendar.get(calendar.year);

int month2 = calendar.get(calendar.month);

int day2 = calendar.get(calendar.date);

int result = year2 - year1;

if (month2 < month1) else if (month2 == month1 && day2 < day1)

return result;

} // 2個日期相差多少月

public static int getmonthdiff(date mindate, date maxdate)

calendar calendar = calendar.getinstance();

calendar.settime(mindate);

int year1 = calendar.get(calendar.year);

int month1 = calendar.get(calendar.month);

int day1 = calendar.get(calendar.date);

calendar.settime(maxdate);

int year2 = calendar.get(calendar.year);

int month2 = calendar.get(calendar.month);

int day2 = calendar.get(calendar.date);

int months = 0;

if (day2 >= day1) else

return (year2 - year1) * 12 + months;

} // 得到2個日期之間的月份,返回值list《字串》

public static listgetmonthsbetween(date mindate, date maxdate)

calendar min = calendar.getinstance();

calendar max = calendar.getinstance();

min.settime(mindate);

min.set(min.get(calendar.year), min.get(calendar.month), 1);

max.settime(maxdate);

max.set(max.get(calendar.year), max.get(calendar.month), 2);

calendar curr = min;

while (curr.before(max))

return result;

} /**

* 最近一周時間段 今天對應的上週星期n-今天

*/public static date getrecentweek(date date) ;

} // 最近乙個月 今天對應的上月n日-今天

public static date getrecentmonth(date date) ;

} // 中文顯示日期與當前時間差

public static string friendlyformat(date date) throws parseexception

date basedate = new date();

if (basedate.before(date))

int year = getyeardiff(basedate, date);

int month = getmonthdiff(basedate, date);

if (year >= 1) else if (month >= 1)

int day = (int) getdaydiff(basedate, date);

if (day > 0) else if (day == 2) else if (day == 1)

} if (!issameday(basedate, date))

int hour = (int) ((basedate.gettime() - date.gettime()) / (1 * 60 * 60 * 1000));

if (hour > 6) else if (hour > 0)

int minute = (int) ((basedate.gettime() - date.gettime()) / (1 * 60 * 1000));

if (minute < 2) else if (minute < 30) else if (minute > 30)

return "未知";

}}

最後乙個方法沒有考慮跨年日期相差3天的情況,如有需要請自行處理。

如有錯誤,歡迎指出,謝謝。

全文完。

Java日期處理

獲取當前系統時間 date d new date system.out.println d.tostring 輸出結果為美式時間格式,用以下辦法轉換成中式時間格式 dateformat sdf new dateformat yyyy mm dd hh mm ss string time sdf.fo...

java 日期處理

日期比較 param date01 param date02 return 如果date01 date02 返回true,否則返回false throws parseexception private boolean datecompare string date01,string date02 c...

java 日期處理

一。date 比較 date a date b 1.a.after b 返回boolean a在b後返回true a.before b 返回boolean b在a前返回true a.equals b a,b相等返回true 2.calendar c1 calendar.getinstance c1....