JAVA获取本周 本月 本年 第一天和最后一天
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | /** * 日期工具类 */ public class DateUtils { /** * 获取今天 * @return String * */ public static String getToday(){ return new SimpleDateFormat( "yyyy-MM-dd" ).format( new Date()); } /** * 获取昨天 * @return String * */ public static String getYestoday(){ Calendar cal=Calendar.getInstance(); cal.add(Calendar.DATE,-1); Date time=cal.getTime(); return new SimpleDateFormat( "yyyy-MM-dd" ).format(time); } /** * 获取本月开始日期 * @return String * **/ public static String getMonthStart(){ Calendar cal=Calendar.getInstance(); cal.add(Calendar.MONTH, 0); cal. set (Calendar.DAY_OF_MONTH, 1); Date time=cal.getTime(); return new SimpleDateFormat( "yyyy-MM-dd" ).format(time)+" 00:00:00; } /** * 获取本月最后一天 * @return String * **/ public static String getMonthEnd(){ Calendar cal=Calendar.getInstance(); cal. set (Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH)); Date time=cal.getTime(); return new SimpleDateFormat( "yyyy-MM-dd" ).format(time)+ " 23:59:59" ; } /** * 获取本周的第一天 * @return String * **/ public static String getWeekStart(){ Calendar cal=Calendar.getInstance(); cal.add(Calendar.WEEK_OF_MONTH, 0); cal. set (Calendar.DAY_OF_WEEK, 2); Date time=cal.getTime(); return new SimpleDateFormat( "yyyy-MM-dd" ).format(time)+ " 00:00:00" ; } /** * 获取本周的最后一天 * @return String * **/ public static String getWeekEnd(){ Calendar cal=Calendar.getInstance(); cal. set (Calendar.DAY_OF_WEEK, cal.getActualMaximum(Calendar.DAY_OF_WEEK)); cal.add(Calendar.DAY_OF_WEEK, 1); Date time=cal.getTime(); return new SimpleDateFormat( "yyyy-MM-dd" ).format(time)+ " 23:59:59" ; } /** * 获取本年的第一天 * @return String * **/ public static String getYearStart(){ return new SimpleDateFormat( "yyyy" ).format( new Date())+ "-01-01" ; } /** * 获取本年的最后一天 * @return String * **/ public static String getYearEnd(){ Calendar calendar = Calendar.getInstance(); calendar. set (Calendar.MONTH,calendar.getActualMaximum(Calendar.MONTH)); calendar. set (Calendar.DAY_OF_MONTH,calendar.getActualMaximum(Calendar.DAY_OF_MONTH)); Date currYearLast = calendar.getTime(); return new SimpleDateFormat( "yyyy-MM-dd" ).format(currYearLast)+ " 23:59:59" ; } } |
如果帮到你啦 可以右侧打赏一下哦 不胜感激 您的打赏是我前进的动力~~~~ 比心心
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步