17

I need add a certain time in seconds to my Date. For this, I'm making it:

Date startTime = dayStart(dateSelected);    
startTime.setSeconds(startTime.getSeconds()+30);

But, for this way I get this alerts:

Multiple markers at this line

- The method setSeconds(int) from the type Date is deprecated
- The method getSeconds() from the type Date is deprecated

What's the better way to don't get these deprecated alerts?

Helio Bentzen
  • 645
  • 2
  • 12
  • 24
  • 1
    do you know what deprecated means? – PKlumpp May 08 '14 at 17:31
  • From [JavaDoc](http://docs.oracle.com/javase/8/docs/api/java/util/Date.html#setSeconds-int-): **Deprecated**. As of JDK version 1.1, replaced by `Calendar.set(Calendar.SECOND, int seconds)`. – Pshemo May 08 '14 at 17:34
  • 6
    Why do questions like this get -1 so often? He is asking what a better way to do this is. That seems like a legitimate question. – Jeff Scott Brown May 08 '14 at 17:44
  • 1
    Unfortunately, some developers like to underestimate others. I know something is not necessarily wrong deprecated. Just wanted to know a more interesting way to do it. – Helio Bentzen May 08 '14 at 17:48
  • 2
    Well, the -1 is gone now. ;) – Jeff Scott Brown May 08 '14 at 17:53
  • Perhaps the -1 is because this question has been asked and answered many times before. – Basil Bourque May 09 '14 at 04:34
  • possible duplicate of [Adding n hours to a date in Java?](http://stackoverflow.com/questions/3581258/adding-n-hours-to-a-date-in-java) – Basil Bourque May 09 '14 at 04:36
  • possible duplicate of [The constructor Date(...) is deprecated. What does it mean? (Java)](http://stackoverflow.com/questions/1999766/the-constructor-date-is-deprecated-what-does-it-mean-java) – Raedwald May 10 '14 at 12:13

5 Answers5

13

JavaDoc for Date class reads

As of JDK 1.1, the Calendar class should be used to convert between dates and time fields and the DateFormat class should be used to format and parse date strings. The corresponding methods in Date are deprecated.

And setSeconds method in JavaDoc has following warning

Deprecated. As of JDK version 1.1, replaced by Calendar.set(Calendar.SECOND, int seconds).

That means you should do something like this

int numberOfseconds = 30;
Calendar calendar = Calendar.getInstance();
calendar.setTime(dateSelected);
calendar.add(Calendar.SECOND, numberOfSeconds);
akhilless
  • 3,169
  • 19
  • 24
7

tl;dr

What's the better way to don't get these deprecated alerts?

Do not use that terrible class, java.util.Date. Use its replacement, Instant.

myJavaUtilDate.toInstant().plusSeconds( 30 ) 

java.time

The modern approach uses the java.time classes defined in JSR 310 that supplanted the terrible legacy date-time classes such as Calendar and Date.

Table of date-time types in Java, both modern and legacy

Start of day

Date startTime = dayStart(dateSelected);

If you want the first moment of the day, you need a LocalDate (date) and a ZoneId (time zone).

ZoneId z = ZoneId.of( "Africa/Tunis" ) ;
LocalDate localDate = LocalDate.now( z ) ;  // Get the current date as seen in a particular time zone.

Get the start of that date. Specify a time zone to get a ZonedDateTime. Always let java.time determine the first moment of the day. Some dates in some zones do not start at 00:00:00. They might start at another time such as 01:00:00.

ZonedDateTime zdt = localDate.atStartOfDay( z ) ;

Date-time math

You can perform date-time math by calling the plus… & minus… methods.

Specify a span-of-time not attached to the timeline using either Duration or Period classes.

Duration d = Duration.ofSeconds( 30 ) ;

Add.

ZonedDateTime zdtLater = zdt.plus( d ) ;

Or combine those 2 lines into 1.

ZonedDateTime zdtLater = zdt.plusSeconds( 30 ) ;

Converting

If you need to interoperate with old code not yet updated to java.time, call new conversion methods added to the old classes.

The java.util.Date legacy class represents a moment in UTC with a resolution of milliseconds. Its replacement is java.time.Instant, also a moment in UTC but with a much finer resolution of nanoseconds.

You can extract a Instant from a ZonedDateTime, effectively adjusting from some time zone to UTC. Same moment, same point on the timeline, different wall-clock time.

Instant instant = zdtLater.toInstant() ;

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
4

The methods on Date are deprecated for good reason.
All of that functionality has been moved to the Calendar class:

    Date oldDate = new Date();
    Calendar gcal = new GregorianCalendar();
    gcal.setTime(oldDate);
    gcal.add(Calendar.SECOND, 30);
    Date newDate = gcal.getTime();
azurefrog
  • 10,785
  • 7
  • 42
  • 56
3
import java.util.Date;
import java.util.Calendar;

public class DateStuff {
    public static void main(String[] args) {
        Calendar cal = Calendar.getInstance();
        Date now = cal.getTime();

        cal.add(Calendar.SECOND, 30);
        Date later = cal.getTime();

        System.out.println("Now: " + now);
        System.out.println("Later: " + later);
    }
}
Jeff Scott Brown
  • 26,804
  • 2
  • 30
  • 47
1

Adding 30 seconds to the old java.util.Date (not recommended but sometimes useful):

Date startTime = dayStart(dateSelected);    
startTime = new Date(startTime.getTime() + 30 * 1000);
  • The `java.util` Date-Time API is outdated and error-prone. It is recommended to stop using it completely and switch to the [modern Date-Time API](https://www.oracle.com/technical-resources/articles/java/jf14-Date-Time.html), released with Java SE 8 in March 2014. – Arvind Kumar Avinash Jun 08 '21 at 18:21