a、使用Calendar类
//根据日期取得星期几
public static String getWeek(Date date){
String[] weeks = {"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int week_index = cal.get(Calendar.DAY_OF_WEEK) - 1;
if(week_index<0){
week_index = 0;
}
return weeks[week_index];
}
b、使用SimpleDateFormat类
//根据日期取得星期几
public static String getWeek(Date date){
SimpleDateFormat sdf = new SimpleDateFormat("EEEE");
String week = sdf.format(date);
return week;
}
注:格式化字符串存在区分大小写
对于创建SimpleDateFormat传入的参数:EEEE代表星期,如“星期四”;MMMM代表中文月份,如“十一月”;MM代表月份,如“11”;
yyyy代表年份,如“2010”;dd代表天,如“25”
取得日期是某年的第几周
根据日期入得日期是某年的第几周。
//取得日期是某年的第几周
public static int getWeekOfYear(Date date){
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int week_of_year = cal.get(Calendar.WEEK_OF_YEAR);
return week_of_year;
}
得到某年的某个月有多少天
已知年份和月份,取得该月有多少天。
//取得某个月有多少天
public static int getDaysOfMonth(int year,int month){
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month-1);
int days_of_month = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
return days_of_month;
}
4、取得两个日期之间的相差多少天
已知两个日期,计算它们之间相差多少天。
// 取得两个日期之间的相差多少天
public static long getDaysBetween(Date date0, Date date1) {
long daysBetween = (date0.getTime() - date1.getTime() + 1000000) / 86400000;// 86400000=3600*24*1000 用立即数,减少乘法计算的开销
return daysBetween;
}
/**
* 两个时间之间的天差
* @param endDate
* @param startDate
* @return
*/
public static Integer getDateBetweenDay(Date startDate ,Date endDate) {
long nd = 1000 * 24 * 60 * 60;
// long ns = 1000;
// 获得两个时间的毫秒时间差异
long diff = endDate.getTime() - startDate.getTime();
// 计算差多少天
long day = diff / nd;
int betweenday = (int)day;
return betweenday;
}
/**
* 两个时间之间的小时差
* @param endDate
* @param startDate
* @return
*/
public static Integer getDateBetweenMinute(Date startDate ,Date endDate) {
long interval = (endDate.getTime() - startDate.getTime())/1000;
return Integer.parseInt(String.valueOf(interval));
}
/**
* 两个时间之间的分差
* @param endDate
* @param startDate
* @return
*/
public static Integer getDateMinutesBetweenMinute(Date startDate ,Date endDate) {
long interval = (endDate.getTime() - startDate.getTime())/(1000* 60);
return Integer.parseInt(String.valueOf(interval));
}
/**
* 两个时间之间的小时分钟差
* @param endDate
* @param startDate
* @return
*/
public static String getDatePoor(Date startDate ,Date endDate) {
long nd = 1000 * 24 * 60 * 60;
long nh = 1000 * 60 * 60;
long nm = 1000 * 60;
// long ns = 1000;
// 获得两个时间的毫秒时间差异
long diff = endDate.getTime() - startDate.getTime();
// 计算差多少天
long day = diff / nd;
// 计算差多少小时
long hour = diff % nd / nh;
// 计算差多少分钟
long min = diff % nd % nh / nm;
// 计算差多少秒//输出结果
// long sec = diff % nd % nh % nm / ns;
// return day + "天" + hour + "小时" + min + "分钟";
return hour + "小时" + min + "分钟";
}
/**
* 获取一年中第几天 date类型YYmmdd
*
* @param date
* @return
*/
public static int getTheNthDayOfYear(String date) {
int nthDay = 0;
int year = Integer.parseInt(date.substring(0, 4));
int month = Integer.parseInt(date.substring(4, 6));
int day = Integer.parseInt(date.substring(6, 8));
for (int i = 1; i < month; i++) {
switch (i) {
case 1:
nthDay += 31;
break;
case 2:
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
nthDay += 29;
} else {
nthDay += 28;
}
break;
case 3:
nthDay += 31;
break;
case 4:
nthDay += 30;
break;
case 5:
nthDay += 31;
break;
case 6:
nthDay += 30;
break;
case 7:
nthDay += 31;
break;
case 8:
nthDay += 31;
break;
case 9:
nthDay += 30;
break;
case 10:
nthDay += 31;
break;
case 11:
nthDay += 30;
break;
case 12:
nthDay += 31;
break;
default:break;
}
}
nthDay += day;
return nthDay;
}
458




被折叠的 条评论
为什么被折叠?



