Solved

Time in milliseconds problem

Posted on 2004-09-01
6
206 Views
Last Modified: 2010-04-01
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,10) );
                  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.GregorianCalendar cal = new java.util.GregorianCalendar();
                  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
0
Comment
Question by:aks143
  • 3
  • 2
6 Comments
 
LVL 92

Expert Comment

by:objects
ID: 11950375
its less than isn't it
0
 
LVL 11

Expert Comment

by:sompol_kiatkamolchai
ID: 11950382
Month start from 0
0
 
LVL 11

Expert Comment

by:sompol_kiatkamolchai
ID: 11950384
cal.set(year, month, day, hour, minute, second );

should be

cal.set(year, month-1, day, hour, minute, second );
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 11

Accepted Solution

by:
sompol_kiatkamolchai earned 50 total points
ID: 11950395
from your code and data

String clientUpdate="2004-09-01 10:30:20"; --> This would be 2004-10-01 10:30:20
String dbUpdate = "2004-08-31 21:54:05"; --> This would be 2004-09-31 21:54:05 --> 2004-10-01 21:54:05




0
 
LVL 92

Expert Comment

by:objects
ID: 11950413
much easier though to use SimpleDateFormat to do your parsing
0
 

Author Comment

by:aks143
ID: 11950430
hii sompol,

thanks for it. I really overlooked it.

Regards,
aks
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
web service Rest Client creation 4 92
sortaSum challenge java 17 174
Java Exception example issues 11 84
maven set up 2 174
The advancement in technology has been a great source of betterment and empowerment for the human race, Nevertheless, this is not to say that technology doesn’t have any problems. We are bombarded with constant distractions, whether as an overload o…
Ransomware is a malware that is again in the list of security  concerns. Not only for companies, but also for Government security and  even at personal use. IT departments should be aware and have the right  knowledge to how to fight it.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question