首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

取当前日期(天)的当前周的首先天和最后一天日期

2013-11-09 
取当前日期(天)的当前周的第一天和最后一天日期? /**???? * 解析日期字符串至日期类型内容???? * @param d

取当前日期(天)的当前周的第一天和最后一天日期

? /**
???? * 解析日期字符串至日期类型内容
???? * @param date 日期字符串
???? * @param format 与日期字符串格式匹配的格式
???? * @return 解析后返回的日期
???? */
??? public static java.util.Date parseDate(String date, String format) {
??????? try {
??????????? SimpleDateFormat formatter;
??????????? if (null == format)
??????????????? throw new IllegalArgumentException("错误的日期格式");
??????????? formatter = new SimpleDateFormat(format);
??????????? ParsePosition pos = new ParsePosition(0);
??????????? return formatter.parse(date, pos);
??????? } catch (Exception e) {
??????????? throw new IllegalArgumentException("错误的日期:" + date + " " + e);
??????? }
??? }

?

?

?public static final String getDateTime(String aMask, Date aDate) {
??????? SimpleDateFormat df = null;
??????? String returnValue = "";

??????? if (aDate == null) {
??????????? log.error("aDate is null!");
??????? } else {
??????????? df = new SimpleDateFormat(aMask);
??????????? returnValue = df.format(aDate);
??????? }

??????? return (returnValue);
??? }

?

?

?

?

/**
???? * 取当前日期(天)的当前周的第一天和最后一天日期
???? *
???? * @param date
???? * @param format
???? * @return
???? */
??? public static String[] getWeek(String date, String format) {
??? ?String[] result = new String[2];
??? ?Calendar calendar = Calendar.getInstance();
??? ?calendar.setTime(parseDate(date, format));
??? ?int dayOfWeek = calendar.get(calendar.DAY_OF_WEEK);
??? ?calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - dayOfWeek + 1);
??? ?result[0] = getDateTime(format, calendar.getTime());
??? ?calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) + 6);
??? ?result[1] = getDateTime(format, calendar.getTime());
??? ?
??? ?return result;
??? }
???

?

//测试类
??? public static void main(String[] args) throws Exception{
??? ?String[] a = getWeek("2010-07-01", "yyyy-MM-dd");
??? ?System.out.println(a[0] + "??? " + a[1]); //得出当前日期,当前周的第一天和最后一天
??? }

热点排行