将剩余的十进制数转换为周,日,小时,分钟,秒

时间:2014-01-11 19:37:21

标签: java math plugins minecraft

现在我发现25%的人年,例如,如果你有5年25%是1.25。虽然插件我不能从你身上移除1.25年,但.25需要转换为几周,然后任何剩余时间转换为几天等等。虽然我不知道如何转换这些时间。

Integer y = itapi.getPlayerYears(player.getName());
Double yremove = Integer.valueOf(y) * 0.25;
Integer w = itapi.getPlayerWeeks(player.getName());
Double wremove = Integer.valueOf(w) * 0.25;
Integer d = itapi.getPlayerDays(player.getName());
Double dremove = Integer.valueOf(d) * 0.25;
Integer h = itapi.getPlayerHours(player.getName());
Double hremove = Integer.valueOf(h) * 0.25;
Integer m = itapi.getPlayerMinutes(player.getName());
Double mremove = Integer.valueOf(m) * 0.25;
Integer s = itapi.getPlayerSeconds(player.getName());
Double sremove = Integer.valueOf(s) * 0.25;
String yminus = String.valueOf(yremove) + 'y';
String wminus = String.valueOf(wremove) + 'w';
String dminus = String.valueOf(dremove) + 'd';
String hminus = String.valueOf(hremove) + 'h';
String mminus = String.valueOf(mremove) + 'm';
String sminus = String.valueOf(sremove) + 's';
ItemStack book = itapi.createTimeCard("Death of " + player.getName(), yminus + wminus + dminus + hminus + mminus + sminus, 1);
itapi.removeTime(player.getName(), yminus + wminus + dminus + hminus + mminus + sminus );
e.getDrops().add(book);

是否有可能将转换工作转出,或者将所有时间转换为秒然后再转换为25%并将其转换回来会更好吗?

3 个答案:

答案 0 :(得分:1)

我会使用嵌套的ifs并传递余数。我没有对此进行测试,但它应该给你一个想法。

Integer y = itapi.getPlayerYears(player.getName());
double yremove = Integer.valueOf(y) *0.25;
double numWeeks = yremove * 52; //returns the number in weeks
double numDays =0;
double numHours =0;
double numMinutes =0;
double numSeconds =0;
if(numWeeks % 52 != 0){
    numDays = (numWeeks % 52) * 7;
    if(numDays % 7 !=0){
          numHours = (numDays % 7) * 24;
          if(numHours % 24 !=0){
              numMinutes = (numHours % 24) * 60;
              if(numMinutes % 60 !=0){
                   numSeconds = (numMinutes % 60) * 60;
              }
          }
    }
}
//... then convert to string as you are already doing and pass it to removeTime()

答案 1 :(得分:0)

您可以使用Calendar课程为您计算,例如:

    Calendar c = Calendar.getInstance();

    double millis = 1.25 * 31557600 * 1000;
    long l = (long) millis;
    c.setTimeInMillis(l);

    System.out.println(c.get(Calendar.YEAR) + "  Year");
    System.out.println(c.get(Calendar.MONTH) + " months");
    System.out.println(c.get(Calendar.WEEK_OF_MONTH) + " weeks");
    System.out.println(c.get(Calendar.DAY_OF_MONTH) + " days");
    System.out.println(c.get(Calendar.HOUR) + " hours");
    System.out.println(c.get(Calendar.MINUTE) + " minutes");
    System.out.println(c.get(Calendar.SECOND) + " seconds");

<强>输出:     1971年     3个月     1周     2天     7个小时     0分钟     0秒

日历将起始年份默认为1970年,如果需要,您可以进一步操纵年份和月份到数周。

答案 2 :(得分:0)

创建一个类来管理时间。本课程将提供以年,周,月等方式返回时间的方法。

public class Time{
    private long milliseconds;

    public double getSeconds(){
        double seconds = milliseconds/1000.0;
        return seconds;
    }
    public void subtractSeconds(double seconds){
        long millisInSeconds = (long)(seconds*1000);
        this.millisecionds -= millisInSeconds;
    }
    //write more methods for years, months etc.
}

然后,使用此类检索年,月,周并减去差异。这将使您的代码保持清洁,易于理解。

Time time = new Time(1000*60*60);
int years = (int)time.getYears();
time.subtractYears(years);
int months = (int)time.getMonths();
time.subtractMonths(months);