How do I increment a java.sql.Timestamp by 14 days?

后端 未结 4 2061
借酒劲吻你
借酒劲吻你 2020-12-09 08:47

I have an app that takes a Timestamp as a boundary for the start date and end date of a sql selection, I want to populate a hashmap with weeks this year since the first mon

4条回答
  •  不知归路
    2020-12-09 09:15

    java.sql.Timestamp ts = ...
    Calendar cal = Calendar.getInstance();
    cal.setTime(ts);
    cal.add(Calendar.DAY_OF_WEEK, 14);
    ts.setTime(cal.getTime().getTime()); // or
    ts = new Timestamp(cal.getTime().getTime());
    

    This will correctly cater for daylight-time transitions in your default Timezone. You can tell the Calendar class to use a different Timezone if need be.

提交回复
热议问题