java获取某天所在月的最后一天

//获取某天所在月的最后一天
public static String getlastday(String day) throws Exception{

    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
    SimpleDateFormat sdf_ny = new SimpleDateFormat("yyyyMM");

    Calendar calendar = Calendar.getInstance();
    calendar.setTime(sdf.parse(day));
    int lastday = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
    String endday = sdf_ny.format(sdf.parse(day))+lastday;

    return endday;
}

public static void main(String[] args) throws Exception{
    System.out.println(getlastday("20190201"));//20190228
    System.out.println(getlastday("20200201"));//20200229
    System.out.println(getlastday("20190708"));//20190731
}

你可能感兴趣的:(工具类,java)