Java 日期工具类(日期,月份加减等)--转
- package util;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.Calendar;
- import java.util.Date;
- /***
- * 日期工具类
- *
- * @author damao
- *
- */
- public class DateAndTimeUtil {
- /***
- * 日期月份减一个月
- *
- * @param datetime
- * 日期(2014-11)
- * @return 2014-10
- */
- public static String dateFormat(String datetime) {
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
- Date date = null;
- try {
- date = sdf.parse(datetime);
- } catch (ParseException e) {
- e.printStackTrace();
- }
- Calendar cl = Calendar.getInstance();
- cl.setTime(date);
- cl.add(Calendar.MONTH, -1);
- date = cl.getTime();
- return sdf.format(date);
- }
- public static String dateFormat(Date date) {
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
- return sdf.format(date);
- }
- /****
- * 传入具体日期 ,返回具体日期减一个月。
- *
- * @param date
- * 日期(2014-04-20)
- * @return 2014-03-20
- * @throws ParseException
- */
- public static String subMonth(String date) throws ParseException {
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
- Date dt = sdf.parse(date);
- Calendar rightNow = Calendar.getInstance();
- rightNow.setTime(dt);
- rightNow.add(Calendar.MONTH, -1);
- Date dt1 = rightNow.getTime();
- String reStr = sdf.format(dt1);
- return reStr;
- }
- /****
- * 获取月末最后一天
- *
- * @param sDate
- * 2014-11-24
- * @return 30
- */
- private static String getMonthMaxDay(String sDate) {
- SimpleDateFormat sdf_full = new SimpleDateFormat("yyyy-MM-dd");
- Calendar cal = Calendar.getInstance();
- Date date = null;
- try {
- date = sdf_full.parse(sDate + "-01");
- } catch (ParseException e) {
- e.printStackTrace();
- }
- cal.setTime(date);
- int last = cal.getActualMaximum(Calendar.DATE);
- return String.valueOf(last);
- }
- // 判断是否是月末
- public static boolean isMonthEnd(Date date) {
- Calendar cal = Calendar.getInstance();
- cal.setTime(date);
- if (cal.get(Calendar.DATE) == cal
- .getActualMaximum(Calendar.DAY_OF_MONTH))
- return true;
- else
- return false;
- }
- /***
- * 日期减一天、加一天
- *
- * @param option
- * 传入类型 pro:日期减一天,next:日期加一天
- * @param _date
- * 2014-11-24
- * @return 减一天:2014-11-23或(加一天:2014-11-25)
- */
- public static String checkOption(String option, String _date) {
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
- Calendar cl = Calendar.getInstance();
- Date date = null;
- try {
- date = (Date) sdf.parse(_date);
- } catch (ParseException e) {
- e.printStackTrace();
- }
- cl.setTime(date);
- if ("pre".equals(option)) {
- // 时间减一天
- cl.add(Calendar.DAY_OF_MONTH, -1);
- } else if ("next".equals(option)) {
- // 时间加一天
- cl.add(Calendar.DAY_OF_YEAR, 1);
- } else {
- // do nothing
- }
- date = cl.getTime();
- return sdf.format(date);
- }
- /***
- * 判断日期是否为当前月, 是当前月返回当月最小日期和当月目前最大日期以及传入日期上月的最大日和最小日
- * 不是当前月返回传入月份的最大日和最小日以及传入日期上月的最大日和最小日
- *
- * @param date
- * 日期 例如:2014-11
- * @return String[] 开始日期,结束日期,上月开始日期,上月结束日期
- * @throws ParseException
- */
- public static String[] getNow_Pre_Date(String date) throws ParseException {
- String[] str_date = new String[4];
- Date now = new Date();
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
- SimpleDateFormat sdf_full = new SimpleDateFormat("yyyy-MM-dd");
- String stMonth = sdf.format(now);
- String stdate = "";// 开始日期
- String endate = "";// 结束日期
- String preDate_start = "";// 上月开始日期
- String preDate_end = "";// 上月结束日期
- // 当前月
- if (date.equals(stMonth)) {
- stdate = stMonth + "-01"; // 2014-11-01
- endate = sdf_full.format(now);// 2014-11-24
- preDate_start = subMonth(stdate);// 2014-10-01
- preDate_end = subMonth(endate);// 2014-10-24
- } else {
- // 非当前月
- String monthMaxDay = getMonthMaxDay(date);
- stdate = date + "-01";// 2014-10-01
- endate = date + "-" + monthMaxDay;// 2014-10-31
- preDate_start = subMonth(stdate);// 2014-09-01
- preDate_end = subMonth(endate);// 2014-09-30
- }
- str_date[0] = stdate;
- str_date[1] = endate;
- str_date[2] = preDate_start;
- str_date[3] = preDate_end;
- return str_date;
- }
- public static void main(String[] args) throws ParseException {
- /*
- * String a =DateAndTimeUtil.dateFormat(new Date());
- * System.out.println(a); String b =
- * DateAndTimeUtil.subMonth("2014-03-31"); System.out.println(b);
- * SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); Date
- * dt=sdf.parse("2014-03-31");
- * System.out.println(DateAndTimeUtil.isMonthEnd(dt));
- */
- String str = null;
- // str = DateAndTimeUtil.checkOption("next", "2014-11-30");
- // str = getMonthMaxDay("2014-11-24");
- // str = dateFormat("2014-11");
- str = getNow_Pre_Date("2014-10")[0];
- System.out.println(str);
- }
- }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· AI与.NET技术实操系列(六):基于图像分类模型对图像进行分类