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

    Default how to get the seconds out of it

    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.Collections;
    import java.util.HashSet;
    import java.util.Set;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.naming.NamingException;
    import java.util.Map;
    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.LinkedHashMap;
    import java.util.List;
    
    
    
    public class Final
    {
    
    
       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 Total=0;
            int b;
            int b2;
            int nano=0;
            int nano1=0;
             String disHour="";
             String disMinu="";
             String disSec="";
             Speedfinal f=new Speedfinal();
             ArrayList arr = new ArrayList();
             ArrayList arr1 = new ArrayList();
           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-06-05 00:00:00'  AND a.gpstime <= '2010-06-05 23:59:59' AND f.DataId='1' AND f.value = '1'");
             rs = preparedStatement.executeQuery();
             long totalTimeInMillis=0;
            
             Set<String> dates = new HashSet<String>();
             ArrayList<Integer> l=new ArrayList<Integer>();
             ArrayList<Integer> m=new ArrayList<Integer>();
             ArrayList<Long> n=new ArrayList<Long>();
             Map <String, Long> datesAndTotal = new HashMap<String, Long>();
             DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
             Timestamp t1 = null;
             Timestamp t2 = null;
             int count  = 0;
             long sum = 0;
             long result=0;
             long results=0;
             while ( rs.next() )
             {
    
                String gpstime = rs.getString("GpsTime");
                if (dates.add(gpstime))
                {
                   Date d2 = rs.getDate(1);
                   t2 = rs.getTimestamp(1);
                    nano = t2.getSeconds();
                      //System.out.println(t2);
                      //System.out.println(nano);
                   count++;
                   if (t1 == null )
                   {
                      //nano = t2.getSeconds();
                      //System.out.println(t2);
                      //System.out.println(nano);
                    }
    
                   else
                   {
    
                      totalTimeInMillis = (t2.getTime() - t1.getTime()) / 1000;
                       String dateString = df.format(t2);
                       System.out.println("GPSTime " +gpstime+ " Time in seconds: " + totalTimeInMillis+ " seconds.");
                      if (datesAndTotal.containsKey(dateString))
                      {
                         m.add((totalTimeInMillis < 80) ? (int) totalTimeInMillis : (int) (totalTimeInMillis % 60));
                       
                          for (int y=0; y<m.size();)
                          {
                          sum+=m.get(y);
                          y++;
                        }
                          sum+= (datesAndTotal.get(dateString))/-1;
                          System.out.println(sum);
                          
                      }
                      datesAndTotal.put(dateString, sum);
                   }
                   t1 = t2;
                  
                }
    
    
    
    
             }           
                         List mapKeys = new ArrayList(datesAndTotal.keySet());
                         List mapValues = new ArrayList(datesAndTotal.values());
                         Collections.sort(mapValues);
                          Collections.sort(mapKeys);
                        LinkedHashMap Maps = new LinkedHashMap();
                        Iterator valueIt = mapValues.iterator();
                        while (valueIt.hasNext()) {
                        Object val = valueIt.next();
                         Iterator keyIt = mapKeys.iterator();
                        while (keyIt.hasNext()) {
                        Object key = keyIt.next();
                        if (datesAndTotal.get(key).toString().equals(val.toString()))
                        {
                        datesAndTotal.remove(key);
                        mapKeys.remove(key);
                        Maps.put(key, val);
                        break;
                                   }
                              }
                        }
    
                        
                        Iterator it1 = Maps.entrySet().iterator();
                        String prevKey="";
                        while (it1.hasNext()) {
                           Map.Entry entry = (Map.Entry) it1.next();
                           String presentKey=(String)entry.getKey();
                           if(prevKey.length()==0){
                                   System.out.println(presentKey+"=>"+entry.getValue());
                                   results=(Long) entry.getValue()+60;
                                   System.out.println(results);
                                   n.add(results);
                                 arr1.add(presentKey);
                                 int hours = (int) (results / 3600),
                                     remainder = (int) (results % 3600),
                                     minutes = remainder / 60,
                                     seconds = remainder % 60;
                                     
                                 disHour = (hours < 10 ? "0" : "") + hours;
                                 disMinu = (minutes < 10 ? "0" : "") + minutes;
                                 disSec = (seconds < 10 ? "0" : "") + seconds;
                                if (results==0 ||nano<1)
                                         {
                                            //int se = Integer.parseInt(disSec);
                                            //String min = (t2.getSeconds() < 10 ? "0" : "") + t2.getSeconds();
                                            System.out.println("00" + ":" +  00 + ":" + 00 + " hh:mm:ss");
                                            arr.add(00 + ":" +  00 + ":" + 00);
                                         }
                                    System.out.println(disHour +":"+ disMinu+":"+disSec+ " hh:mm:ss");
                                    arr.add(disHour + ":" + disMinu + ":"+ disSec);
                                  
                         
                           }
                           else{
                                   Long present=(Long)entry.getValue();
                                   Long prev=(Long) Maps.get(prevKey);
                                   result=(Long) (present - prev)+30;
                                   System.out.println(""+presentKey+"=>"+result);
                                 System.out.println(result);
                                 n.add(result);
                                  arr1.add(presentKey);
                                 int hours = (int) (result / 3600),
                                     remainder = (int) (result % 3600),
                                     minutes = remainder / 60,
                                     seconds = remainder % 60;
    
                                 disHour = (hours < 10 ? "0" : "") + hours;
                                 disMinu = (minutes < 10 ? "0" : "") + minutes;
                                 disSec = (seconds < 10 ? "0" : "") + seconds;
                                       if (result==0||nano < 1)
                                         {
                                            int se = Integer.parseInt(disSec);
                                            String min = (t2.getSeconds() < 10 ? "0" : "") + t2.getSeconds();
                                            System.out.println("00" + ":" +  min + ":" + se + " hh:mm:ss");
                                            arr.add(00 + ":" +  00 + ":" + 00);
                                         }
    
                                     System.out.println(disHour +":"+ disMinu+":"+disSec+ " hh:mm:ss");
                                    arr.add(disHour + ":" + disMinu + ":"+ disSec);
                                     
                                   }
                           prevKey=(String)presentKey;
    
                        
                          
                         }
    
                          
    
    
                             for (int i=0; i<n.size();)
                          {
                              Total+=n.get(i);
                              // System.out.println(Total);
                              i++;
    
                          }
                             System.out.println(Total);
                                     int hours = (int) (Total / 3600),
                                     remainder = (int) (Total % 3600),
                                     minutes = remainder / 60,
                                     seconds = remainder % 60;
    
                                  disHour = (hours < 10 ? "0" : "") + hours;
                                  disMinu = (minutes < 10 ? "0" : "") + minutes;
                                   disSec = (seconds < 10 ? "0" : "") + seconds;
                                       if (Total==0||count < 2)
                                         {
                                            int se = Integer.parseInt(disSec);
                                            String min = (t2.getSeconds() < 10 ? "0" : "") + t2.getSeconds();
                                            System.out.println("00" + ":" +  min + ":" + se + " hh:mm:ss");
                                            arr.add(00 + ":" +  00 + ":" + 00);
                                         }
    
                                     System.out.println("Total Time=>" +disHour +":"+ disMinu+":"+disSec+ " hh:mm:ss");
                                f.getClass();
                                
                 
          }catch(Throwable th)
    
          {
             Logger.getLogger(testing1.class.getName()).log(Level.SEVERE, null, th);
          }finally
          {
             if(preparedStatement!=null)
                preparedStatement.close();
             if(conn!=null)
                conn.close();
    
          }
    }
    
    }
    output

    [code]2010-06-05 00:06:28.0
    GPSTime 2010-06-05 12:08:19.0 Time in seconds: 43311 seconds.
    ?
    GPSTime 2010-06-05 12:09:20.0 Time in seconds: 61 seconds.
    61
    {2010-06-05=61}
    2010-06-05=>61
    121
    0
    Total Time=>00:00:00 hh:mm:ss

    the every first row compared and print the value ex. 43311, but i am not able to get the sec out of it like

    m.add((totalTimeInMillis < 80) ? (int) totalTimeInMillis : (int) (totalTimeInMillis % 60));
    the above code is not compare the value and print out of it ?

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

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

    Default

    Quote Originally Posted by Eranga View Post
    Can you explain your question more clearly? What conversion you want to do?
    I don't understand the question either: we're looking at a bunch of code; some output (supposedly generated by that program) and a question about second ... If people want help they should help the people willing to help by asking a clear question.

    kind regards,

    Jos

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default

    Last edited by DarrylBurke; 06-06-2010 at 01:58 PM.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default

    Wow, that was fast! Thanks, Pete.

    db

  7. #7
    somanayr is offline Member
    Join Date
    May 2010
    Posts
    27
    Rep Power
    0

    Default

    If you have ms and you want seconds, divide by 1000 :D !

  8. #8
    Join Date
    May 2010
    Posts
    22
    Rep Power
    0

    Default

    i am getting the timestamp from database
    2010-06-01 09:36:13
    2010-06-01 09:37:14
    2010-06-01 09:38:14
    2010-06-01 09:39:14
    2010-06-01 09:39:45
    2010-06-01 17:05:27
    2010-06-01 17:06:28
    2010-06-01 17:07:28
    2010-06-01 17:08:28
    2010-06-01 17:10:29
    2010-06-01 17:11:29
    2010-06-01 17:12:29
    2010-06-01 17:13:29

    i need output
    Java Code:
    from                              to
    2010-06-01 09:36:13      2010-06-01 09:39:45-->take the total time (output)
    2010-06-01 17:05:27     2010-06-01 17:08:28-->take the total time(output)
    2010-06-01 17:10:29     2010-06-01 17:13:29-->take the total time(output)
    example
    2010-06-01 17:08:28-end time
    2010-06-01 17:10:29-start time

    please help me how to write the java code to get the above output
    Last edited by baktha.thalapathy; 06-08-2010 at 08:04 AM.

  9. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,259
    Rep Power
    25

    Default

    when the minutes above 1 take the time diff
    Not sure what you are asking for.
    i need output
    Code:
    from to
    2010-06-01 09:36:13 2010-06-01 09:39:45-->take the time diff (output)
    What do ask here?
    Are you asking to take the two time stamps shown above and compute the difference in some units of time.
    For the times shown above, would 32 seconds be the time diff that you want?

  10. #10
    Join Date
    May 2010
    Posts
    22
    Rep Power
    0

    Default

    i am getting the Timestamp from database
    2010-06-01 09:36:13
    2010-06-01 09:37:14
    2010-06-01 09:38:14
    2010-06-01 09:39:14
    2010-06-01 09:39:45
    2010-06-01 17:05:27
    2010-06-01 17:06:28
    2010-06-01 17:07:28
    2010-06-01 17:08:28
    2010-06-01 17:10:29
    2010-06-01 17:11:29
    2010-06-01 17:12:29
    2010-06-01 17:13:29

    i need output

    From To
    2010-06-01 09:36:13 2010-06-01 09:39:45-->take the total time(output)
    2010-06-01 17:05:27 2010-06-01 17:08:28-->take the total time(output)
    2010-06-01 17:10:29 2010-06-01 17:13:29-->take the total time(output)

    the above output fromtime is the starting time and totime is the endtime

    i do want to calculate the time diff between 2010-06-01 09:39:45 to 2010-06-01 17:05:27 please see the above need output with database result.

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,751
    Rep Power
    19

    Default

    But how do you determine the cutoff point for the calulcation?

    Why 09:36:13 to 09:39:45 and not 09:36:13 to 17:07:28?

    How do you determine the cutoff?

    In any case, parse them to a Date (SimpleDateFormat) and subtract one from the other to get milliseconds.

  12. #12
    Join Date
    May 2010
    Posts
    22
    Rep Power
    0

    Default

    how to subtract one from the other to get milliseconds

    please provide any sample code

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,751
    Rep Power
    19

    Default

    You can do maths can't you?
    What do you think you need to do?

    If you can't do this then maybe you shouldn't be doing software?

  14. #14
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,259
    Rep Power
    25

    Default

    For each time:
    Create a SimpleDateFormat object.
    Use the getCalendar() method to get a Calendar() object
    Use the getTimeInMillis() to get the milliseconds value.

    Subtract one from the other to get the difference.

  15. #15
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,751
    Rep Power
    19

    Default

    Why go through all that?
    Simply parse() with the SimpleDateFormat, then use getTime() on the returned Date object to get the time in millis. There's no need whatsoever to go through a Calendar.

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

    Default

    Quote Originally Posted by JosAH View Post
    I don't understand the question either: we're looking at a bunch of code; some output (supposedly generated by that program) and a question about second ... If people want help they should help the people willing to help by asking a clear question.

    kind regards,

    Jos
    Yes it is. I think that's because most of them believe that whoever looking into the thread will fix all stuff there.

Similar Threads

  1. Replies: 3
    Last Post: 04-05-2010, 01:08 PM
  2. Convert microseconds to seconds
    By Basit56 in forum New To Java
    Replies: 4
    Last Post: 12-31-2009, 05:54 AM
  3. Difference in seconds between two times
    By jazzy639 in forum New To Java
    Replies: 11
    Last Post: 09-26-2009, 06:58 PM
  4. getting time elapsed in seconds
    By ali_sakar in forum New To Java
    Replies: 3
    Last Post: 03-06-2009, 08:37 AM
  5. Need help with calculate the number of seconds
    By ProUnbeatable in forum New To Java
    Replies: 5
    Last Post: 10-11-2008, 03:07 AM

Posting Permissions

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