Adding seconds to a Date

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);

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);
    }
}

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();

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?

  • Java SE 8, Java SE 9, Java SE 10, Java SE 11, and later - Part of the standard Java API with a bundled implementation.
    • Java 9 adds some minor features and fixes.
  • Java SE 6 and Java SE 7
    • Most of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
  • Android
    • Later versions of Android bundle implementations of the java.time classes.
    • For earlier Android (<26), the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….

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.

Tags:

Java