1、获取系统当前日期
Date now = new Date(); Calendar calendar = Calendar.getInstance(); calendar.setTime(now); calendar.add(calendar.MONTH, -0); String day=new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime()); //设置时间格式 System.out.println(day);
2、获取当月第一天
Date now = new Date(); Calendar calendar = Calendar.getInstance(); calendar.setTime(now); calendar.add(calendar.MONTH, -0); // 设置日历中月份的最小天数 calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH)); String firstDay = new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());//设置时间格式 System.out.println(firstDay);
3、获取上个月第一天
Date now = new Date(); Calendar calendar = Calendar.getInstance(); calendar.setTime(now); calendar.add(calendar.MONTH, -1);//月份往前推1个月 // 设置日历中月份的最小天数 calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH)); String time = new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());//设置时间格式 System.out.println(time);
4、获取半年前的今天的日期
Date now = new Date(); Calendar calendar = Calendar.getInstance(); calendar.setTime(now); calendar.add(calendar.MONTH, -6);//月份往前推6个月(需要几个月前的当前天往前推几个月) String time = new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());//设置时间格式 System.out.println(time);
5、获取本月最后一天日期
Date dNow = new Date(); Calendar calendar = Calendar.getInstance(); calendar.setTime(dNow); calendar.add(calendar.MONTH, 0); // 获取某月最大天数 int lastDay=0; //2月的平年瑞年天数 if(Calendar.MONTH==1) { lastDay = calendar.getLeastMaximum(Calendar.DAY_OF_MONTH); }else { lastDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH); } // 设置日历中月份的最大天数 calendar.set(Calendar.DAY_OF_MONTH, lastDay); //设置时间格式 String time = new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime()); System.out.println(time);
6、获取上月最后一天日期
Date dNow = new Date(); Calendar calendar = Calendar.getInstance(); calendar.setTime(dNow); calendar.add(calendar.MONTH, -1); // 获取某月最大天数 int lastDay=0; //2月的平年瑞年天数 if(Calendar.MONTH==1) { lastDay = calendar.getLeastMaximum(Calendar.DAY_OF_MONTH); }else { lastDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH); } // 设置日历中月份的最大天数 calendar.set(Calendar.DAY_OF_MONTH, lastDay); //设置时间格式 String time = new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime()); System.out.println(time);
7、获取今年第一天
Calendar calendar = Calendar.getInstance(); Calendar date = Calendar.getInstance(); calendar.clear(); calendar.set(Calendar.YEAR, Integer.parseInt(String.valueOf(date.get(Calendar.YEAR)))); String time = new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime()); System.out.println(time);
8、获取去年第一天
Calendar calendar = Calendar.getInstance(); Calendar date = Calendar.getInstance(); calendar.clear(); calendar.set(Calendar.YEAR, Integer.parseInt(String.valueOf(date.get(Calendar.YEAR)))-1); String time = new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime()); System.out.println(time);
9、获取今年最后一天
Calendar calendar = Calendar.getInstance(); Calendar date = Calendar.getInstance(); calendar.clear(); calendar.set(Calendar.YEAR, Integer.parseInt(String.valueOf(date.get(Calendar.YEAR)))); calendar.roll(Calendar.DAY_OF_YEAR, -1); String time = new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime()); System.out.println(time);
10、获取去年最后一天
Calendar calendar = Calendar.getInstance(); Calendar date = Calendar.getInstance(); calendar.clear(); calendar.set(Calendar.YEAR, Integer.parseInt(String.valueOf(date.get(Calendar.YEAR)))-1); calendar.roll(Calendar.DAY_OF_YEAR, -1); String time = new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime()); System.out.println(time);
11、获取7天前的日期
Calendar calendar = new GregorianCalendar(); calendar.setTime(new Date()); calendar.add(calendar.DATE, -7); String time = new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime()); System.out.println(time);
12、获取int类型的年份
//上一年 int years = Integer.parseInt(String.valueOf(Calendar.getInstance().get(Calendar.YEAR)))-1; System.out.println(years); //当前年 int year= Integer.parseInt(String.valueOf(Calendar.getInstance().get(Calendar.YEAR))); System.out.println(year); //下一年 int yearss = Integer.parseInt(String.valueOf(Calendar.getInstance().get(Calendar.YEAR)))+1; System.out.println(yearss);
13、获取int类型的天
//后一天 int days = Integer.parseInt(String.valueOf(Calendar.getInstance().get(Calendar.DAY_OF_MONTH)+1)); System.out.println(days); //当前天 int day = Integer.parseInt(String.valueOf(Calendar.getInstance().get(Calendar.DAY_OF_MONTH))); System.out.println(day); //前一天 int dayss = Integer.parseInt(String.valueOf(Calendar.getInstance().get(Calendar.DAY_OF_MONTH)-1)); System.out.println(dayss);
14、获取int类型的月份
//当前月 int month = Integer.parseInt(String.valueOf(Calendar.getInstance().get(Calendar.MONTH)+1)); System.out.println(month); //上个月 int months = Integer.parseInt(String.valueOf(Calendar.getInstance().get(Calendar.MONTH))); System.out.println(months); //下个月 int monthss = Integer.parseInt(String.valueOf(Calendar.getInstance().get(Calendar.MONTH)+2)); System.out.println(monthss);