package com.lish.taskchart.utils;import java.util.Calendar;import java.util.Date;
public class DateUtils {
public static final String[] sWeekDay = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sta"};
/**
* 给定日期 返回星期的字符串
*/
public static String getWeekDay(Date date) {
int ofWeek = getIndexDayOfWeek(date);
return sWeekDay[ofWeek - 1];
}
/**
* 比当前日期增加一天
*/
public static Date addOneDay(Date someDay) {
return addSomeDay(someDay, 1);
}
/**
* 比当前日期减少一天
*/
public static Date subOneDay(Date someDay) {
return addSomeDay(someDay, -1);
}
/**
* 比当前多num天 num > 0,增加了几天,num < 0 前几天
*/
public static Date addSomeDay(Date someDay, int num) {
Calendar instance = Calendar.getInstance();
instance.setTime(someDay);
instance.add(Calendar.DATE, num);
return instance.getTime();
}
/**
* 两个日期的相差天数 含正负
* antherDay 在之后 day 返回正值 否则 负值
*/
public static int getDaysBetween(Date day, Date antherDay) {
boolean symbol = day.after(antherDay);//默认 antherDay 在 day 后的某一天
if (symbol) {//时间交换
Date temp = day;
day = antherDay;
antherDay = temp;
}
Calendar calendar = Calendar.getInstance();
calendar.setTime(day);
Calendar d1 = (Calendar) calendar.clone();
calendar.setTime(antherDay);
int days = calendar.get(Calendar.DAY_OF_YEAR) - d1.get(Calendar.DAY_OF_YEAR);
int year = calendar.get(Calendar.YEAR);
if (d1.get(Calendar.YEAR) != year) {
do {
days += d1.getActualMaximum(Calendar.DAY_OF_YEAR);
d1.add(Calendar.YEAR, 1);
} while (d1.get(Calendar.YEAR) != year);
}
return symbol ? -days : days;
}
/**
* 返回当前日期是周几
* Calendar.SUNDAY = 1
* Calendar.MONDAY = 2
* Calendar.TUESDAY = 3
* Calendar.WEDNESDAY = 4
* Calendar.THURSDAY = 5
* Calendar.FRIDAY = 6
* Calendar.SATURDAY = 7
*/
public static int getIndexDayOfWeek(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.DAY_OF_WEEK);
}
/**
* 这里转换为 周一到周日
*/
public static Date[] getWeekDates(Date date) {
Date[] dates = new Date[7];
int ofWeek = getIndexDayOfWeek(date);
int sub = ofWeek - 2;//前面的天数
if (sub < 0) {//今天是周日
sub = 6;
}
for (int i = 0; i < 7; i++) {
if (sub > 0) {
dates[i] = addSomeDay(date, -sub);
sub--;
} else if (sub == 0) {
dates[i] = date;
} else {
dates[i] = addSomeDay(date, i - ofWeek + 2);//i+2 是为了和 周几同步 周二 为 3,此时 i = 1;
}
}
return dates;
}
/**
* 返回给定日期月份的天数
*/
public static int getTotalDayOfMonth(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
}
/**
* 返回给定日期所在月份第一天的日期
*/
public static Date getFirstDayOfMonth(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int minimum = calendar.getActualMinimum(Calendar.DAY_OF_MONTH);
calendar.set(Calendar.DAY_OF_MONTH, minimum);
return calendar.getTime();
}
/**
* 返回给定日期所在月份最后一天的日期
*/
public static Date getLastDayOfMonth(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int maximum = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
calendar.set(Calendar.DAY_OF_MONTH, maximum);
return calendar.getTime();
}
}