public static String getWeekOfDate(Date dt)
{
String[] weekDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
Calendar cal = Calendar.getInstance();
cal.setTime(dt);
int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
if (w < 0)
w = 0;
return weekDays[w];
}
public static String getStringDayOfWeek(Date date) {
String result = "";
if (null != date) {
SimpleDateFormat formatter4 = new SimpleDateFormat("E");
result = formatter4.format(date);
}
return result;
}
{
String[] weekDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
Calendar cal = Calendar.getInstance();
cal.setTime(dt);
int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
if (w < 0)
w = 0;
return weekDays[w];
}
public static String getStringDayOfWeek(Date date) {
String result = "";
if (null != date) {
SimpleDateFormat formatter4 = new SimpleDateFormat("E");
result = formatter4.format(date);
}
return result;
}
本文提供两种方法将Java中的日期转换为对应的星期名称。一种是通过Calendar类获取日期所对应的星期数,再映射到具体的星期名称;另一种是使用SimpleDateFormat直接格式化日期得到星期的英文缩写。
1万+

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



