package Test; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; public class TestTimes { public static void main(String args[]) throws ParseException { //GregorianCalendar ca = new GregorianCalendar(); //System.out.println(ca.get(GregorianCalendar.AM_PM)); Calendar curr = Calendar.getInstance(); curr.set(Calendar.DAY_OF_MONTH,curr.get(Calendar.DAY_OF_MONTH)-7); Date date = curr.getTime(); String date1 = getStrDateTimeFormat(date); System.out.println("上周一"+date1); System.out.println("++++++"+getOneWeekDates(date1)); } /** * 把日期转化为字符串格式,如:2007-01-05 * @param d * @return */ public static String getStrDateTimeFormat(Date d) { String strd = null; if (d == null) return strd; SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); strd = formatter.format(d); // 将日期时间格式化 return strd; } /** * 获得一周七天的日期 * @param date * @return * @throws ParseException */ public static String[] getOneWeekDates(String date) throws ParseException { return getOneWeekDates(reviseSundayBug(date), "yyyy-MM-dd"); } /** * 修正当前日期是周日时计算本周日期的bug * @param date * @return * @throws ParseException */ private static String reviseSundayBug(String date) throws ParseException { if (getDayOfTheWeek(date) == Calendar.SUNDAY) { Calendar c = Calendar.getInstance(); c.setTime(parseDateStr(date, "yyyy-MM-dd")); c.add(Calendar.DATE, -7); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); date = format.format(c.getTime()); } return date; } /** * 按照指定格式日期获得一周七天的日期 * @param date * @param df * @return * @throws ParseException */ public static String[] getOneWeekDates(String date, String df) throws ParseException { String[] dates = new String[7]; dates[0] = getDayOfTheWeek(date, df, Calendar.MONDAY); dates[1] = getDayOfTheWeek(date, df, Calendar.TUESDAY); dates[2] = getDayOfTheWeek(date, df, Calendar.WEDNESDAY); dates[3] = getDayOfTheWeek(date, df, Calendar.THURSDAY); dates[4] = getDayOfTheWeek(date, df, Calendar.FRIDAY); dates[5] = getDayOfTheWeek(date, df, Calendar.SATURDAY); dates[6] = getDayOfTheWeek(date, df, Calendar.SUNDAY); System.out.println("上周一"+dates[0]); System.out.println("上周日"+dates[6]); return dates; } /** * 获得指定日期所在周的周几的日期 * @param date 字符串格式的日期 * @param df 日期的格式化字符串,如yyyy-MM-dd * @param DAY_OF_WEEK 周几,如Calendar.MONDAY * @return * @throws ParseException */ public static String getDayOfTheWeek(String date, String df, int DAY_OF_WEEK) throws ParseException { Calendar c = Calendar.getInstance(); c.setTime(parseDateStr(date, df)); c.set(Calendar.DAY_OF_WEEK, DAY_OF_WEEK); // 中国习惯是周日是一个星期的最后一天 if (DAY_OF_WEEK == Calendar.SUNDAY) { c.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY); c.add(Calendar.DATE, 1); } return new SimpleDateFormat(df).format(c.getTime()); } /** * 获得当前日期是周几 * @param date * @return 周几在Calendar类中对应的静态变量值 * @throws ParseException */ public static int getDayOfTheWeek(String date) throws ParseException { Calendar c = Calendar.getInstance(); c.setTime(parseDateStr(date, "yyyy-MM-dd")); return c.get(Calendar.DAY_OF_WEEK); } /** * 解析时间日期字符串 * @param date * @param format * @return * @throws ParseException */ public static Date parseDateStr(String date, String format) throws ParseException { DateFormat df = new SimpleDateFormat(format); try { return df.parse(date); } catch (ParseException e) { e.printStackTrace(); throw e; } } }