Solved More & More Dates in Java

Discussion in 'Plugin Development' started by Sansanvi, Apr 20, 2017.

Thread Status:
Not open for further replies.
  1. Offline

    Sansanvi

    I'm just wondering how do I convert a date e.g Thursday, April 20, 2017, 4:48 PM to milliseconds so I can check if the time has gone 1 minute?
     
  2. Offline

    MCMastery

    Where do you get that date?
     
  3. Offline

    timtower Administrator Administrator Moderator

    @Sansanvi Syste.currentMs() or something along those lines. Then convert the ms to minutes.
    Get the difference with the stored one.
     
  4. Offline

    Sansanvi

    Code:
    public static void temporaryBan(Player p, long time, String reason) {
            isTempBanned = true;
    
            Date date = new Date((time * 1000) + System.currentTimeMillis());
            SimpleDateFormat sdf = new SimpleDateFormat("EEEE, MMMM d, yyyy h:mm a");
            sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
            String timeDate = sdf.format(date);
    
            tempBanned.add(p.getUniqueId());
            temporary.set("bans.arraylist", tempBanned);
            temporary.set(p.getUniqueId() + ".banned", true);
            temporary.set(p.getUniqueId() + ".reason", reason);
            temporary.set(p.getUniqueId() + ".amount", temporary.getInt(p.getUniqueId() + ".amount") + 1);
            temporary.set(p.getUniqueId() + ".time", timeDate);
            saveFile(temporaryYML, temporary);
            p.kickPlayer(reason);
        }
    @timtower
    And about the System.currentTimeMillis(); So I would write date.compareTo(New date)? But I think I can do something like this:
    Code:
    if(date.compareTo(newDate) >= System.currentTimeMillis()) {
        //Whatever
    }
     
    Last edited: Apr 20, 2017
  5. Offline

    MCMastery

    Don't store the date as a string, just store it as System.currentTimeMillis. Then you can compare the milliseconds directly without converting.

    Like this

    Code:
    public static void temporaryBan(Player p, long time, String reason) {
            isTempBanned = true;
    
            tempBanned.add(p.getUniqueId());
            temporary.set("bans.arraylist", tempBanned);
            temporary.set(p.getUniqueId() + ".banned", true);
            temporary.set(p.getUniqueId() + ".reason", reason);
            temporary.set(p.getUniqueId() + ".amount", temporary.getInt(p.getUniqueId() + ".amount") + 1);
            temporary.set(p.getUniqueId() + ".time", System.currentTimeMillis());
            saveFile(temporaryYML, temporary);
            p.kickPlayer(reason);
        }
    Also, what is isTempBanned? That would get set to true once anyone is tempbanned - that variable is only really checking whether anyone has ever been tempbanned before
     
    Sansanvi likes this.
  6. Offline

    Sansanvi

    Thanks, @MCMastery & @timtower for the help much appreciated!
     
    MCMastery likes this.
Thread Status:
Not open for further replies.

Share This Page