Java 日期加减 该当日期是所在月的第几周 所在月的最后一天

/**
* 根据日期,取得星期几

* @param date
* @return
*/
private String getWeek(Date date) {
SimpleDateFormat sdf = new SimpleDateFormat("EEEE");
String week = sdf.format(date);
return week.substring(0, 1);
}


@SuppressWarnings("unused")
private String getWeek(String date) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("EEEE");
String week = sdf.format(stringToDate(date));
return week.substring(0, 1);
}


/**
* 日期的天数的加减

* @param date
* @param con
* @return
* @throws ParseException
*/
@SuppressWarnings("unused")
private String getDateTO(String date, int count) throws ParseException {


SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat sdf = new SimpleDateFormat("EEEE");


String week = sdf.format(stringToDate(date));
Calendar rightNow = Calendar.getInstance();
rightNow.setTime(stringToDate(date));
rightNow.add(Calendar.DATE, count);


Date dt1 = rightNow.getTime();


String str = dateFormat.format(dt1).replaceAll("-", "");


return str;
}


/**
* String类型变为DATE类型

* @param dateStr
* @param con
* @return
* @throws ParseException
*/
@SuppressWarnings("unused")
private Date stringToDate(String dateStr) throws ParseException {
String dateString = "0000-00-00";
if (!"".equals(dateStr) && dateStr != null) {
if (dateStr.contains("-")) {
dateString = dateStr;
} else {
dateString = dateStr.substring(0, 4) + "-"
+ dateStr.substring(4, 6) + "-"
+ dateStr.substring(6, 8);
}
}
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
return dateFormat.parse(dateString);


}


/**
* 取得一个月的第一天

* @param someDate
* @return
*/
@SuppressWarnings("unused")
private String getMonthFirstDay(Date someDate) {


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


String getMonthFirstDay = "";
Calendar ca = Calendar.getInstance();
ca.setTime(someDate);
ca.set(Calendar.DAY_OF_MONTH, 1);
// 一日目
Date firstDate = ca.getTime();
getMonthFirstDay = dateFormat.format(firstDate);


return getMonthFirstDay;


}


/**
* 取得一个月的最后一天

* @param someDate
* @return
*/
@SuppressWarnings("unused")
private String getMonthLastDay(Date someDate) {


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


String getMonthLastDay = "";
Calendar ca = Calendar.getInstance();
ca.setTime(someDate);
ca.add(Calendar.MONTH, 1);
ca.add(Calendar.DATE, -1);
// 最後日
getMonthLastDay = dateFormat.format(ca.getTime());


return getMonthLastDay;


}


/**取得一个日期是所在月的第几周

* @param str
* @return
* @throws Exception
*/
private int getWeekNum(String str) throws Exception {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = sdf.parse(str);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int week = calendar.get(Calendar.WEEK_OF_MONTH);
int day = calendar.get(Calendar.DAY_OF_WEEK);
return week;
}

你可能感兴趣的:(Java 日期加减 该当日期是所在月的第几周 所在月的最后一天)