The proper way of getting the current time in Java



  •   The one who wrote this code must be living by the credo "Carpe diem!", so he probably wanted to really grasp the "rightNow" moment :)).

           //Create the timestamp
            Calendar rightNow = Calendar.getInstance();
            Date now = rightNow.getTime();
            long nowl = now.getTime();
            String newtimestamp = String.valueOf(nowl);



  • by the time he's done his now is long gone, maybe that's why he uses a long as intermediate storage? [:P]



  • @Vlad.the.Impaler said:

     
           //Create the timestamp
            Calendar rightNow = Calendar.getInstance();
            Date now = rightNow.getTime();
            long nowl = now.getTime();
            String newtimestamp = String.valueOf(nowl);



    Not withstanding the variable names, this is a correct way of getting the current system time.


    Of course, String.valueOf(System.currentTimeMillis()) would be an easier way to get the same result...



Log in to reply