Adding/Subtracting 5 seconds from Java Date - Showing deprected warning

后端 未结 8 843
旧巷少年郎
旧巷少年郎 2021-01-23 01:43

I want to add 5 seconds to current time

    Date date = new Date();

    date.setSeconds(date.getSeconds()+ 5);
    System.out.println(\"old Value is: \"+date);         


        
8条回答
  •  北恋
    北恋 (楼主)
    2021-01-23 02:12

    An easy way is to use apache DateUtils.

    import org.apache.commons.lang.time.DateUtils;
    
    DateUtils.addSeconds(now, 5); // Add 5 seconds.
    DateUtils.addSeconds(now, -5); // Subtracting 5 seconds
    

    You can also add/subtract minutes, hours, and so on...

提交回复
热议问题