aks143
asked on
Time in milliseconds problem
Hii all,
I am trying to do the following, which is easily judged from the code
String clientUpdate="2004-09-01 10:30:20";
String dbUpdate = "2004-08-31 21:54:05";
boolean isUpToDate=false;
try {
// get date of clientupdate in UTC milliseconds
int year = Integer.parseInt( clientUpdate.substring(0,4 ) );
int month = Integer.parseInt( clientUpdate.substring(5,7 ) );
int day = Integer.parseInt( clientUpdate.substring(8,1 0) );
int hour = Integer.parseInt( clientUpdate.substring(11, 13) );
int minute = Integer.parseInt( clientUpdate.substring(14, 16) );
int second = Integer.parseInt( clientUpdate.substring(17, 19) );
java.util.GregorianCalenda r cal = new java.util.GregorianCalenda r();
cal.set(year, month, day, hour, minute, second );
long nClientUpdate = cal.getTimeInMillis();
// get date of dbupdate in UTC milliseconds
year = Integer.parseInt( dbUpdate.substring(0,4) );
month = Integer.parseInt( dbUpdate.substring(5,7) );
day = Integer.parseInt( dbUpdate.substring(8,10) );
hour = Integer.parseInt( dbUpdate.substring(11,13) );
minute = Integer.parseInt( dbUpdate.substring(14,16) );
second = Integer.parseInt( dbUpdate.substring(17,19) );
cal.set(year, month, day, hour, minute, second );
long nDBUpdate = cal.getTimeInMillis();
if ( nClientUpdate >= nDBUpdate ) {
isUpToDate = true;
System.out.println( "green");
}else{
System.out.println( "red when: " + nClientUpdate +" < " + nDBUpdate );
}
} catch(Exception ignored) {
}
But the output is
red when: 1096619420596 < 1096660445596
I believe, i should get the output of green. B'coz my client update date is greater than dbupdate date.
How this strange behaviour??
Regards
aks
I am trying to do the following, which is easily judged from the code
String clientUpdate="2004-09-01 10:30:20";
String dbUpdate = "2004-08-31 21:54:05";
boolean isUpToDate=false;
try {
// get date of clientupdate in UTC milliseconds
int year = Integer.parseInt( clientUpdate.substring(0,4
int month = Integer.parseInt( clientUpdate.substring(5,7
int day = Integer.parseInt( clientUpdate.substring(8,1
int hour = Integer.parseInt( clientUpdate.substring(11,
int minute = Integer.parseInt( clientUpdate.substring(14,
int second = Integer.parseInt( clientUpdate.substring(17,
java.util.GregorianCalenda
cal.set(year, month, day, hour, minute, second );
long nClientUpdate = cal.getTimeInMillis();
// get date of dbupdate in UTC milliseconds
year = Integer.parseInt( dbUpdate.substring(0,4) );
month = Integer.parseInt( dbUpdate.substring(5,7) );
day = Integer.parseInt( dbUpdate.substring(8,10) );
hour = Integer.parseInt( dbUpdate.substring(11,13) );
minute = Integer.parseInt( dbUpdate.substring(14,16) );
second = Integer.parseInt( dbUpdate.substring(17,19) );
cal.set(year, month, day, hour, minute, second );
long nDBUpdate = cal.getTimeInMillis();
if ( nClientUpdate >= nDBUpdate ) {
isUpToDate = true;
System.out.println( "green");
}else{
System.out.println( "red when: " + nClientUpdate +" < " + nDBUpdate );
}
} catch(Exception ignored) {
}
But the output is
red when: 1096619420596 < 1096660445596
I believe, i should get the output of green. B'coz my client update date is greater than dbupdate date.
How this strange behaviour??
Regards
aks
its less than isn't it
Month start from 0
cal.set(year, month, day, hour, minute, second );
should be
cal.set(year, month-1, day, hour, minute, second );
should be
cal.set(year, month-1, day, hour, minute, second );
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
much easier though to use SimpleDateFormat to do your parsing
ASKER
hii sompol,
thanks for it. I really overlooked it.
Regards,
aks
thanks for it. I really overlooked it.
Regards,
aks