Android 获得
星期几,昨天今天明天 ,日期所在月的第一天和最后一天
最近用到的一些时间控制,记录一下。
明天的时间戳:long nextDay = System.currentTimeMillis() +24 *60 *60 *1000L;//明天
/**
* 获取传入日期所在月的第一天
*/
public static DategetFirstDayDateOfMonth(final Date date) {
final Calendar cal = Calendar.getInstance();
cal.setTime(date);
final int last = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
cal.set(Calendar.DAY_OF_MONTH, last);
return cal.getTime();
}
/**
* 获取传入日期所在月的最后一天
*/
public static DategetLastDayOfMonth(final Date date) {
final Calendar cal = Calendar.getInstance();
cal.setTime(date);
final int last = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
cal.set(Calendar.DAY_OF_MONTH, last);
return cal.getTime();
}
/**
* 返回星期几
*/
public static StringgetWeekNameFromMillis(Long millis) {
String weekStr ="";
if (millis ==null || millis ==0) {
L.e("时间有错");
return weekStr;
}
int selectDate =getWeekValueFromMillis(millis);
switch (selectDate) {
case 1:
weekStr ="周日";
break;
case 2:
weekStr ="周一";
break;
case 3:
weekStr ="周二";
break;
case 4:
weekStr ="周三";
break;
case 5:
weekStr ="周四";
break;
case 6:
weekStr ="周五";
break;
case 7:
weekStr ="周六";
break;
default:
weekStr ="";
break;
}
return weekStr;
}
/**
* 昨天 今天 明天
*/
public static StringgetDayFromMillis(Long millis) {
String weekStr ="";
if (millis ==null || millis ==0) {
L.e("时间有错");
return weekStr;
}
int currentDate =getWeekValueFromMillis(System.currentTimeMillis());
int selectDate =getWeekValueFromMillis(millis);
int currentDay =getDayoFMonthFromMillis(System.currentTimeMillis());
int selectDay =getDayoFMonthFromMillis(millis);
if (bIsCurrentMonth(millis) && currentDay == (selectDay +1) && currentDate == (selectDate +1)) {
return "昨天";
}else if (bIsCurrentMonth(millis) && currentDay == selectDay && currentDate == selectDate) {
return "今天";
}else if (bIsCurrentMonth(millis) && currentDay == (selectDay -1) && currentDate == (selectDate -1)) {
return "明天";
}
return weekStr;
}
/**
* 必须是当前年份,不能跨年
*/
public static boolean bIsCurrentMonth(long millis) {
Calendar calendar = Calendar.getInstance(TimeUtils.getDefaultTimeZone(), TimeUtils.getDefaultLocale());
int current = calendar.get(Calendar.MONTH);
int year = calendar.get(Calendar.YEAR);
return year ==getYearValueOfMillis(millis) && current ==getMonthValueFromMillis(millis);
}
public static int getMonthValueFromMillis(long millis) {
Calendar calendar = Calendar.getInstance(TimeUtils.getDefaultTimeZone(), TimeUtils.getDefaultLocale());
calendar.setTimeInMillis(millis);
return calendar.get(Calendar.MONTH);
}
public static int getYearValueOfMillis(long millis) {
Calendar calendar = Calendar.getInstance(TimeUtils.getDefaultTimeZone(), TimeUtils.getDefaultLocale());
calendar.setTimeInMillis(millis);
return calendar.get(Calendar.YEAR);
}
public static int getWeekValueFromMillis(long millis) {
Calendar calendar = Calendar.getInstance(TimeUtils.getDefaultTimeZone(), TimeUtils.getDefaultLocale());
calendar.setTimeInMillis(millis);
return calendar.get(Calendar.DAY_OF_WEEK);
}
public static int getDayoFMonthFromMillis(long millis) {
Calendar calendar = Calendar.getInstance(TimeUtils.getDefaultTimeZone(), TimeUtils.getDefaultLocale());
calendar.setTimeInMillis(millis);
return calendar.get(Calendar.DAY_OF_MONTH);
}