SimpleDateFormat sm = new SimpleDateFormat("yyyy-MM-dd");
String str = "2019-02-05";
String firstday, lastday;
Date date = sm.parse(str);
Calendar cale = Calendar.getInstance();
cale.setTime(date);
cale.add(Calendar.MONTH, 0);
cale.set(Calendar.DAY_OF_MONTH, 1);
firstday = sm.format(cale.getTime());
// 获取前月的最后一天
cale = Calendar.getInstance();
cale.setTime(date);
cale.add(Calendar.MONTH, 1);
cale.set(Calendar.DAY_OF_MONTH, 0);
lastday = sm.format(cale.getTime());
System.out.println("本月第一天和最后一天分别是 : " + firstday+" and " + lastday);
传入一个字符串日期,获取此日期的当月的第一天和最后一天
最新推荐文章于 2024-03-07 10:37:03 发布