1. 方法1: 如果是 java8, 可以使用 LocalDate 获取
// 获取当前日期前一天的日期LocalDate localDate = LocalDate.now().minusDays(1);//1代表提前多少天// 获取时间字符串如: 2023-03-01System.out.println(localDate.toString());// 获取当前年如 2023System.out.println(localDate.getYear());// 获取当前月如 3System.out.println(localDate.getMonthValue());// 获取当前天如 1System.out.println(localDate.getDayOfMonth());//获取前一天的日期String date = localDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));//转为String类型
LocalDate转化为指定格式的字符串
方法1
LocalDate localDate = LocalDate.parse("2022-02-02");String date = localDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));System.out.println(date); //2022-02-02
方法2
LocalDate localDate = LocalDate.parse("2022-02-02");DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");String date = dtf.format(localDate);System.out.println(date); //2022-02-02
2. 方法2: 使用 Calendar 类
//获取当前日期Date date = new Date();//将时间格式化成yyyy-MM-dd HH:mm:ss的格式SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//创建Calendar实例Calendar cal = Calendar.getInstance();//设置当前时间cal.setTime(date);//在当前时间基础上减一年cal.add(Calendar.YEAR, -1); System.out.println(format.format(cal.getTime()));//在当前时间基础上减一月cal.add(Calendar.MONTH,-1);System.out.println(format.format(cal.getTime()));//同理增加一天的方法:cal.add(Calendar.DATE, 1);System.out.println(format.format(cal.getTime()));
3. 获取指定时间
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");//定义日期显示格式String now = sdf.format(new Date());//当前日期String nowMonthLateDay = getNextDays(now, "-1");//当前日期的前一天Calendar datees = Calendar.getInstance();String years = String.valueOf(datees.get(Calendar.YEAR)) + "-11-15";//获取当前年的11-15
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");//定义日期显示格式Calendar c = Calendar.getInstance();c.add(Calendar.MONTH, -1);//当前月减去一个月String before = sdf.format(c.getTime());//上个月的当前日期String now = sdf.format(new Date());//当前日期String nowMonthLateDay = getNextDay(now, "-1");//当前日期的前一天String lateMonthLateDay = getNextDay(before, "-1");//上个月的当前日期的前一天Calendar cal = Calendar.getInstance();
// int m=cal.get(Calendar.MONTH)+1;//获取当前月份
// System.err.println("当前月份:"+m);String nowMonthFirstDay = getFirstDayOfMonth(1);//获取当前月份的第一天cal.add(Calendar.MONTH, -1);cal.set(Calendar.DAY_OF_MONTH, 1); //重写当前日期String lateMonthFirstDay = sdf.format(cal.getTime());//获取上个月的第一天
4. 获取当前年度,如果超过某个时间,就减去一年或者加上一年
Date date = new Date();//获取日期SimpleDateFormat dateFormats= new SimpleDateFormat("yyyy");//日期格式String year=dateFormats.format(date);//获取当前年度DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");//把String转为LocalDateLocalDate localTime=LocalDate.parse(year+"-06-30",dtf);Integer years=Integer.valueOf(year);if (!LocalDate.now().isAfter(localTime)){//判断时间是否超过years=Integer.valueOf(year)-1;}System.err.println(years);}
5. 获取时间的工具类
/*** 将短时间格式字符串转换为时间 yyyy-MM-dd** @param strDate* @return*/public static Date strToDate(String strDate) {SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");ParsePosition pos = new ParsePosition(0);Date strtodate = formatter.parse(strDate, pos);return strtodate;}/*** 得到一个时间延后或前移几天的时间,nowdate为时间,delay为前移或后延的天数*/public static String getNextDay(String nowdate, String delay) {try {SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");String mdate = "";Date d = strToDate(nowdate);long myTime = (d.getTime() / 1000) + Integer.parseInt(delay) * 24 * 60 * 60;d.setTime(myTime * 1000);mdate = format.format(d);return mdate;} catch (Exception e) {return "";}}/*** 得到一个时间延后或前移几天的时间,nowdate为时间,delay为前移或后延的天数*/public static String getNextDays(String nowdate, String delay) {try {SimpleDateFormat format = new SimpleDateFormat("-MM-dd");String mdate = "";Date d = strToDate(nowdate);long myTime = (d.getTime() / 1000) + Integer.parseInt(delay) * 24 * 60 * 60;d.setTime(myTime * 1000);mdate = format.format(d);return mdate;} catch (Exception e) {return "";}}/*** 获取当前小时** @return*/public static String getTimeShort() {SimpleDateFormat formatter = new SimpleDateFormat(" HH");Date currentTime = new Date();String dateString = formatter.format(currentTime);return dateString;}/*** 获取当前月第一天** @param month* @return*/public static String getFirstDayOfMonth(int month) {Calendar calendar = Calendar.getInstance();// 设置月份calendar.set(Calendar.MONTH, month - 1);// 获取某月最小天数int firstDay = calendar.getActualMinimum(Calendar.DAY_OF_MONTH);// 设置日历中月份的最小天数calendar.set(Calendar.DAY_OF_MONTH, firstDay);// 格式化日期SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");String firstDays = sdf.format(calendar.getTime());return firstDays;}
//g过去年度public static String gerLateYear(Integer num){SimpleDateFormat format = new SimpleDateFormat("yyyy");Calendar c = Calendar.getInstance();//过去一年c.setTime(new Date());c.add(Calendar.YEAR, num);Date y = c.getTime();String year = format.format(y);return year;}//判断是否超过指定时间public static boolean afterDate(String date) {DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");//把String转为LocalDateLocalDate localTime = LocalDate.parse(date, dtf);//判断当前日期是否大于指定日期return LocalDate.now().isAfter(localTime);}