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

localdatetime to date
java date add seconds
java date add minutes
java localdatetime subtract seconds
java calendar subtract 1 hour
java util date
how to subtract seconds from timestamp in java
date now 5 seconds

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);
    System.out.println("New Value is: "+ date);

It generates the correct output exactly what I needed as:

Old Value is: Thu Apr 17 14:10:33 PKT 2014
New Value is: Thu Apr 17 14:10:38 PKT 2014

but it gives me a warning error message as

Multiple markers at this line
    - The method getSeconds() from the type Date is deprecated
    - The method setSeconds(int) from the type Date is 
     deprecated

What this means. Is it safe to ignore this warning? if not then how to handle it?

You can try this. Calendar is the best solution you are looking at.

Date date=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
                                             .parse("2014-04-17 14:53:25");
Calendar calendar=Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.SECOND,(calendar.get(Calendar.SECOND)-5));

System.out.println(calendar.getTime());

Out put:

Thu Apr 17 14:53:20 IST 2014

All about java.util.Date, How do you minus days from current date in Java? I'm code reviewing a change one of my co-workers just did, and he added a bunch of calls to Date.toMonth(), Date.toYear() and other deprecated Date methods. All these methods were deprecated in JDK 1.1, but he insists that it's ok to use them because they haven't gone away yet (we're using JDK 1.5) and I'm saying they might go away any day now

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.

Subtract days from current date using Calendar.DATE in Java, object to represent time milliseconds after January 1, 1970 00:00:00 GMT. Opposite of adding seconds to a java date, this example shows how to subtract seconds from a date using java's Calendar.add, java 8 date time api, joda’s DateTime.minusSeconds and apache commons DateUtils.addSeconds. In each of the examples below, we will set a date that represents new years day or January 1st.

you can use date.setTime(date.getTime() + 5000)

Add or Subtract Number of Days to a Date Calculator, Note: In 2.4.0, the globally exported moment object was deprecated. If you don'​t specify noGlobal then the globally exported moment will print a deprecation warning. For Typescript 1.x try adding "allowSyntheticDefaultImports": true in 2013-02-08 09 # A calendar date part and hour time part 2013-W06-5 09 # A week  @Deprecated or java.lang.Deprecated annotation – Introduced in Java 5, or JavaSE 1.5, @Deprecated annotation when applied on a program element gives a compiler warning when that element is used while coding. This compiler warning effectively dissuades or discourages a developer from using deprecated program elements which have been marked for

showing deprecated warning that means there is some better way available in java to do so.

Date setTime() method in Java with Examples, This means that operations like add, subtract, or set change the original moment object. To avoid situations like that, clone the moment before performing date math: In addition, the ECMA Script 5 Specification makes an unusual assertion There are several places where Moment.js displays deprecation warnings  Stop thinking of date-time values as strings, that will drive you nuts. We work with date-time objects in our code; we exchange data with users or other apps using a String representation of that date-time object. In Java 8 and later, use the java.time framework. See Tutorial. You want only a date, without time-of-day, so we can use the

Java 8 has completly different time-api and makes handling dates a lot easier. Here's an article

With Java Versions 5-7 (everything below should not be used IMHO) you should use Calendar (as Petr suggested) If you are allowed to use third-party APIs, you should definitly take a look at JodaTime

Docs, showing deprecated warning that means there is some better way available in java to do so. addSeconds(now, -5); // Subtracting 5 seconds. You can also  Now we will see how to subtract minutes from current date time in java. How to subtract minutes from current time in java / How to subtract minutes from time in java. Lets see an example java program which will subtract minutes from java date time; How to subtract minutes from current time in java

Guides, The corresponding methods in Date are deprecated. For example, the last minute of the year 1995 was 61 seconds long, thanks to an added leap second. Subtract months from current date using Calendar.add method: 35. Add seconds to current date using Calendar.add method: 36. Subtract seconds from current time using Calendar.add method: 37. Add week to current date using Calendar.add method: 38. Add hours, minutes or seconds to a date: 39. Subtract week from current date: 40. Add year to

Date Add Seconds Java, Y: Deprecation Warning: Future versions of Snowflake might automatically interpret stringified integer values as seconds, rather than as milliseconds, microseconds, or nanoseconds. Timestamp is one of a few legacy date-time objects in Java. Includes DATEADD(): Add or subtract from a date column in Snowflake. This method has been deprecated to maintain API consistency. All newInstance methods have been replaced with corresponding newFactory methods. The replacement XMLEventFactory.newFactory(java.lang.String, java.lang.ClassLoader) method defines no changes in behavior.

Date (Java Platform SE 8 ), ways to add or subtract a given number of hours from a date in Java. Java date-time related classes, and then we'll showcase a few  Add or subtract days to current date using Java Calendar: 29. Subtract days from current date using Calendar.add method: 30. Add hours to current date using Calendar.add method: 31. Subtract hours from current date using Calendar.add method: 32. Add minutes to current date using Calendar.add method: 33. Subtract minutes from current date using

Comments
  • When are API methods marked "deprecated" actually going to go away? may help you.
  • Andy suggested date.setTime(date.getTime() + 5000) and it works with no warning messages. Actually I want to work with Date object. Is it ok to use date.setTime(date.getTime() + 5000) instead for going to Calendar object ?
  • In this simple case it is, however in case of some more advanced date arithmetic it might be necessary to use Calendar class.
  • Also by using Calendar you will get your code more readable because you are not adding some magical numbers to your date but you explicitly say what is being added to what.
  • Actually I will need to store the date in XML file in xml datetime format by yyyy-MM-dd'T'HH:mm:ss. Which I am currently performing SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); How to do this for calendar. And beside xml format, I will also need date comparison operations on date. Does the Calendar is still best option for me?
  • a)You can do it the same way via Calendar#getTime(). b) mkyong.com/java/how-to-compare-dates-in-java