cgray1223
asked on
Java Timestamp Comparison
Hello,
I have a Timestamp being passed from an external source to my application in the 2011-01-23-12.31.45 format. I need to compare it to the current system timestamp an make sure its less than 2 minutes difference. Any ideas on how to accomplish this?
I have a Timestamp being passed from an external source to my application in the 2011-01-23-12.31.45 format. I need to compare it to the current system timestamp an make sure its less than 2 minutes difference. Any ideas on how to accomplish this?
You can use SImpleDateFormat and getTime() method which returns the time in milliseconds
SimplDateFormat f = new SimpleSDateFormat("yyyy-mm
Date d1 = f.parse(s1, new ParsePosition(0));
Date d2 = f.parse(s2, new ParsePosition(0));
if((d2.getTime() - d1.getTime()) > 120000) ....
;
With curret time it would be like that:
SimplDateFormat f = new SimpleSDateFormat("yyyy-mm
Date d1 = f.parse(s1, new ParsePosition(0));
Date d2 = new Date();
if((d2.getTime() - d1.getTime()) > 120000) ....
ASKER
@Yan, what is s1 and s2 in your example?
String s1 = "2011-01-23-12.31.45 ";
SimplDateFormat f = new SimpleDateFormat("yyyy-mm-
Date d1 = f.parse(s1, new ParsePosition(0));
Date d2 = new Date();
if((d2.getTime() - d1.getTime()) < 120000) {
//do something
}
There is no more s2 - you don't need it for the current date
and s1 is the String ofor the date as you receive it
and s1 is the String ofor the date as you receive it
Try
boolean ok = Math.abs(System.currentTimeMillis() - java.sql.Timestamp.valueOf(args[0]).getTime()) < (1000 * 1 * 60 * 2);
(That assumes the timestamp is in a correct format of yyyy-mm-dd hh:mm:ss[.fffffffff]
If they are passing you hours ion 24 hour system it should be HH, if with AM/PM it should be hh, but then you need am/pm
First parse the timestamp as for_yan has shown above
Then compare the Timestamp with the current time using System.currentTimeMillis()
Then compare the Timestamp with the current time using System.currentTimeMillis()
SimpleDateFormat does not make any assumption of the format - whatever format you receive
you can specify in the pattern youself
you can specify in the pattern youself
ASKER
@yan, Date d1 = f.parse(strTimestamp, new ParsePosition(0)); return null.
If the format is *exactly* as you mentioned, then
boolean ok = Math.abs(System.currentTimeMillis() - new SimpleDateFormat("yyyy-MM-dd-HH.mm.ss").parse(dateString).getTime()) < (1000 * 1 * 60 * 2);
ASKER
@CEHJ, I get an Exception java.text.ParseException: Unparseable date: "03/26/2011 16:00:00"
>> Unparseable date: "03/26/2011 16:00:00"
That's a completely different format. Try
That's a completely different format. Try
boolean ok = Math.abs(System.currentTimeMillis() - new SimpleDateFormat("MM/dd/yyyy HH:mm:ss").parse(dateString).getTime()) < (1000 * 1 * 60 * 2);
This works - I checked
String ss1 = "2011-01-23-12.31.45";
SimpleDateFormat ff = new SimpleDateFormat("yyyy-MM-dd-HH.mm.ss");
java.util.Date dd1 = ff.parse(ss1, new ParsePosition(0));
java.util.Date dd2 = new java.util.Date();
System.out.println(dd1);
if((dd2.getTime() - dd1.getTime()) < 120000) {
//do something
}
ASKER
Does it work for GMT time comparisons?
I think it should.
Try it.
ASKER
format is 03/11/2011 19:35:00
Try this - I hope it should work for you
String ss1 = "2011-03-28-15.00.00";
SimpleDateFormat ff = new SimpleDateFormat("MM/dd/yy HH:mm:ss", Locale.UK);
ff.setTimeZone(new SimpleTimeZone(0,"GMT"));
java.util.Date dd1 = ff.parse(ss1, new ParsePosition(0));
java.util.Date dd2 = new java.util.Date();
System.out.println(dd1);
if((dd2.getTime() - dd1.getTime()) < 120000) {
//do something
}
System.out.println("long num;" + (dd2.getTime() - dd1.getTime()) );
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
>>Does it work for GMT time comparisons
All time comparisons, done in the way you're using it, are done using GMT
All time comparisons, done in the way you're using it, are done using GMT
This is one more variant which should work for you in GMT zone:
SimpleDateFormat ff = new SimpleDateFormat("MM/dd/yy HH:mm:ss z");
java.util.Date dd1 = ff.parse(ss1 + " GMT", new ParsePosition(0));
java.util.Date dd2 = new java.util.Date();
System.out.println(dd2);
if((dd2.getTime() - dd1.getTime()) < 120000L) {
//do something
}
That's the *exact* format?