1. 方法1: 如果是 java8, 可以使用 LocalDate 获取 // 获取当前日期前一天的日期 LocalDate localDate = LocalDate.now().minusDays(1
// 获取当前日期前一天的日期 LocalDate localDate = LocalDate.now().minusDays(1);//1代表提前多少天 // 获取时间字符串如: 2023-03-01 System.out.println(localDate.toString()); // 获取当前年如 2023 System.out.println(localDate.getYear()); // 获取当前月如 3 System.out.println(localDate.getMonthValue()); // 获取当前天如 1 System.out.println(localDate.getDayOfMonth()); //获取前一天的日期 String date = localDate.fORMat(DateTimeFormatter.ofPattern("yyyy-MM-dd"));//转为String类型
方法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
//获取当前日期 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()));
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());//获取上个月的第一天
Date date = new Date();//获取日期 SimpleDateFormat dateFormats= new SimpleDateFormat("yyyy");//日期格式 String year=dateFormats.format(date);//获取当前年度 DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd"); //把String转为LocalDate LocalDate 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); }
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; } 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 ""; } } 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 ""; } } public static String getTimeShort() { SimpleDateFormat formatter = new SimpleDateFormat(" HH"); Date currentTime = new Date(); String dateString = formatter.format(currentTime); return dateString; } 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转为LocalDate LocalDate localTime = LocalDate.parse(date, dtf); //判断当前日期是否大于指定日期 return LocalDate.now().isAfter(localTime); }
来源地址:https://blog.csdn.net/m0_54790214/article/details/129280230
--结束END--
本文标题: Java 获取日期前一年、月、日,指定日期的前几天,后几天
本文链接: https://www.lsjlt.com/news/385176.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-04-03
2024-04-03
2024-04-01
2024-01-21
2024-01-21
2024-01-21
2024-01-21
2023-12-23
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0