******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);
JAVA 獲取當前上一周 上一月 上一年的時間
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 ...
python django查詢一周,一月,一年時間
首先是當前時間的確定,對於年月日,orm模型都有對應的方法直接查詢,週是沒有方法直接查詢的,我是沒有找到這個方法,只能間接的查詢 now time datetime.datetime.now 如果資料庫儲存的是utc時間,程式不會蹦但是會提示你這不是本地時間 now time utc datetim...
獲取一月or一周的日期集合
2 listlistalldaysbymonth getalldaysweekbydate firstdate,enddate 3 private listgetalldaysweekbydate date firstdate,date enddate return lst private stat...