Java 8: Efficient and performant way to get Current Timestamp and compute the Timestamp difference in...
Specifying author pseudonyms in BibLaTeX
Running a subshell from the middle of the current command
Did CPM support custom hardware using device drivers?
Implicit nil checks in algorithms
Ban on all campaign finance?
Is Mortgage interest accrued after a December payment tax deductible?
Plot a function of two variables equal 0
What to do when during a meeting client's people start to (physically) fight with each other?
Does splitting a potentially monolithic application into several smaller ones help prevent bugs?
Word for a person who has no opinion about whether god exists
Checking if rendered lightning:input components are populated
Instead of Universal Basic Income, why not Universal Basic NEEDS?
My story is written in English, but is set in my home country. What language should I use for the dialogue?
Should we release the security issues we found in our product as CVE or we can just update those on weekly release notes?
Is having access to past exams cheating and, if yes, could it be proven just by a good grade?
Why are the outputs of printf and std::cout different
Why would a flight no longer considered airworthy be redirected like this?
How to deal with a cynical class?
It's a yearly task, alright
Possible Leak In Concrete
Rejected in 4th interview round citing insufficient years of experience
Cultural lunch issues
Could the Saturn V actually have launched astronauts around Venus?
2D counterpart of std::array in C++17
Java 8: Efficient and performant way to get Current Timestamp and compute the Timestamp difference in minutes
Date Format providerHave I coded this small object grouping script Pythonically?Adding minutes to a time value in RubyGet the difference between two dates, in the most convenient unitConverting UTC time to date in d3Get and use user's current location efficiently for Route CalculationConvert FoxPro/dBase DATETIME field to Win32 SYSTEMTIME structFunction which would get the next `n` slots from the current timeIterate between dates and INSERT values in a performant wayA portable cross platform C++17 method to retrieve the current date and time
$begingroup$
I have the below code to get Current Timestamp and compute the Last Timestamp's difference with Current timestamp in minutes, am wondering if this can be optimized further for production
public static Timestamp getTimestamp() {
java.util.Date date= new java.util.Date();
long time = date.getTime();
java.sql.Timestamp ts = new java.sql.Timestamp(time);
return ts;
}
public static long getLastTimestampElapse(java.sql.Timestamp oldTime){
long milliseconds1 = oldTime.getTime();
long milliseconds2 = getTimestamp().getTime();
long diff = milliseconds2 - milliseconds1;
long diffMinutes = diff / (60 * 1000);
return diffMinutes;
}
java performance datetime comparative-review
$endgroup$
add a comment |
$begingroup$
I have the below code to get Current Timestamp and compute the Last Timestamp's difference with Current timestamp in minutes, am wondering if this can be optimized further for production
public static Timestamp getTimestamp() {
java.util.Date date= new java.util.Date();
long time = date.getTime();
java.sql.Timestamp ts = new java.sql.Timestamp(time);
return ts;
}
public static long getLastTimestampElapse(java.sql.Timestamp oldTime){
long milliseconds1 = oldTime.getTime();
long milliseconds2 = getTimestamp().getTime();
long diff = milliseconds2 - milliseconds1;
long diffMinutes = diff / (60 * 1000);
return diffMinutes;
}
java performance datetime comparative-review
$endgroup$
add a comment |
$begingroup$
I have the below code to get Current Timestamp and compute the Last Timestamp's difference with Current timestamp in minutes, am wondering if this can be optimized further for production
public static Timestamp getTimestamp() {
java.util.Date date= new java.util.Date();
long time = date.getTime();
java.sql.Timestamp ts = new java.sql.Timestamp(time);
return ts;
}
public static long getLastTimestampElapse(java.sql.Timestamp oldTime){
long milliseconds1 = oldTime.getTime();
long milliseconds2 = getTimestamp().getTime();
long diff = milliseconds2 - milliseconds1;
long diffMinutes = diff / (60 * 1000);
return diffMinutes;
}
java performance datetime comparative-review
$endgroup$
I have the below code to get Current Timestamp and compute the Last Timestamp's difference with Current timestamp in minutes, am wondering if this can be optimized further for production
public static Timestamp getTimestamp() {
java.util.Date date= new java.util.Date();
long time = date.getTime();
java.sql.Timestamp ts = new java.sql.Timestamp(time);
return ts;
}
public static long getLastTimestampElapse(java.sql.Timestamp oldTime){
long milliseconds1 = oldTime.getTime();
long milliseconds2 = getTimestamp().getTime();
long diff = milliseconds2 - milliseconds1;
long diffMinutes = diff / (60 * 1000);
return diffMinutes;
}
java performance datetime comparative-review
java performance datetime comparative-review
edited 2 hours ago
RanPaul
asked 2 hours ago
RanPaulRanPaul
3902513
3902513
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
$begingroup$
Note that none of these suggestions will have any significant impact on the performance of your application overall. Don't micro-optimize performance until you have known, tested bottlenecks.
The getTimestamp()
method is noise. If all you care about is the current timestamp in milliseconds, use System.currentTimeMillis()
.
You can use a constant to store the number of milliseconds in a minute, potentially saving the multiplication. Even if the compiler optimizes the math away, it's easier to read.
A java.sql.Timestamp
is a kind of java.util.Date
, and the getTime()
method is defined there. Your method should accept a java.util.Date
to support more clients at no cost.
Your method is poorly named. Something like getMinutesSince()
would be more readable. Likewise, there are better variable names than what you've selected.
Use final
to indicate that variables won't be reassigned. That reduces the cognitive load on the reader.
You don't really need as many variables as you have. You might even be able to get away with none and still have a reasonably clear method.
If you were to use all my suggestions, your code might look more like:
private static final long MILLISECONDS_PER_MINUTE;
public static long getMinutesSince(final java.util.Date startTime) {
final long millisecondsSinceStart =
System.currentTimeMillis() - startTime.getTime();
return millisecondsSinceStart / MILLISECONDS_PER_MINUTE;
}
$endgroup$
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f215432%2fjava-8-efficient-and-performant-way-to-get-current-timestamp-and-compute-the-ti%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
Note that none of these suggestions will have any significant impact on the performance of your application overall. Don't micro-optimize performance until you have known, tested bottlenecks.
The getTimestamp()
method is noise. If all you care about is the current timestamp in milliseconds, use System.currentTimeMillis()
.
You can use a constant to store the number of milliseconds in a minute, potentially saving the multiplication. Even if the compiler optimizes the math away, it's easier to read.
A java.sql.Timestamp
is a kind of java.util.Date
, and the getTime()
method is defined there. Your method should accept a java.util.Date
to support more clients at no cost.
Your method is poorly named. Something like getMinutesSince()
would be more readable. Likewise, there are better variable names than what you've selected.
Use final
to indicate that variables won't be reassigned. That reduces the cognitive load on the reader.
You don't really need as many variables as you have. You might even be able to get away with none and still have a reasonably clear method.
If you were to use all my suggestions, your code might look more like:
private static final long MILLISECONDS_PER_MINUTE;
public static long getMinutesSince(final java.util.Date startTime) {
final long millisecondsSinceStart =
System.currentTimeMillis() - startTime.getTime();
return millisecondsSinceStart / MILLISECONDS_PER_MINUTE;
}
$endgroup$
add a comment |
$begingroup$
Note that none of these suggestions will have any significant impact on the performance of your application overall. Don't micro-optimize performance until you have known, tested bottlenecks.
The getTimestamp()
method is noise. If all you care about is the current timestamp in milliseconds, use System.currentTimeMillis()
.
You can use a constant to store the number of milliseconds in a minute, potentially saving the multiplication. Even if the compiler optimizes the math away, it's easier to read.
A java.sql.Timestamp
is a kind of java.util.Date
, and the getTime()
method is defined there. Your method should accept a java.util.Date
to support more clients at no cost.
Your method is poorly named. Something like getMinutesSince()
would be more readable. Likewise, there are better variable names than what you've selected.
Use final
to indicate that variables won't be reassigned. That reduces the cognitive load on the reader.
You don't really need as many variables as you have. You might even be able to get away with none and still have a reasonably clear method.
If you were to use all my suggestions, your code might look more like:
private static final long MILLISECONDS_PER_MINUTE;
public static long getMinutesSince(final java.util.Date startTime) {
final long millisecondsSinceStart =
System.currentTimeMillis() - startTime.getTime();
return millisecondsSinceStart / MILLISECONDS_PER_MINUTE;
}
$endgroup$
add a comment |
$begingroup$
Note that none of these suggestions will have any significant impact on the performance of your application overall. Don't micro-optimize performance until you have known, tested bottlenecks.
The getTimestamp()
method is noise. If all you care about is the current timestamp in milliseconds, use System.currentTimeMillis()
.
You can use a constant to store the number of milliseconds in a minute, potentially saving the multiplication. Even if the compiler optimizes the math away, it's easier to read.
A java.sql.Timestamp
is a kind of java.util.Date
, and the getTime()
method is defined there. Your method should accept a java.util.Date
to support more clients at no cost.
Your method is poorly named. Something like getMinutesSince()
would be more readable. Likewise, there are better variable names than what you've selected.
Use final
to indicate that variables won't be reassigned. That reduces the cognitive load on the reader.
You don't really need as many variables as you have. You might even be able to get away with none and still have a reasonably clear method.
If you were to use all my suggestions, your code might look more like:
private static final long MILLISECONDS_PER_MINUTE;
public static long getMinutesSince(final java.util.Date startTime) {
final long millisecondsSinceStart =
System.currentTimeMillis() - startTime.getTime();
return millisecondsSinceStart / MILLISECONDS_PER_MINUTE;
}
$endgroup$
Note that none of these suggestions will have any significant impact on the performance of your application overall. Don't micro-optimize performance until you have known, tested bottlenecks.
The getTimestamp()
method is noise. If all you care about is the current timestamp in milliseconds, use System.currentTimeMillis()
.
You can use a constant to store the number of milliseconds in a minute, potentially saving the multiplication. Even if the compiler optimizes the math away, it's easier to read.
A java.sql.Timestamp
is a kind of java.util.Date
, and the getTime()
method is defined there. Your method should accept a java.util.Date
to support more clients at no cost.
Your method is poorly named. Something like getMinutesSince()
would be more readable. Likewise, there are better variable names than what you've selected.
Use final
to indicate that variables won't be reassigned. That reduces the cognitive load on the reader.
You don't really need as many variables as you have. You might even be able to get away with none and still have a reasonably clear method.
If you were to use all my suggestions, your code might look more like:
private static final long MILLISECONDS_PER_MINUTE;
public static long getMinutesSince(final java.util.Date startTime) {
final long millisecondsSinceStart =
System.currentTimeMillis() - startTime.getTime();
return millisecondsSinceStart / MILLISECONDS_PER_MINUTE;
}
answered 47 mins ago
Eric SteinEric Stein
4,232613
4,232613
add a comment |
add a comment |
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f215432%2fjava-8-efficient-and-performant-way-to-get-current-timestamp-and-compute-the-ti%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown