/** *获取当月的开始日期 *@author: lkf *@Date: 2017/9/13 11:16 */ public static Date getCurrentMonthStartTime() { SimpleDateFormat shortSdf = new SimpleDateFormat("yyyy-MM-dd"); Calendar c = Calendar.getInstance(); Date now = null; try { c.set(Calendar.DATE, 1); now = shortSdf.parse(shortSdf.format(c.getTime())); } catch (Exception e) { e.printStackTrace(); } return now; } /** *获取当月的结束日期 *@author: lkf *@Date: 2017/9/13 11:16 */ public static Date getCurrentMonthEndTime() { SimpleDateFormat shortSdf = new SimpleDateFormat("yyyy-MM-dd"); Calendar c = Calendar.getInstance(); Date now = null; try { c.set(Calendar.DATE, 1); c.add(Calendar.MONTH, 1); c.add(Calendar.DATE, -1); now = shortSdf.parse(shortSdf.format(c.getTime())); } catch (Exception e) { e.printStackTrace(); } return now; }
public static void main(String[] args) { Date date = DateUtils.formatDate("2017-09-30","yyyy-MM-dd"); Date a = getCurrentMonthStartTime(); Date b = getCurrentMonthEndTime(); if(date.getTime() >= a.getTime() && date.getTime() <= b.getTime()){ System.out.println("是当月数据"); }else{ System.out.println("不是当月数据"); } }
首先获取当前月的开始日期和结束日期,然后用你要比较的日期去进行比较。直接上代码,简单易懂。为了防止以后经常用到就写到博客里面把保存咯。希望可以帮助大家减少开发时间。绝得好用就点个赞吧
哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈
时间: 2024-10-14 17:41:23