I am looking for the best way to add milliseconds to a Java Date when milliseconds is stored as a 'long'. Java calendar has an add function, but it only takes an 'int' as the amount.
This is one solution I am proposing...
Calendar now = Calendar.getInstance();
Calendar timeout = Calendar.getInstance();
timeout.setTime(token.getCreatedOn());
timeout.setTimeInMillis(timeout.getTimeInMillis() + token.getExpiresIn());
Any other suggestions?
Use the timedelta() class from the datetime module to add milliseconds to datetime, e.g. result = dt + timedelta(milliseconds=300) . The timedelta class can be passed a milliseconds argument and adds the specified number of milliseconds to the datetime.
DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy"); long tempo1=Long. parseLong(x); System. out. println(tempo1); // output is 86073200000 instead of the whole thing long milliSeconds=1346482800000L; Calendar calendar = Calendar.
Millisecond (MS) and microsecond (US) values in a conversion from string to TIMESTAMP are used as part of the seconds after the decimal point. For example TO_TIMESTAMP('12:3', 'SS:MS') is not 3 milliseconds, but 300, because the conversion counts it as 12 + 0.3 seconds.
Your solution looks nearly fine to me, actually. I originally posted an answer going via Date
, when I hadn't taken getTimeInMillis
and setTimeInMillis
into account properly.
However, you're calling setTime
and then setTimeInMillis
which seems somewhat redundant to me. Your code looks equivalent to this:
Calendar timeout = Calendar.getInstance(); timeout.setTimeInMillis(token.getCreatedOn().getTime() + token.getExpiresIn());
A generally nicer alternative would be to use Joda Time though :) It's generally a much nicer date/time API.
You can also create a date with the current local date plus the number of miliseconds you need to add as Expire time
import java.util.Date; long expiremilis = 60000l; // 1 minute // Expires in one minute from now Date expireDate = new Date(System.currentTimeMillis() + expiremilis);
Or the same with a calendar
long expiremilis = 60000l; // 1 minute Calendar expireDate= Calendar.getInstance(); // Expires on one minute from now expireDate.setTimeInMillis(System.currentTimeMillis() + expiremilis);
If you use an existing Date object you can do:
import java.util.Date; long expiremilis = 60000l; // 1 minute // Expires on one minute from the date object date Date expireDate = new Date(myDate.getTime() + expiremilis);
And with an existing Calendar Object
long expiremilis = 60000l; // 1 minute Calendar expireDate= Calendar.getInstance(); // Expires on one minute from the calendar date expireDate.setTimeInMillis(myCalendar.getTimeInMillis() + expiremilis);
The java.util
Date-Time API and their formatting API, SimpleDateFormat
are outdated and error-prone. It is recommended to stop using them completely and switch to the modern Date-Time API*.
Demo using java.time
, the modern API:
import java.time.Instant;
public class Main {
public static void main(String[] args) {
Instant createdOn = Instant.ofEpochMilli(1_622_800_000_000L);
System.out.println(createdOn);
Instant timeout = createdOn.plusMillis(50_000L);
System.out.println(timeout);
}
}
See this code run live at IdeOne.com.
Output:
2021-06-04T09:46:40Z
2021-06-04T09:47:30Z
An Instant
represents an instantaneous point on the timeline in UTC. The Z
in the output is the timezone designator for a zero-timezone offset. It stands for Zulu and specifies the Etc/UTC
timezone (which has the timezone offset of +00:00
hours).
So, the solution to your problem would be:
Instant createdOn = token.getCreatedOn().toInstant();
Instant timeout = createdOn.plusMillis(token.getExpiresIn());
Check documentation of Date#toInstant
to learn more about it.
Learn more about java.time
, the modern Date-Time API* from Trail: Date Time.
* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.
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