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?
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.
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.
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.
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);
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 )
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
.
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 ) ;
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 ) ;
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() ;
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With