欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用java的Calendar對象獲得當(dāng)前日期

 更新時間:2015年07月02日 08:39:03   投稿:hebedich  
本文給大家分享的是使用使用java的Calendar對象獲得當(dāng)前日期的上幾個度開始、結(jié)束時間,主要思路是先獲得當(dāng)前季度的開始和結(jié)束日期,在當(dāng)前日期的基礎(chǔ)上往前推3個月即上個季度的開始和結(jié)束日期,十分的實(shí)用,小伙伴們可以參考下。

思路:

先獲得當(dāng)前季度的開始和結(jié)束日期,在當(dāng)前日期的基礎(chǔ)上往前推3個月即上個季度的開始和結(jié)束日期

/**
   * @param flag true:開始日期;false:結(jié)束日期
   * @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:結(jié)束日期
      if(flag){
        if (currentMonth >= 1 && currentMonth <= 3)
          calendar.set(Calendar.MONTH, 0);
        else if (currentMonth >= 4 && currentMonth <= 6)
          calendar.set(Calendar.MONTH, 3);
        else if (currentMonth >= 7 && currentMonth <= 9)
          calendar.set(Calendar.MONTH, 6);
        else if (currentMonth >= 10 && currentMonth <= 12)
          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 <= 3) {
          calendar.set(Calendar.MONTH, 2);
          calendar.set(Calendar.DATE, 31);
        } else if (currentMonth >= 4 && currentMonth <= 6) {
          calendar.set(Calendar.MONTH, 5);
          calendar.set(Calendar.DATE, 30);
        } else if (currentMonth >= 7 && currentMonth <= 9) {
          calendar.set(Calendar.MONTH, 8);
          calendar.set(Calendar.DATE, 30);
        } else if (currentMonth >= 10 && currentMonth <= 12) {
          calendar.set(Calendar.MONTH, 11);
          calendar.set(Calendar.DATE, 31);
        }
        now = longSdf.parse(shortSdf.format(calendar.getTime()) + " 23:59:59");
      }
      calendar.setTime(now);// 設(shè)置日期
      calendar.add(Calendar.MONTH, -3);
      resultDate = longSdf.format(calendar.getTime());
       
    } catch (Exception e) {
      ;
    }
    return resultDate;
  }

相關(guān)文章

最新評論