System.currentTimeMillis()

Discussion in 'Spigot Plugin Development' started by Ilikebananas, Oct 9, 2019.

  1. How can i check if a time i got from System.currentTimeMillis() was 24 Hours ago or older?
    And when it is how can i get the hours and minutes that it will take to get to 24 hours?
     
  2. As the method suggest, it counts milliseconds from some point in time (1. January 1970, but that's not important).

    There are 1000 ms in 1 second. 60 seconds in 1 minute, 60 minutes in one hour and 24 hours in a day.
    Thus, to get the number of milliseconds in a day, you multiply these numbers. I.e you have 1000*60*60*24 milliseconds in a day.

    Now all you need to do is get the timestamp at another point in time and check if the two differ by more than the amount of milliseconds in a day.
     
  3. When you begin the task, get the current time and the time 24 hours later

    To get the time until 24 hours subtract the time 24 hours ahead from the current time
     
    #3 Snowcharge, Oct 9, 2019
    Last edited: Oct 9, 2019
  4. It looks like that works:
    if((System.currentTimeMillis()-time) >= (1000*60*60*24)) {

    but now i want to get the time remaining(in Hours and Seconds) how can i do that?
     
  5. You get the difference between the two timestamps.
    And then you just use the same thinking I detailed above and you'll get there in no time.
     
  6. Like this:
    Long difference = System.currentTimeMillis() + (1000*60*60*24) - time;
    It looks like it works :D
     
  7. No doesnt work :(
    I did this in my test application:
    Code (Text):

    public static void main(String[] args) {
        Long time = 1570630719877L;

        System.out.println(System.currentTimeMillis());
        System.out.println("-------------");

        if((System.currentTimeMillis()-time) >= (1000*60*60*24)) {
            System.out.println("lol");
         } else {
            Long difference = System.currentTimeMillis() + (1000*60*60*24) - time;
            System.out.println(difference);
            System.out.println("Du kannst deine Tägliche belohnung erst wieder in " + TimeUnit.MILLISECONDS.toHours(difference) + " Stunden abholen!");
        }
    }
     
    Result:
    Du kannst deine Tägliche belohnung erst wieder in 28 Stunden abholen!
    28 Hours??? Whats wrong?
     
  8. first, dont use the object long, just use its primiitive. second, it was literally told to you. you simply need the difference of the time stamps. you dont need to throw the long of day in there. this represents how much time is passed. now, to determine how much time is left, subtract the time passed from the long of day and you have however much time is left until its been 24 hours. then you do your string formatting. this is something you could easily google for as thousands of people have probably asked your same exact question

    /e if u want a one-liner, write it as (#*#*#*#) - (currTime - time)
     
  9. Or you just use the TimeUnit from Java 8:
    Code (Text):

    TimeUnit.DAYS.toMillis(1);
     
    Or we use LocalDateTime and Duration from Java 8.
    This looks even easier to calculate time differences or manage "long" cooldowns:

    Code (Text):

            LocalDateTime endDate = LocalDateTime.now().plusHours(24);

            if (LocalDateTime.now().isAfter(endDate)) {
                System.out.println("Current date is after set date!");
            } else {
                Duration duration = Duration.between(LocalDateTime.now(), endDate);
                System.out.println("You need to wait another " + duration.toHours() + " hours and " + duration.toMinutesPart() + " minutes");
            }
     
    Which would print:
     
    • Agree Agree x 1
    • Useful Useful x 1
  10. Or the calendar class works just as well.

    Calendar cal = Calendar.getInstance();
    cal.getTimeInMilis();
     
  11. I suggest using java Instant, if you are having to store the original time anyway, why not just store it in an instant, it will store everything you need and will even give you any conversions you need of the bat (days, hours, seconds, millis, even nanos or micros)
     
  12. Doesnt exist for me :(
     
  13. Just use this code but in the line where you calculate the difference for displaying, don't add a day of milliseconds.
     
  14. if it doesnt exist, update the java youre developing against, or make sure you have the right import