Checking whether a timestamp is 10 minutes old
up vote
4
down vote
favorite
I have a timestamp (searchTimestamp
) which I need to check to see whether it is less than 10 minutes old or not:
long currentTimestamp = System.currentTimeMillis();
long searchTimestamp = getTheTimestampFromURL();// this also gives me back timestamp in 13 digit (1425506040493)
long difference = Math.abs(currentTimestamp - searchTimestamp);
System.out.println(difference);
if (difference > 10 * 60 * 1000) {
System.out.println("timestamp is greater than 5 minutes old");
}
I have got the above code which is working fine. Is this the right way to do this or is there any better way?
getTheTimestampFromURL
will always be older than the current timestamp in milliseconds.
java datetime
add a comment |
up vote
4
down vote
favorite
I have a timestamp (searchTimestamp
) which I need to check to see whether it is less than 10 minutes old or not:
long currentTimestamp = System.currentTimeMillis();
long searchTimestamp = getTheTimestampFromURL();// this also gives me back timestamp in 13 digit (1425506040493)
long difference = Math.abs(currentTimestamp - searchTimestamp);
System.out.println(difference);
if (difference > 10 * 60 * 1000) {
System.out.println("timestamp is greater than 5 minutes old");
}
I have got the above code which is working fine. Is this the right way to do this or is there any better way?
getTheTimestampFromURL
will always be older than the current timestamp in milliseconds.
java datetime
add a comment |
up vote
4
down vote
favorite
up vote
4
down vote
favorite
I have a timestamp (searchTimestamp
) which I need to check to see whether it is less than 10 minutes old or not:
long currentTimestamp = System.currentTimeMillis();
long searchTimestamp = getTheTimestampFromURL();// this also gives me back timestamp in 13 digit (1425506040493)
long difference = Math.abs(currentTimestamp - searchTimestamp);
System.out.println(difference);
if (difference > 10 * 60 * 1000) {
System.out.println("timestamp is greater than 5 minutes old");
}
I have got the above code which is working fine. Is this the right way to do this or is there any better way?
getTheTimestampFromURL
will always be older than the current timestamp in milliseconds.
java datetime
I have a timestamp (searchTimestamp
) which I need to check to see whether it is less than 10 minutes old or not:
long currentTimestamp = System.currentTimeMillis();
long searchTimestamp = getTheTimestampFromURL();// this also gives me back timestamp in 13 digit (1425506040493)
long difference = Math.abs(currentTimestamp - searchTimestamp);
System.out.println(difference);
if (difference > 10 * 60 * 1000) {
System.out.println("timestamp is greater than 5 minutes old");
}
I have got the above code which is working fine. Is this the right way to do this or is there any better way?
getTheTimestampFromURL
will always be older than the current timestamp in milliseconds.
java datetime
java datetime
edited Mar 5 '15 at 18:14
200_success
127k15148411
127k15148411
asked Mar 4 '15 at 23:58
david
44252137
44252137
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
up vote
11
down vote
accepted
Your description says that you want to check whether the searchTimestamp
is less than 10 minutes old. Your code does something different, though.
Your code checks whether there's less than 10 minutes between the times (the difference is <= 10 minutes .... If the searchTimestamp is 2 minutes in the future, it will pass the test, if it is 9 minutes in the future, it will pass the test, and if it is 11 minutes in the future, it will fail the test.
Of interest, the math you use does a 10 minute check, but the message says "greater than 5 minutes old."
Your message should say: "timestamp is older than 10 minutes, or more than 10 minutes in the future"
So, changing your code to be what I think it should be, is a lot simpler than you would think.
What you want is for the searchTimestamp
to have happened sometime after 10 minutes ago.... this is the way to do it:
private static final int TEN_MINUTES = 10 * 60 * 1000;
then, in your method:
long tenAgo = System.currentTimeMillis() - TEN_MINUTES;
if (searchTimestamp < tenAgo) {
System.out.println("searchTimestamp is older than 10 minutes");
}
add a comment |
up vote
4
down vote
joda-time is a very nice library that can overcome some of the shortfalls in the core Java Date/Time classes. For example:
DateTime now = new DateTime();
DateTime fromString = DateTimeFormat.forPattern("yyyyMMdd").parseDateTime(input);
DateTime fromDate = new DateTime(timeInMillis);
DateTime then = fromDate;
Interval interval = new Interval(then, now);
if (interval.toDuration().getStandardMinutes() > 10) {
...
}
I wouldn't use Math.abs(...) to bypass not knowing which way around to compare your timestamps. That's just lazy.
Also, 10, 60, etc. are magic numbers relating to time. joda-time has DateTimeConstants.MILLIS_PER_MINUTE
if you must have the number, but for manipulating DateTimes you should be using functions rather than directly manipulating the millis. Core Java, you can use TimeUnit.MINUTES.toMillis(10)
.
add a comment |
up vote
-1
down vote
To check if date is today, use Android utils library
DateUtils.isToday(long timeInMilliseconds)
This utils class also offers human readable strings for relative times. For example,
DateUtils.getRelativeTimeSpanString(long timeInMilliseconds) -> "42
minutes ago"
The are several parameters you can play with to define how precise the time span should be
New contributor
2
Welcome on Code Review. The OP never talk about Android, so, I'm not sure your response could be helpful. (especially considering the posting date)
– Calak
2 days ago
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
11
down vote
accepted
Your description says that you want to check whether the searchTimestamp
is less than 10 minutes old. Your code does something different, though.
Your code checks whether there's less than 10 minutes between the times (the difference is <= 10 minutes .... If the searchTimestamp is 2 minutes in the future, it will pass the test, if it is 9 minutes in the future, it will pass the test, and if it is 11 minutes in the future, it will fail the test.
Of interest, the math you use does a 10 minute check, but the message says "greater than 5 minutes old."
Your message should say: "timestamp is older than 10 minutes, or more than 10 minutes in the future"
So, changing your code to be what I think it should be, is a lot simpler than you would think.
What you want is for the searchTimestamp
to have happened sometime after 10 minutes ago.... this is the way to do it:
private static final int TEN_MINUTES = 10 * 60 * 1000;
then, in your method:
long tenAgo = System.currentTimeMillis() - TEN_MINUTES;
if (searchTimestamp < tenAgo) {
System.out.println("searchTimestamp is older than 10 minutes");
}
add a comment |
up vote
11
down vote
accepted
Your description says that you want to check whether the searchTimestamp
is less than 10 minutes old. Your code does something different, though.
Your code checks whether there's less than 10 minutes between the times (the difference is <= 10 minutes .... If the searchTimestamp is 2 minutes in the future, it will pass the test, if it is 9 minutes in the future, it will pass the test, and if it is 11 minutes in the future, it will fail the test.
Of interest, the math you use does a 10 minute check, but the message says "greater than 5 minutes old."
Your message should say: "timestamp is older than 10 minutes, or more than 10 minutes in the future"
So, changing your code to be what I think it should be, is a lot simpler than you would think.
What you want is for the searchTimestamp
to have happened sometime after 10 minutes ago.... this is the way to do it:
private static final int TEN_MINUTES = 10 * 60 * 1000;
then, in your method:
long tenAgo = System.currentTimeMillis() - TEN_MINUTES;
if (searchTimestamp < tenAgo) {
System.out.println("searchTimestamp is older than 10 minutes");
}
add a comment |
up vote
11
down vote
accepted
up vote
11
down vote
accepted
Your description says that you want to check whether the searchTimestamp
is less than 10 minutes old. Your code does something different, though.
Your code checks whether there's less than 10 minutes between the times (the difference is <= 10 minutes .... If the searchTimestamp is 2 minutes in the future, it will pass the test, if it is 9 minutes in the future, it will pass the test, and if it is 11 minutes in the future, it will fail the test.
Of interest, the math you use does a 10 minute check, but the message says "greater than 5 minutes old."
Your message should say: "timestamp is older than 10 minutes, or more than 10 minutes in the future"
So, changing your code to be what I think it should be, is a lot simpler than you would think.
What you want is for the searchTimestamp
to have happened sometime after 10 minutes ago.... this is the way to do it:
private static final int TEN_MINUTES = 10 * 60 * 1000;
then, in your method:
long tenAgo = System.currentTimeMillis() - TEN_MINUTES;
if (searchTimestamp < tenAgo) {
System.out.println("searchTimestamp is older than 10 minutes");
}
Your description says that you want to check whether the searchTimestamp
is less than 10 minutes old. Your code does something different, though.
Your code checks whether there's less than 10 minutes between the times (the difference is <= 10 minutes .... If the searchTimestamp is 2 minutes in the future, it will pass the test, if it is 9 minutes in the future, it will pass the test, and if it is 11 minutes in the future, it will fail the test.
Of interest, the math you use does a 10 minute check, but the message says "greater than 5 minutes old."
Your message should say: "timestamp is older than 10 minutes, or more than 10 minutes in the future"
So, changing your code to be what I think it should be, is a lot simpler than you would think.
What you want is for the searchTimestamp
to have happened sometime after 10 minutes ago.... this is the way to do it:
private static final int TEN_MINUTES = 10 * 60 * 1000;
then, in your method:
long tenAgo = System.currentTimeMillis() - TEN_MINUTES;
if (searchTimestamp < tenAgo) {
System.out.println("searchTimestamp is older than 10 minutes");
}
edited Nov 9 at 23:10
answered Mar 5 '15 at 1:28
rolfl♦
90.6k13190393
90.6k13190393
add a comment |
add a comment |
up vote
4
down vote
joda-time is a very nice library that can overcome some of the shortfalls in the core Java Date/Time classes. For example:
DateTime now = new DateTime();
DateTime fromString = DateTimeFormat.forPattern("yyyyMMdd").parseDateTime(input);
DateTime fromDate = new DateTime(timeInMillis);
DateTime then = fromDate;
Interval interval = new Interval(then, now);
if (interval.toDuration().getStandardMinutes() > 10) {
...
}
I wouldn't use Math.abs(...) to bypass not knowing which way around to compare your timestamps. That's just lazy.
Also, 10, 60, etc. are magic numbers relating to time. joda-time has DateTimeConstants.MILLIS_PER_MINUTE
if you must have the number, but for manipulating DateTimes you should be using functions rather than directly manipulating the millis. Core Java, you can use TimeUnit.MINUTES.toMillis(10)
.
add a comment |
up vote
4
down vote
joda-time is a very nice library that can overcome some of the shortfalls in the core Java Date/Time classes. For example:
DateTime now = new DateTime();
DateTime fromString = DateTimeFormat.forPattern("yyyyMMdd").parseDateTime(input);
DateTime fromDate = new DateTime(timeInMillis);
DateTime then = fromDate;
Interval interval = new Interval(then, now);
if (interval.toDuration().getStandardMinutes() > 10) {
...
}
I wouldn't use Math.abs(...) to bypass not knowing which way around to compare your timestamps. That's just lazy.
Also, 10, 60, etc. are magic numbers relating to time. joda-time has DateTimeConstants.MILLIS_PER_MINUTE
if you must have the number, but for manipulating DateTimes you should be using functions rather than directly manipulating the millis. Core Java, you can use TimeUnit.MINUTES.toMillis(10)
.
add a comment |
up vote
4
down vote
up vote
4
down vote
joda-time is a very nice library that can overcome some of the shortfalls in the core Java Date/Time classes. For example:
DateTime now = new DateTime();
DateTime fromString = DateTimeFormat.forPattern("yyyyMMdd").parseDateTime(input);
DateTime fromDate = new DateTime(timeInMillis);
DateTime then = fromDate;
Interval interval = new Interval(then, now);
if (interval.toDuration().getStandardMinutes() > 10) {
...
}
I wouldn't use Math.abs(...) to bypass not knowing which way around to compare your timestamps. That's just lazy.
Also, 10, 60, etc. are magic numbers relating to time. joda-time has DateTimeConstants.MILLIS_PER_MINUTE
if you must have the number, but for manipulating DateTimes you should be using functions rather than directly manipulating the millis. Core Java, you can use TimeUnit.MINUTES.toMillis(10)
.
joda-time is a very nice library that can overcome some of the shortfalls in the core Java Date/Time classes. For example:
DateTime now = new DateTime();
DateTime fromString = DateTimeFormat.forPattern("yyyyMMdd").parseDateTime(input);
DateTime fromDate = new DateTime(timeInMillis);
DateTime then = fromDate;
Interval interval = new Interval(then, now);
if (interval.toDuration().getStandardMinutes() > 10) {
...
}
I wouldn't use Math.abs(...) to bypass not knowing which way around to compare your timestamps. That's just lazy.
Also, 10, 60, etc. are magic numbers relating to time. joda-time has DateTimeConstants.MILLIS_PER_MINUTE
if you must have the number, but for manipulating DateTimes you should be using functions rather than directly manipulating the millis. Core Java, you can use TimeUnit.MINUTES.toMillis(10)
.
answered Mar 5 '15 at 11:28
Danikov
51725
51725
add a comment |
add a comment |
up vote
-1
down vote
To check if date is today, use Android utils library
DateUtils.isToday(long timeInMilliseconds)
This utils class also offers human readable strings for relative times. For example,
DateUtils.getRelativeTimeSpanString(long timeInMilliseconds) -> "42
minutes ago"
The are several parameters you can play with to define how precise the time span should be
New contributor
2
Welcome on Code Review. The OP never talk about Android, so, I'm not sure your response could be helpful. (especially considering the posting date)
– Calak
2 days ago
add a comment |
up vote
-1
down vote
To check if date is today, use Android utils library
DateUtils.isToday(long timeInMilliseconds)
This utils class also offers human readable strings for relative times. For example,
DateUtils.getRelativeTimeSpanString(long timeInMilliseconds) -> "42
minutes ago"
The are several parameters you can play with to define how precise the time span should be
New contributor
2
Welcome on Code Review. The OP never talk about Android, so, I'm not sure your response could be helpful. (especially considering the posting date)
– Calak
2 days ago
add a comment |
up vote
-1
down vote
up vote
-1
down vote
To check if date is today, use Android utils library
DateUtils.isToday(long timeInMilliseconds)
This utils class also offers human readable strings for relative times. For example,
DateUtils.getRelativeTimeSpanString(long timeInMilliseconds) -> "42
minutes ago"
The are several parameters you can play with to define how precise the time span should be
New contributor
To check if date is today, use Android utils library
DateUtils.isToday(long timeInMilliseconds)
This utils class also offers human readable strings for relative times. For example,
DateUtils.getRelativeTimeSpanString(long timeInMilliseconds) -> "42
minutes ago"
The are several parameters you can play with to define how precise the time span should be
New contributor
New contributor
answered 2 days ago
Abhay Pratap
1
1
New contributor
New contributor
2
Welcome on Code Review. The OP never talk about Android, so, I'm not sure your response could be helpful. (especially considering the posting date)
– Calak
2 days ago
add a comment |
2
Welcome on Code Review. The OP never talk about Android, so, I'm not sure your response could be helpful. (especially considering the posting date)
– Calak
2 days ago
2
2
Welcome on Code Review. The OP never talk about Android, so, I'm not sure your response could be helpful. (especially considering the posting date)
– Calak
2 days ago
Welcome on Code Review. The OP never talk about Android, so, I'm not sure your response could be helpful. (especially considering the posting date)
– Calak
2 days ago
add a comment |
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%2f83244%2fchecking-whether-a-timestamp-is-10-minutes-old%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