java获取当前月和周的第一天和最后一天

获取当前周的第一天:

public static Date getFirstDayOfWeek(Date date) {
    Calendar cal = Calendar.getInstance();
    try {
        cal.setTime(date);
        cal.set(Calendar.DAY_OF_WEEK, 2);
    } catch (Exception e) {
        e.printStackTrace();
    }

    return cal.getTime();
}

获取当前周最后一天:

public static Date getLastDayOfWeek(Date date) {
    Calendar cal = Calendar.getInstance();

    try {
        cal.setTime(date);
        cal.set(Calendar.DAY_OF_WEEK, 2);
        cal.set(Calendar.DATE, cal.get(Calendar.DATE) + 6);
    } catch(Exception e) {
        e.printStackTrace();
    }

    return cal.getTime();
}

 

获取当前月第一天:

public static String getCurrentMonthFirstDay() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");  
        Calendar cale = Calendar.getInstance();  
        cale = Calendar.getInstance();  
        cale.add(Calendar.MONTH, 0);  
        cale.set(Calendar.DAY_OF_MONTH, 1);  
        return format.format(cale.getTime());
    }

 

获取当前月最后一天:

public static String getCurrentMonthLastDay() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");  
        Calendar cale = Calendar.getInstance();  
        cale = Calendar.getInstance();  
        cale.add(Calendar.MONTH, 1);  
        cale.set(Calendar.DAY_OF_MONTH, 0);  
        return format.format(cale.getTime());
    }

 

你可能感兴趣的:(杂七杂八)