/** * 计算指定日期为当年第几周 * @param year 指定的年份 * @param month 指定的月份 * @param day 指定的日 * @return 指定日期为当年的第几周 */ public static int caculateWeekOfYear(int year,int month,int day){ Calendar c = Calendar.getInstance(); c.set(Calendar.YEAR, year); c.set(Calendar.MONTH, month - 1); c.set(Calendar.DATE, day); return c.get(Calendar.WEEK_OF_YEAR); } /** * 获取本月第一天是星期几 * @return */ public static int getWeekOfFirstDay(Calendar c){ Calendar calendar = c; calendar.set(Calendar.DAY_OF_MONTH, 1); return calendar.get(Calendar.DAY_OF_WEEK); } /** * 获取本月1号是该年的第几周 * @return */ public static int getMonthStartWeek(Calendar c){ Calendar calendar = c; calendar.set(Calendar.DATE, 1); return calendar.get(Calendar.WEEK_OF_YEAR); } /** * 获取当天是该年的第几周 */ public static int getCurrentWeekOfYear(){ Calendar calendar = Calendar.getInstance(); return calendar.get(Calendar.WEEK_OF_YEAR); } /** * 获取上月的总天数 * @return */ public static int getLastMonthDays(Calendar c){ Calendar calendar = c; calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - 1); return calendar.getActualMaximum(Calendar.DAY_OF_MONTH); } /** * 获取指定月份的总天数 * @return */ public static int getCurrentMonthDays(int month){ Calendar c = Calendar.getInstance(); c.set(Calendar.MONTH, month); return c.getActualMaximum(Calendar.DAY_OF_MONTH); } /** * 获取指定年份有多少周 * @param year * @return */ public static int getTotalWeekOfYear(int year){ Calendar c = Calendar.getInstance(); return c.getActualMaximum(Calendar.WEEK_OF_YEAR); } /** * 判断指定月份是否是当前月 * @param month * @return */ public static boolean isCurrentMonth(int month){ Calendar c = Calendar.getInstance(); return (c.get(Calendar.MONTH) == month)?true:false; } /** * 计算指定的月份共有多少天 */ public static int getTotalDaysOfMonth(int year, int month){ Calendar c = Calendar.getInstance(); c.set(Calendar.YEAR, year); c.set(Calendar.MONTH, month); c.set(Calendar.DATE, 1); /** * 计算这个月有多少天 */ return c.getActualMaximum(Calendar.DAY_OF_MONTH); }
1.以上是各种方法
2.java 日期的加减
- public class test1 {
- public static void main(String[] args) {
- Calendar c = Calendar.getInstance();
- int year=c.get(Calendar.YEAR);
- int month=c.get(Calendar.MONTH)+1;
- int date=c.get(Calendar.DATE);
- System.out.println("今天是"+year+"年"+month+"月"+date+"日");
- System.out.println("是今年的第"+c.get(Calendar.DAY_OF_YEAR)+"天");
- System.out.println("c.getTime()的結果: "+c.getTime());
- System.out.println("new Date()的結果: "+new Date());
- c.set(Calendar.DAY_OF_YEAR, date + 30);
- System.out.println("17天后是"+c.getTime());
- }
- }
- public static Date getDateBefore(Date d, int day) {
- Calendar now = Calendar.getInstance();
- now.setTime(d);
- now.set(Calendar.DATE, now.get(Calendar.DATE) - day);
- return now.getTime();
- }
- public static Date getDateAfter(Date d, int day) {
- Calendar now = Calendar.getInstance();
- now.setTime(d);
- now.set(Calendar.DATE, now.get(Calendar.DATE) + day);
- return now.getTime();
- }
注意int month=c.get(Calendar.MONTH)+1哦,好像系统是从0开始计月份,到了12月就归零了。所以单独取月份时,要在后面加一才能得到当前的月份。
calender日期加减后赋值给Date类型
Java代码
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
- String time=sdf.format(new Date());
- Calendar cd = Calendar.getInstance();
- try {
- cd.setTime(sdf.parse(time));
- } catch (ParseException e) {
- e.printStackTrace();
- }
- cd.add(Calendar.DATE, 1);//增加一天
- //cal.add(Calendar.DATE, -1); //减一天
- //cd.add(Calendar.MONTH, 1);//增加一月
- Date date=cd.getTime();
- System.out.println(sdf.format(date));