JAVA BIG HELP.. PLEASE.....

HallowMan

Well-known member
  • Jan 10, 2010
    5,788
    1,005
    113
    You could have used another Date object to convert the milliseconds different


    Code:
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    public String countTime (String inTime, String outTime) {
    
            SimpleDateFormat format;
            try {
                format = new SimpleDateFormat("HH:mm:ss");
                Date time1 = format.parse(inTime);
                Date time2 = format.parse(outTime);
                long difference = time2.getTime() - time1.getTime();
    
                format = new SimpleDateFormat("HH:mm");
                Date resultdate = new Date(difference);
    
                return format.format(resultdate);
            } catch (ParseException ex) {
                ex.printStackTrace();
            }
            return "";
    }
     
    • Like
    Reactions: danlix

    danlix

    Well-known member
  • Feb 12, 2012
    2,814
    767
    113
    Dark Horizon
    Code:
    import java.text.ParseException;
    import java.util.concurrent.TimeUnit;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    
    public class Dates {
    
        public static void main(String[] args) {
            System.out.println(convertToSeconds("12:40:10", "03:40:10"));
        }
    
        public static long GetTime(String inTime, String outTime) {
            try {
                java.text.DateFormat df = new java.text.SimpleDateFormat("hh:mm:ss");
                java.util.Date date1 = df.parse(inTime);
                java.util.Date date2 = df.parse(outTime);
                long diff = date2.getTime() - date1.getTime();
                return diff;
            } catch (ParseException ex) {
                Logger.getLogger(Dates.class.getName()).log(Level.SEVERE, null, ex);
                return 0;
            }
        }
        
        public static String convertToSeconds(String inTime, String outTime) {
            long mil = GetTime(inTime, outTime);
            return String.format("%d min, %d sec",
                    TimeUnit.MILLISECONDS.toMinutes(mil),
                    TimeUnit.MILLISECONDS.toSeconds(mil)
                    - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(mil)));
        }
    }

    ඕක කෙලින්ම රන් කරල බලපන්.හදිසියට ලිව්වේ. අවුලක් තිබ්බොත් අහපන් මල්ලී.

    http://stackoverflow.com/questions/3514639/android-java-how-to-subtract-two-times


    Ela ela Thanks aiya :)
     

    danlix

    Well-known member
  • Feb 12, 2012
    2,814
    767
    113
    Dark Horizon
    You could have used another Date object to convert the milliseconds different


    Code:
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    public String countTime (String inTime, String outTime) {
    
            SimpleDateFormat format;
            try {
                format = new SimpleDateFormat("HH:mm:ss");
                Date time1 = format.parse(inTime);
                Date time2 = format.parse(outTime);
                long difference = time2.getTime() - time1.getTime();
    
                format = new SimpleDateFormat("HH:mm");
                Date resultdate = new Date(difference);
    
                return format.format(resultdate);
            } catch (ParseException ex) {
                ex.printStackTrace();
            }
            return "";
    }

    Thanks :)