打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
使用java的Calendar对象获得当前日期

      本文给大家分享的是使用使用java的Calendar对象获得当前日期的上几个度开始、结束时间,主要思路是先获得当前季度的开始和结束日期,在当前日期的基础上往前推3个月即上个季度的开始和结束日期,十分的实用,小伙伴们可以参考下。

      思路:

      先获得当前季度的开始和结束日期,在当前日期的基础上往前推3个月即上个季度的开始和结束日期

      /** 

      * @param flag true:开始日期;false:结束日期 

      * @return 

      */

      public static String getLastQuarterTime(boolean flag){ 

      SimpleDateFormat shortSdf = new SimpleDateFormat('yyyy-MM-dd'); 

      SimpleDateFormat longSdf = new SimpleDateFormat('yyyy-MM-dd HH:mm:ss'); 

      String resultDate=''; 

      Date now = null; 

      try { 

      Calendar calendar = Calendar.getInstance(); 

      int currentMonth = calendar.get(Calendar.MONTH) + 1; 

      //true:开始日期;false:结束日期 

      if(flag){ 

      if (currentMonth >= 1 && currentMonth <>

      calendar.set(Calendar.MONTH, 0); 

      else if (currentMonth >= 4 && currentMonth <>

      calendar.set(Calendar.MONTH, 3); 

      else if (currentMonth >= 7 && currentMonth <>

      calendar.set(Calendar.MONTH, 6); 

      else if (currentMonth >= 10 && currentMonth <>

      calendar.set(Calendar.MONTH, 9); 

      calendar.set(Calendar.DATE, 1); 

      now = longSdf.parse(shortSdf.format(calendar.getTime()) + ' 00:00:00'); 

      }else{ 

      if (currentMonth >= 1 && currentMonth <>

      calendar.set(Calendar.MONTH, 2); 

      calendar.set(Calendar.DATE, 31); 

      } else if (currentMonth >= 4 && currentMonth <>

      calendar.set(Calendar.MONTH, 5); 

      calendar.set(Calendar.DATE, 30); 

      } else if (currentMonth >= 7 && currentMonth <>

      calendar.set(Calendar.MONTH, 8); 

      calendar.set(Calendar.DATE, 30); 

      } else if (currentMonth >= 10 && currentMonth <>

      calendar.set(Calendar.MONTH, 11); 

      calendar.set(Calendar.DATE, 31); 

      } 

      now = longSdf.parse(shortSdf.format(calendar.getTime()) + ' 23:59:59'); 

      } 

      calendar.setTime(now);// 设置日期 

      calendar.add(Calendar.MONTH, -3); 

      resultDate = longSdf.format(calendar.getTime()); 

      } catch (Exception e) { 

      ; 

      } 

      return resultDate; 

      } 

      文章来源:http://www.chinatjetc.com/article.php?CID=26&ID=677

      了解更多内容可登录中软国际官方网站:www.chinatjetc.com

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
java获取当月的第一天或最后一天
JAVA 日期加减运算
java日期增加一个月或减少一天
Java常用时间操作工具类
Calendar获取上个月第一天和最后一天,上周第一天和最后一天
获取日期对应是星期几
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服