1. 程式人生 > >Java時間計算相關方法

Java時間計算相關方法

Java 時間計算相關:

SimpleDateFormat format = new SimpleDateFormat(“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小時制

SimpleDateFormat format = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”); String nowDate=format.format(new Date()); System.out.println(nowDate);

6.當天的開始時間

SimpleDateFormat format = new SimpleDateFormat(“yyyy-MM-dd”); Calendar c = Calendar.getInstance(); String start = format.format(c.getTime())+" 00:00:00"; System.out.println(start);

7.當天的截止時間

SimpleDateFormat format = new SimpleDateFormat(“yyyy-MM-dd”); Calendar calendar = Calendar.getInstance(); String end = format.format(calendar.getTime())+" 23:59:59"; System.out.println(end);

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

SimpleDateFormat format = new SimpleDateFormat(“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.當天日期前一個月

SimpleDateFormat format = new SimpleDateFormat(“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.當前時間的前一年開始時間

SimpleDateFormat format = new SimpleDateFormat(“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()方法

SimpleDateFormat format = new SimpleDateFormat(“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.當前時所在月份的第一天和最後一天

SimpleDateFormat format=new SimpleDateFormat(“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.當期時間所在年份的開始時間

SimpleDateFormat format = new SimpleDateFormat(“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 SimpleDateFormat(“yyyy-MM-dd”).format(cal.getTime()); System.out.println(monday);