Best way to subtract a date from one another?

Discussion in 'Plugin Development' started by TheWolfBadger, Aug 5, 2017.

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

    TheWolfBadger

    So basically I have two dates and I need to subtract one from the other. It's for when the player is away and then they come back. I need to take the new time and time they started from and figure out how much time has passed. None of the code is tested, I'm just trying to find the most efficient and possibly best way to do this. Here is what I have so far though:
    Code:
                    for(UUID users : timeBeenAwayStart.keySet()) {
                        if(!afkManager.isAfk(Bukkit.getPlayer(users))) {
                            //No longer AFK, add their start time
                            String timeStamp = new SimpleDateFormat("MM:dd:HH:mm:ss").format(Calendar.getInstance().getTime());
                            String addToTime = "";
                            try {
                                String nowTime[] = timeStamp.split(":");
                                String startTime[] = timeBeenAwayStart.get(users).split(":");
                                /* Trial 1 Test? */
                                int monthsBetween = Integer.parseInt(nowTime[0]) - Integer.parseInt(startTime[0]);
                                if(!(monthsBetween > 0)) {
                                    monthsBetween = 0;
                                }
                                int daysBetween = Integer.parseInt(nowTime[1]) - Integer.parseInt(startTime[1]);
                                if(!(daysBetween > 0)) {
                                    daysBetween = 0;
                                }
                                int hoursBetween = Integer.parseInt(nowTime[2]) - Integer.parseInt(startTime[2]);
                                if(!(hoursBetween > 0)) {
                                    hoursBetween = 0;
                                }
                                int minutesBetween = Integer.parseInt(nowTime[3]) - Integer.parseInt(startTime[3]);
                                if(!(minutesBetween > 0)) {
                                    minutesBetween = 0;
                                }
                                int secondsBetween = Integer.parseInt(nowTime[4]) - Integer.parseInt(startTime[4]);
                                if(!(secondsBetween > 0)) {
                                    secondsBetween = 0;
                                }
                                if(!(monthsBetween > 9)) {
                                    //It needs a zero in front
                                    addToTime += "0" + String.valueOf(monthsBetween) + ":";
                                } else {
                                    addToTime += String.valueOf(monthsBetween) + ":";
                                }
                                if(!(daysBetween > 9)) {
                                    //It needs a zero in front
                                    addToTime += "0" + String.valueOf(daysBetween) + ":";
                                } else {
                                    addToTime += String.valueOf(daysBetween) + ":";
                                }
                                if(!(hoursBetween > 9)) {
                                    //It needs a zero in front
                                    addToTime += "0" + String.valueOf(hoursBetween) + ":";
                                } else {
                                    addToTime += String.valueOf(hoursBetween) + ":";
                                }
                                if(!(minutesBetween > 9)) {
                                    //It needs a zero in front
                                    addToTime += "0" + String.valueOf(minutesBetween) + ":";
                                } else {
                                    addToTime += String.valueOf(minutesBetween) + ":";
                                }
                                if(!(secondsBetween > 9)) {
                                    //It needs a zero in front
                                    addToTime += "0" + String.valueOf(secondsBetween);
                                } else {
                                    addToTime += String.valueOf(secondsBetween);
                                }
                                if(referredPlayerInstances.containsKey(users)) {
                                    //They have an instance
                                    if(!addToTime.equals("")) {
                                        referredPlayerInstances.get(users).addToEndTime(addToTime);
                                    }
                                }
                                /**/
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }
     
  2. Offline

    timtower Administrator Administrator Moderator

    @TheWolfBadger Why not get the milliseconds and subtract those from each other?
     
  3. Offline

    TheWolfBadger

    Is there a way I can convert the milliseconds back to the date format I need?
     
  4. Offline

    timtower Administrator Administrator Moderator

  5. Offline

    TheWolfBadger

    Code:
    for(UUID users : timeBeenAwayStart.keySet()) {
                        if(!afkManager.isAfk(Bukkit.getPlayer(users))) {
                            //No longer AFK, add their start time
                            String timeStamp = new SimpleDateFormat("dd:HH:mm:ss").format(Calendar.getInstance().getTime());
                            String startedAfkDate = timeBeenAwayStart.get(users);
                            String addToTime = "";
                            try {
                                Date currentTime = new SimpleDateFormat("dd:HH:mm:ss").parse(timeStamp);
                                Date afkStartDate = new SimpleDateFormat("dd:HH:mm:ss").parse(startedAfkDate); //This [1]
                                String nowTime[] = timeStamp.split(":");
                                String startTime[] = timeBeenAwayStart.get(users).split(":");
                                long currentDay = TimeUnit.DAYS.toDays(System.currentTimeMillis());
                                long currentHour = TimeUnit.HOURS.toHours(System.currentTimeMillis());
                                long currentMinute = TimeUnit.MINUTES.toMinutes(System.currentTimeMillis());
                                long currentSeconds = TimeUnit.SECONDS.toSeconds(System.currentTimeMillis());
                                // How to get currentMillis of their startDate [1]
                                /* Trial 1 Test? */
                                if(referredPlayerInstances.containsKey(users)) {
                                    //They have an instance
                                    if(!addToTime.equals("")) {
                                        referredPlayerInstances.get(users).addToEndTime(addToTime);
                                    }
                                }
                                /**/
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }
    I think I fixed it and figured out an easier way!

    Code:
    for(UUID users : timeBeenAwayStart.keySet()) {
                        if(!afkManager.isAfk(Bukkit.getPlayer(users))) {
                            //No longer AFK, add their start time
                            long timeStartedAway = timeBeenAwayStart.get(users);
                            try {
                                /* Right now: */
                                long currentMillis = System.currentTimeMillis();
                                /* Trial 1 Test? */
                                if(referredPlayerInstances.containsKey(users)) {
                                    //They have an instance
                                    /* ChangedTimeMillis */
                                    long changedTimeMillis = currentMillis - timeStartedAway;
                                    //String newTime = new SimpleDateFormat("mm:ss:SSS").format(new Date(changedTimeMillis));
                                    referredPlayerInstances.get(users).addToEndTime(changedTimeMillis);
                                }
                                /**/
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }
    In my RefferedPlayerInstances
    Code:
        public void addToEndTime(long timeMillis) {
            this.endTimeMillis += timeMillis;
            this.endTime = new SimpleDateFormat("mm:ss:SSS").format(new Date(this.endTimeMillis));
        }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Aug 5, 2017
Thread Status:
Not open for further replies.

Share This Page