Java获得当前日期是星期几的代码
这段代码没有根据基姆拉尔森公式进行计算,而是直接使用的java内置的日期库实现
代码转自:http://blog.duplicatedcode.com/
//从 yyyyMMdd 返回 星期几 public static String getDayAndWeek(String day){ if(day.length()<8){ return day; } Calendar c = new GregorianCalendar(); DateFormatSymbols dfs = new DateFormatSymbols(); String[] weeks = dfs.getWeekdays(); int year = Integer.parseInt(day.substring(0,4)); int month =Integer.parseInt( day.substring(4,6)); int day_ = Integer.parseInt( day.substring(6,8)); c.set(year, month-1, day_); //Calendar的月从0-11,所以4月是3. c.get(Calendar.DAY_OF_WEEK); String week = weeks[c.get(Calendar.DAY_OF_WEEK)]; week = "星期"+week.substring(2); day = day_+ "("+week+")"; return day; }