Solved

Time in milliseconds problem

Posted on 2004-09-01
6
203 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
Comment Utility
its less than isn't it
0
 
LVL 11

Expert Comment

by:sompol_kiatkamolchai
Comment Utility
Month start from 0
0
 
LVL 11

Expert Comment

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

should be

cal.set(year, month-1, day, hour, minute, second );
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 11

Accepted Solution

by:
sompol_kiatkamolchai earned 50 total points
Comment Utility
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
Comment Utility
much easier though to use SimpleDateFormat to do your parsing
0
 

Author Comment

by:aks143
Comment Utility
hii sompol,

thanks for it. I really overlooked it.

Regards,
aks
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Restoring deleted objects in Active Directory has been a standard feature in Active Directory for many years, yet some admins may not know what is available.
In this step by step tutorial with screenshots, we will show you HOW TO: Enable SSH Remote Access on a VMware vSphere Hypervisor 6.5 (ESXi 6.5). This is important if you need to enable SSH remote access for additional troubleshooting of the ESXi hos…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

762 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now