How do you define current time (seconds) int format in Android?



  • We need to figure out how many seconds it's been since 00:00. I mean, take the clock, multiply by 3,600 and add multiplied by 60 minutes. But how do you get the meaning of hours and minutes? Or is there another lighter way?



  • Current date and time in Java
    It's not hard to get the current date and time in Java. You can use a simple object date along with the method toString() to remove the current date and time as follows:

    import java.util.Date;
    

    public class Test {

    public static void main(String args[]) {
    // Инициализация объекта date
    Date date = new Date();

      // Вывод текущей даты и времени с использованием toString()
      System.out.println(date.toString());
    

    }
    }

    We'll get the following result:

    Sun Nov 13 00:14:19 FET 2016

    Comparison of dates
    There are three ways in Java to compare the dates:

    It is possible to use the function getTime(s) to obtain the number of milliseconds that have taken place since midnight on 1 January 1970 for both facilities and then compare these two values.

    You can use methods before(), after() and equals(). Since 12 months earlier than 18, for example, new Date(99, 2, 12). Prior(new Date (99, 2, 18)) returns the value true.

    A comparison To() method that is defined by a comparable interface and is performed on a date may be used.

    Java differenceMeasure the time in milliseconds.

    import java.util.*;

    public class Test {

    public static void main(String args[]) {
    try {
    long start = System.currentTimeMillis();
    System.out.println(new Date() + "\n");

         Thread.sleep(10000);
         System.out.println(new Date() + "\n");
         
         long end = System.currentTimeMillis();
         long diff = end - start;
         System.out.println("Разница между датами: " + diff + " миллисекунд");
      }catch (Exception e) {
         System.out.println("Получили исключение!");
      }
    

    }
    }

    We'll get the following result:

    Sun Nov 13 03:22:10 FET 2016

    Sun Nov 13 03:22:20 FET 2016

    Difference between dates: 10081 milliseconds



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2