Results 1 to 3 of 3
  1. #1
    Join Date
    May 2010
    Posts
    22
    Rep Power
    0

    Default calculate time diff for particular time period

    Java Code:
    package tcplistener;
    
    import java.sql.Connection;
    import java.sql.Date;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Timestamp;
    import java.text.DateFormat;
    import java.text.SimpleDateFormat;
    import java.util.ArrayList;
    import java.util.HashSet;
    import java.util.Set;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.naming.NamingException;
    
    
    
    
    
    
    public class testing1
    {
    
    
        private static PreparedStatement preparedStatement;
        private static ResultSet rs;
        public static void main(String[] args) throws NamingException, SQLException
        {
    
    
            int objectId;
            int clientId;
             DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            // String gpstime = null;
             PreparedStatement preparedStatement = null;
             Connection conn=null;
             long mill;
             long diffSeconds=0;
             long diffMinutes=0;
             try
             {
              Class.forName("com.mysql.jdbc.Driver");
            Connection conn1=DriverManager.getConnection("jdbc:mysql://108.108.8.4:3306/navl?","root", "ajmani@%");
            preparedStatement = conn1.prepareStatement("select gpstime from xydata a inner join fm4features f on a.ID=f.ID where a.objectId= '18'  AND a.clientId = '1' AND a.gpstime >= '2010-05-11 12:20:40'  AND a.gpstime <= '2010-05-13 13:14:08' AND f.DataId='1' AND f.value = '1'");
            rs = preparedStatement.executeQuery();
            int a = 0;
            int b=0;
             int b1=0;
             int b2=0;
             int x=0;
             float secs = 0;
    
            Set<String> dates = new HashSet<String>();
            ArrayList<Integer> l=new ArrayList<Integer>();
               Date d1=null;
              Timestamp t1 = null;
              Timestamp t2 = null;
              int count  = 0;
    
              while ( rs.next() )
                {
    
                    String gpstime = rs.getString("GpsTime");
                //System.out.println("Time in minutes: " + diffSeconds+ " seconds.");
               
                     
                 if (dates.add(gpstime) )
                 {
                     //System.out.println(rs.getTimestamp(1));
                    Date d2 = rs.getDate(1);
                    System.out.print(d2);
                    t2 =   rs.getTimestamp(1);
                     
                     count++;
                   if (t1 == null )
                  {
                       int nano = t2.getSeconds();
                       //System.out.println(nano);
                       System.out.println(nano);
                       System.out.println(t2);
                       System.out.println(count);
                  }
    
                  else
                  {
                        
                        mill= (t1.getTime() - t2.getTime())/-1;
    
                        //System.out.println(d1);
                        diffSeconds = mill / 1000;
                        diffMinutes = mill / (60 * 1000);
                        long diffHours = mill / (60 * 60 * 1000);
                        long diffDays = mill / (24 * 60 * 60 * 1000);
                        //System.out.println("\nThe Date Different");
                        //System.out.println("Time in milliseconds: " + mill+ " milliseconds.");
                        System.out.println("GPSTime " +gpstime+ " Time in seconds: " + diffSeconds+ " seconds.");
                        //System.out.println("Time in minutes: " + diffMinutes+ " minutes.");
                        //System.out.println("Time in hours: " + diffHours+ " hours.");
                        //System.out.println("Time in days: " + diffDays+ " days.");
    
                 }
                  t1 = t2;
                 l.add((diffSeconds < 80) ? (int) diffSeconds : (int) (diffMinutes % 60));
                 }
             }
    
              
    
              for (int i=0; i<l.size();)
              {
                  secs+=l.get(i);
    
               
                  i++;
                 
              }
                 System.out.println(secs);
                 int hours = (int) (secs / 3600),
                 remainder = (int) (secs % 3600),
                 minutes = remainder / 60,
                 seconds = remainder % 60;
    
                    String disHour = (hours < 10 ? "0" : "") + hours,
                    disMinu = (minutes < 10 ? "0" : "") + minutes ,
                    disSec = (seconds < 10 ? "0" : "") + seconds ;
    
                    if (count < 2)
                    {
                        int se = Integer.parseInt(disSec);
                        String min = (t2.getSeconds() < 10 ? "0" : "") + t2.getSeconds();
                        System.out.println("00" + ":" +  "00" + ":" + min + " hh:mm:ss");
                    }
    
                    System.out.println(disHour +":"+ disMinu+":"+disSec+ " hh:mm:ss");
                
              
             }catch(Throwable th)
             {
               Logger.getLogger(testing1.class.getName()).log(Level.SEVERE, null, th);
             }finally
             {
               if(preparedStatement!=null)
                   preparedStatement.close();
               if(conn!=null)
                   conn.close();
    
             }
        }
    }
    i got the output-> 08:34:00 hh:mm:ss

    but i need the separate total time for date

    2010-05-11->get the time and count the total time(output)
    2010-05-12->get the time and count the total time(output)
    2010-05-13->get the time and count the total time(output)

    any body having idea

    thanks in advance
    Last edited by baktha.thalapathy; 05-24-2010 at 11:47 AM. Reason: edit

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,526
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by baktha.thalapathy View Post
    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    Again that stupid comment block? Is there a reason for everybody to include that block of comment? The reason is beyond me ...

    kind regards,

    Jos

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. Replies: 3
    Last Post: 08-23-2011, 12:46 PM
  2. Class Time - represents time of day
    By verbazon in forum New To Java
    Replies: 1
    Last Post: 04-13-2009, 01:06 AM
  3. first time
    By oiballab in forum Introductions
    Replies: 2
    Last Post: 12-18-2008, 01:11 PM
  4. Replies: 21
    Last Post: 08-01-2008, 11:13 AM
  5. signing applets for a period of time
    By sandeepk84@gmail.com in forum Java Applets
    Replies: 0
    Last Post: 12-04-2007, 01:34 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •