Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding seconds to a Date

Tags:

java

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?

like image 269
Helio Bentzen Avatar asked May 08 '14 17:05

Helio Bentzen


People also ask

How do you add time?

To add time, you add the hours together, then you add the minutes together. Because there are only 60 minutes in an hour, you cannot have a time whose minute value is greater than 60. In this case, subtract 60 minutes and add 1 more to the hour.

How do you add minutes to a date?

To add minutes to a date:Use the getMinutes() method to get the minutes of the specific date. Use the setMinutes() method to set the minutes for the date. The setMinutes method takes the minutes as a parameter and sets the value for the date.

What is getTime in JavaScript?

getTime() The getTime() method returns the number of milliseconds since the ECMAScript epoch. You can use this method to help assign a date and time to another Date object.


2 Answers

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);
like image 60
akhilless Avatar answered Sep 21 '22 05:09

akhilless


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.

like image 36
Basil Bourque Avatar answered Sep 20 '22 05:09

Basil Bourque