简简单单,代码如下
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class DateCalendarUtils {
public static Date getMonth(Date pase) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String date1 = sdf.format(pase);
Date date = sdf.parse(date1);
Calendar c = Calendar.getInstance();
c.setTime(date);
c.add(Calendar.MONTH, 0);
c.set(Calendar.DAY_OF_MONTH, 1);
Date first = c.getTime();
return first;
}
public static Date getNextMonth(Date pase) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String date1 = sdf.format(pase);
Date date = sdf.parse(date1);
Calendar ca = Calendar.getInstance();
ca.setTime(date);
ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
Date last = ca.getTime();
long time = last.getTime() + 24 * 60 * 60 * 1000L;
Date next = new Date(time);
return next;
}
public static Date getMonday(Date now) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String date1 = sdf.format(now);
Date parse = sdf.parse(date1);
Calendar calendar = Calendar.getInstance();
calendar.setTime(parse);
int week = calendar.get(Calendar.DAY_OF_WEEK);
long time = parse.getTime();
long monday = time - (week-1) * 24 * 60 * 60 * 1000;
Date date = new Date(monday);
return date;
}
@SuppressWarnings("deprecation")
public static Date nextMonday(Date now) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String date1 = sdf.format(now);
Date parse = sdf.parse(date1);
int week = parse.getDay();
long time = parse.getTime();
long sunday = time + (7 - week) * 24 * 60 * 60 * 1000;
Date date = new Date(sunday);
return date;
}
public static int countDays(Date beginTime, Date endTime) {
long count = endTime.getTime() - beginTime.getTime();
boolean sameDate = isSameDate(beginTime, endTime);
if (sameDate) {
return 1;
} else {
long oneDay = 24 * 60 * 60 * 1000;
long num = count % oneDay;
int days = 0;
if (num != 0) {
days = (int) (1 + count / oneDay);
} else {
days = (int) (count / oneDay);
}
return days;
}
}
public static boolean isSameDate(Date date1, Date date2) {
Calendar cal1 = Calendar.getInstance();
cal1.setTime(date1);
Calendar cal2 = Calendar.getInstance();
cal2.setTime(date2);
boolean isSameYear = cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR);
boolean isSameMonth = isSameYear && cal1.get(Calendar.MONTH) == cal2.get(Calendar.MONTH);
boolean isSameDate = isSameMonth
&& cal1.get(Calendar.DAY_OF_MONTH) == cal2.get(Calendar.DAY_OF_MONTH);
return isSameDate;
}
public static boolean isSameWeeks(Date now, Date beginTime)
{
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String date1 = sdf.format(now);
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
String date2 = sdf1.format(beginTime);
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date d1 = null;
Date d2 = null;
try {
d1 = format.parse(date1);
d2 = format.parse(date2);
} catch (Exception e) {
e.printStackTrace();
}
Calendar cal1 = Calendar.getInstance();
Calendar cal2 = Calendar.getInstance();
cal1.setTime(d1);
cal2.setTime(d2);
int subYear = cal1.get(Calendar.YEAR) - cal2.get(Calendar.YEAR);
if (subYear == 0) {
if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))
return true;
} else if (subYear == 1 && cal2.get(Calendar.MONTH) == 11) {
if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))
return true;
}
else if (subYear == -1 && cal1.get(Calendar.MONTH) == 11) {
if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))
return true;
}
return false;
}
@SuppressWarnings("deprecation")
public static long getNextMonthTime(long time) {
Date date = new Date(time);
int hours = date.getHours();
int minutes = date.getMinutes();
int seconds = date.getSeconds();
int day = date.getDate();
int month = date.getMonth();
int year = date.getYear();
if (month == 11) {
year++;
}
month = (month + 1)%12;
return new Date(year, month, day, hours, minutes, seconds).getTime();
}
}