Java獲取給定日期的月初和月末兩個日期


在Java開發過程中,我們會遇到很多關於日期操作的問題,今天就跟大家分享一下如何獲取給定日期的月初和月末兩個日期

代碼如下:

public String getFirstAndLastDayOfMonth() throws Exception {

  String date_str = "2019-02-15";

  Calendar cale = Calendar.getInstance();

  SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");

  cale.setTime(formatter.parse(date_str));

  

  cale.add(Calendar.MONTH, 0);

  cale.set(Calendar.DAY_OF_MONTH, 1);

  String firstDayOfMonth = formatter.format(cale.getTime()); // 當月第一天 2019-02-01

  

  cale.add(Calendar.MONTH, 1);

  cale.set(Calendar.DAY_OF_MONTH, 0);

  String lastDayOfMonth = formatter.format(cale.getTime()); // 當月最后一天 2019-02-28

}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM