zhao1949

https://blog.csdn.net/wtopps/article/details/48262221

*****************************************************************

不同的月可能有不同的天数, 有的是30天, 有的是31天, 对于二月的天数判断还得先判断是平年还是闰年, 所以如果自己写代码判断会比较麻烦, 其实java.util.Calendar中已经提供了获取天数的方法, 代码如下:

 

package top.itart;
 
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
 
public class DateUtil {
 
    public static int getDaysOfMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
    }
 
    public static void main(String[] args) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        System.out.println(getDaysOfMonth(sdf.parse("2015-02-2")));
    }
}

 

分类:

技术点:

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-11-29
  • 2022-12-23
  • 2022-12-23
  • 2021-10-09
  • 2021-09-06
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-09-12
  • 2021-11-28
  • 2022-12-23
  • 2022-12-23
  • 2022-03-04
  • 2022-12-23
相关资源
相似解决方案