Help formating

Discussion in 'Spigot Plugin Development' started by Lovis, Aug 14, 2018.

  1. int sec = 7200 % 60;
    int min = 7200 / 60;
    int hr = min / 60;
    int day = hr / 24;
    int week = day / 7;
    int year = week / 52;

    That's right?
     
  2. Here is conflict

    example:
    2h 120m 0s

    or

    5h 300m 0s

    what he receives from the hours, he receives the minutes too
    Help me please
     
  3. ?

    You defined min as 7200 / 60 for example. Then you made a variable called hour which is min / 60. Of course, whatever you put in min will then pass through to hour variable.

    Anyway, let's see some code from your classes so we can assist you better.
     
  4. Code (Java):
    String format(int second) {
        String _second = Integer.toString(second % 60);
        second = second / 60;
        String _minute = Integer.toString(second % 60);
        second = second / 60;
        String _hour = Integer.toString(second % 24);
        second = second / 24;
        String _day = Integer.toString(second % 7);
        second = second / 7;
        String _week = Integer.toString(second % 52);
        second = second / 52;
        String _year = Integer.toString(second);
        return _year + " years " + _week + " weeks " + _day + " days " + _hour + " hours " + _minute + " minutes " + _second + " seconds";
    }
     
  5. What your code is bringing out is that you are converting a given amount of seconds into minutes and hours and days and so on. If you pass 18,000 seconds you get 18000s in minutes, 18000s in hours but you want the number of seconds displayed in a proper time format.

    Instead of working forwards. seconds -> minutes -> hours -> day -> week -> year. I'd suggest to work backwards.

    Start with int years = seconds % 31449600; If (year < 1D) then you use int weeks = seconds % 604800; If you still can't get a value of 1 then keep going lower and lower. If you have something like 18,000 seconds.

    You'll reach eventually int minutes = 18000 % 3600 which is 5 minutes. Then you can easily display 5 minutes as your string.

    If you get to the point where you get a value more than 1, in this case, our minutes was 5, you divide the number normally, take the remainder, get its value in seconds and repeat the process.

    This is obviously a very slow method but I hope it highlights my point.
     
  6. I did not understand, could you do more or less for me to understand?