国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

使用java的Calendar對象獲得當前日期

2019-11-26 15:07:08
字體:
來源:轉載
供稿:網友

思路:

先獲得當前季度的開始和結束日期,在當前日期的基礎上往前推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 <= 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);// 設置日期      calendar.add(Calendar.MONTH, -3);      resultDate = longSdf.format(calendar.getTime());           } catch (Exception e) {      ;    }    return resultDate;  }

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 晋城| 金川县| 嘉善县| 汝城县| 蒲江县| 大田县| 桓台县| 苗栗市| 云霄县| 孟村| 卫辉市| 宣武区| 长宁区| 沂水县| 额尔古纳市| 交口县| 长海县| 永吉县| 鹤壁市| 随州市| 青州市| 屯留县| 开远市| 当阳市| 卓资县| 海原县| 曲阜市| 沁水县| 嘉兴市| 陈巴尔虎旗| 安多县| 灌云县| 潞西市| 兰溪市| 邵武市| 吉林省| 友谊县| 乐昌市| 金湖县| 福贡县| 台安县|