java 日期转换星期_[转]java根据日期获得星期常用方法

/**

* 根据日期获得星期

* @param date

* @return

*/

public static String getWeekOfDate(Date date) {

String[] weekDaysName = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };

String[] weekDaysCode = { "0", "1", "2", "3", "4", "5", "6" };

Calendar calendar = Calendar.getInstance();

calendar.setTime(date);

int intWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;

return weekDaysCode[intWeek];

}

/**

* 获得周一的日期

*

* @param date

* @return

*/

public static String getMonday(Date date) {

Calendar calendar = Calendar.getInstance();

calendar.setTime(date);

calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);

return dateFormat.format(calendar.getTime());

}

/**

* 获得周三的日期

*

* @param date

* @return

*/

public static String getWednesday(Date date) {

Calendar calendar = Calendar.getInstance();

calendar.setTime(date);

calendar.set(Calendar.DAY_OF_WEEK, Calendar.WEDNESDAY);

return dateFormat.format(calendar.getTime());

}

/**

* 获得周五的日期

*

* @param date

* @return

*/

public static String getFriday(Date date) {

Calendar calendar = Calendar.getInstance();

calendar.setTime(date);

calendar.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);

return dateFormat.format(calendar.getTime());

}

/**

* 当前日期前几天或者后几天的日期

* @param n

* @return

*/

public static String afterNDay(int n) {

Calendar calendar = Calendar.getInstance();

calendar.setTime(new Date());

calendar.add(Calendar.DATE, n);

Date date = calendar.getTime();

String s = dateFormat.format(date);

return s;

}

/**

* 判断两个日期是否在同一周

*

* @param date1

* @param date2

* @return

*/

public static boolean isSameWeekDates(Date date1, Date date2) {

Calendar cal1 = Calendar.getInstance();

Calendar cal2 = Calendar.getInstance();

cal1.setTime(date1);

cal2.setTime(date2);

int subYear = cal1.get(Calendar.YEAR) - cal2.get(Calendar.YEAR);

if (0 == subYear) {

if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2

.get(Calendar.WEEK_OF_YEAR))

return true;

} else if (1 == subYear && 11 == cal2.get(Calendar.MONTH)) {

// 如果12月的最后一周横跨来年第一周的话则最后一周即算做来年的第一周

if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2

.get(Calendar.WEEK_OF_YEAR))

return true;

} else if (-1 == subYear && 11 == cal1.get(Calendar.MONTH)) {

if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2

.get(Calendar.WEEK_OF_YEAR))

return true;

}

return false;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值