Java實現獲取過去一周,乙個月,一年等日期

2021-08-26 23:42:58 字數 4742 閱讀 6058

******dateformat format = new ******dateformat("yyyy-mm-dd hh:mm:ss");

calendar c = calendar.getinstance();

1.過去七天

c.settime(new date());

c.add(calendar.date, - 7);

date d = c.gettime();

string day = format.format(d);

system.out.println("過去七天:"+day);

2.過去一月

c.settime(new date());

c.add(calendar.month, -1);

date m = c.gettime();

string mon = format.format(m);

system.out.println("過去乙個月:"+mon);

3.過去三個月

c.settime(new date());

c.add(calendar.month, -3);

date m3 = c.gettime();

string mon3 = format.format(m3);

system.out.println("過去三個月:"+mon3);

4.過去一年

c.settime(new date());

c.add(calendar.year, -1);

date y = c.gettime();

string year = format.format(y);

system.out.println("過去一年:"+year);

5.現在時間24小時制  

******dateformat format = new ******dateformat("yyyy-mm-dd hh:mm:ss");  

string nowdate=format.format(new date());  

system.out.println(nowdate);  

6.當天的開始時間  

******dateformat format = new ******dateformat("yyyy-mm-dd");  

calendar c = calendar.getinstance();  

string start = format.format(c.gettime())+" 00:00:00";  

system.out.println(start);  

7.當天的截止時間  

******dateformat format = new ******dateformat("yyyy-mm-dd");  

calendar calendar = calendar.getinstance();  

string end = format.format(calendar.gettime())+" 23:59:59";  

system.out.println(end);  

8.當前時間的前乙個星期 開始時間  

******dateformat format = new ******dateformat("yyyy-mm-dd");  

calendar c = calendar.getinstance();  

c.add(calendar.day_of_month, -6);  

string start = format.format(c.gettime())+" 00:00:00";  

system.out.println(start);  

9.當天日期前乙個月  

******dateformat format = new ******dateformat("yyyy-mm-dd");  

calendar c = calendar.getinstance();  

c.add(calendar.month, -1);    //得到前乙個月    

string start = format.format(c.gettime())+" 00:00:00";  

system.out.println(start);  

10.當前時間的前一年開始時間  

******dateformat format = new ******dateformat("yyyy-mm-dd");  

calendar c = calendar.getinstance();  

c.add(calendar.year, -1); //年份減1   

string start =format.format(c.gettime())+" 00:00:00";  

system.out.println(start);  

11.當前時間的周一時間和週末時間 setfirstdayofweek()方法  

******dateformat format  = new ******dateformat("yyyy-mm-dd ");  

calendar c = calendar.getinstance();  

c.set(calendar.day_of_week,calendar.monday);  

string weekstart = format.format(c.gettime())+" 00:00:00";  

system.out.println(weekstart);  

calendar ca = calendar.getinstance();  

ca.setfirstdayofweek(calendar.monday);  

ca.set(calendar.day_of_week, ca.getfirstdayofweek() + 6); // sunday  

string weekend = format.format(ca.gettime())+" 23:59:59";  

system.out.println(weekend);  

12.當前時所在月份的第一天和最後一天  

******dateformat format=new ******dateformat("yyyy-mm-dd");  

calendar c = calendar.getinstance();      

c.set(calendar.day_of_month,1);//設定為1號,當前日期既為本月第一天   

string monthstart = format.format(c.gettime())+" 00:00:00";  

system.out.println(monthstart);  

calendar ca = calendar.getinstance();      

ca.set(calendar.day_of_month, ca.getactualmaximum(calendar.day_of_month));    

string monthend = format.format(ca.gettime())+" 23:59:59";  

system.out.println(monthend);  

13.當期時間所在年份的開始時間  

******dateformat format = new ******dateformat("yyyy-mm-dd");  

calendar c = calendar.getinstance();  

c.set(c.get(calendar.year) ,0, 1);//開始時間日期  

string yearstart = format.format(c.gettime())+" 00:00:00";  

system.out.println(yearstart);  

calendar ca = calendar.getinstance();  

ca.set(ca.get(calendar.year) ,11, ca.getactualmaximum(calendar.day_of_month));//結束日期  

string yearend = format.format(ca.gettime())+" 23:59:59";  

system.out.println(yearend);  

14.計算時間差取得兩個時間的微秒級的時間差

//得微秒級時間差    

long val = calendarend.gettimeinmillis() - calendarbegin.gettimeinmillis();    

//換算後得到天數    

long day = val / (1000 * 60 * 60 * 24);

15.獲取某個時間的上週一和週日

calendar cal = calendar.getinstance();

//n為推遲的週數,1本週,-1向前推遲一周,2下週,依次類推

int n = 1;

string monday;

cal.add(calendar.date, n*7);

//想周幾,這裡就傳幾calendar.monday(tuesday...)

cal.set(calendar.day_of_week,calendar.monday);

monday = new ******dateformat("yyyy-mm-dd").format(cal.gettime());

system.out.println(monday);

乙個月又過去了

今天週日,還是去加了一天班。好像已經有好幾年沒有在週日加班了。雖然今天去沒做什麼事,但是,終究算是加班,還是會覺得有點累。明天就要出差了。呼,希望一切順利。晚上af說,乙個月過去了,目標找得怎麼樣啦?無言以對。怎麼辦。出差回來再說吧。沒辦法了。看著行銷時不時 叫人來加班,來解決問題。想法開始混亂,難...

MYSQL最近一周乙個月時間段的查詢語句

select from wap content where week created at week now 如果你要嚴格要求是某一年的,那可以這樣 查詢一天 select from table where to days column time to days now select from ta...

strtotime 獲取上乙個月

1.案例 strtotime結合 1 month,1 month,next month 有時候會出現錯誤 date y m d strtotime 1 month 輸出 2018 07 01 1 month 程式設定 30天,相當於7月31日 30天得7月30號 var dump date y m ...