Java TimeUnit - Setup for a week's time unit

1k views Asked by At

I am trying to work on some methods and a response for an action/"command" being able to be used one time a week (spam prevention). My only problem is how to set my java time to set as a week. This is what I have:

public static long displayTime;

Methods:

    public static boolean setDisplayName(Player player, String displayName) {
    synchronized (cachedNames) {
        if((SerializableFilesManager.containsPlayer(Utils.formatPlayerNameForProtocol(displayName)) || cachedNames.contains(displayName) || !cachedNames.add(displayName)))
            return false;
        if(player.hasDisplayName())
            cachedNames.remove(player.getDisplayName());
    }
    displayTime = System.currentTimeMillis() + (1000*60*60);
    String displayname = player.getDisplayName();
    player.setDisplayName(displayName);
    FriendChatsManager.refreshChat(player);
    Highscores.highscores(player, displayname);
    player.getAppearence().generateAppearenceData();
    return true;
}

public static String convertToTime() {
    String time = "You have to wait "+(getTime() == 0 ? "few more seconds" :      getTime()+" minutes")+" to change your display name again!";
    return time;
}

public static int getTime() {
    return (int) (displayTime-System.currentTimeMillis()/60000);
}    

What I am trying to change into the "week's" time is displayTime = System.currentTimeMillis() + (1000*60*60);. I believe (1000*60*60) is 24 hours, corrrect? This is my only problem. When this is solved, the user should only be able to use the command once a week. The command is right here:

case "setdisplay":
            if (DisplayNames.displayTime > System.currentTimeMillis()) {
                player.getPackets().sendGameMessage(DisplayNames.convertToTime());
                return true;
            }
            if (!player.isDonator() && !player.isExtremeDonator()) {
                player.getPackets().sendGameMessage(
                        "You do not have the privileges to use this.");
                return true;
            }

DisplayNames is the class that the methods and everything is in. Thank you for all of the help and I am sure this is just a stupid question :P.

~Derek

2

There are 2 answers

2
Cheung Sean On

You can use Calendar class, it has method to modify date, such as adding day. And it could be converted to Date. It solves most of my problems regarding Time. Hope it works for you too.

0
fileyfood500 On

I used Calendar.DAY_OF_WEEK for days in a week