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

后端 未结 8 846
旧巷少年郎
旧巷少年郎 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:26

    deprecated means that this method should't be used anymore because it can be removed from the Java language in favor of some other method (it probably won't be removed but it's not the preferable way of doing things anymore). Documentation suggests you to use Calendar.get(Calendar.SECOND) which is what you should use in your code.

    Note that from Java 8 you can use LocalDateTime#getSecond method again.

提交回复
热议问题