如何使用 JAVA 日历以日期显示周数



我试图显示今天的日期和今天的日期周。

例如(2013.6.11和2013.6.17。

我假设getFromToDate在这里扮演着重要的角色。所以这里是代码。

以下是似乎与之相关的控制器页面。

@RequestMapping(value="/getFromToDate")
public void getFromToDate( 
        ModelMap model,
        @RequestParam int addMonth) throws Exception { 

    SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
    Calendar cal = Calendar.getInstance();
    Date currentDate = cal.getTime();
    cal.add(Calendar.MONTH, addMonth);
    Date toDate = cal.getTime();
    String toDt = format.format(currentDate);
    String fromDt = format.format(toDate);
    if(addMonth > 0) {
        model.addAttribute("isMinus", false);
    } else {
        model.addAttribute("isMinus", true);
    }
    model.addAttribute("fromDt", fromDt);
    model.addAttribute("toDt", toDt);
}

而下面的页面.js。(我不知道这个页面是否相关(

getFromToDate: function(searchBtnId, startDtId, endDtId, addMonth) {
    $.ajax({
        url: ctx+'/potcom/getFromToDate.json',
        type: 'post',
        data: { addMonth : addMonth },
        dataType: 'json',
        async : false,
        success: function(data) {
            if(data.fromDt != null && data.toDt != null) {
                if(data.isMinus) {
                    CommonUtil.$id(startDtId).val(data.fromDt);
                    CommonUtil.$id(endDtId).val(data.toDt);
                } else {
                    CommonUtil.$id(endDtId).val(data.fromDt);
                    CommonUtil.$id(startDtId).val(data.toDt);
                }
                CommonUtil.$id(searchBtnId).click();
            }
        },
        error: function(xhr, status, error) {
            alert(CommonUtil.Message.AJAX_ERROR_MESSAGE);
        }
    });
},

以下是 JSP 页面。

<td class="typeFD bgN">
<a href="" class="buttonA on" onclick="CommonUtil.getFromToDate('', 'startDt', 'endDt', 0); return false;">Daily</a>
<a href="" class="buttonA" onclick="CommonUtil.getFromToDate('', 'startDt', 'endDt', 1); return false;">Weekly</a>
<a href="" class="buttonA" onclick="CommonUtil.getFromToDate('', 'startDt', 'endDt', 1); return false;">Monthly</a>
</td>

当我执行上面的代码时,每周只增加了一个月。

每日20130611 20130611每周20130611 20130711每月20130611 20130711

使用 JAVA 类或其他什么,我如何从今天起一周获得日期

20130611 20130617周刊。

使用:

cal.add(Calendar.DATE, 7);

这将使日历实例增加 7 天

当你在Java 8中使用Joda-Time或java.time包时,寻找plusWeeks方法。

DateTime now = DateTime.now();
DateTime nextWeek = now.plusWeeks( 1 );