2

I have the following two methods:

private long getTimeInMilliseconds()
    {
        Calendar c = Calendar.getInstance();


        if(c.get(Calendar.DAY_OF_MONTH) == 21)  
        {
            c.set(Calendar.MONTH, Calendar.MONTH + 1 );
            c.set(Calendar.DAY_OF_MONTH, 1);
        }
        else
            c.set(Calendar.DAY_OF_MONTH, Calendar.DAY_OF_MONTH + 10);

        if(c.get(Calendar.MONTH) > 11)
            c.set(Calendar.MONTH, 0);

        return c.getTimeInMillis();
    }

public static void remainingTime(L2PcInstance player)
    {
        long now = System.currentTimeMillis();
        long then = player.getExpBoostTime();

        long time = then - now;

        int hours = (int) (time / 3600000);

        player.sendMessage(hours+ " hours remaining until your EXP BOOST PERIOD ends");
    }

I want getTimeInMillisSeconds() to return the time 10 days later. I want remainingTime() to show how many days (in hours) remain.

With the code above, it shows 4 days remaining and not 10.

Can anybody help?

4

2 回答 2

3

你在方法上犯了一个错误set()

它应该是

c.set(Calendar.DAY_OF_MONTH, c.get(Calendar.DAY_OF_MONTH) + 10);

但是,您的方法远非最佳。在另一个答案中建议的那个(将 10 * 24 * 60 * 60 * 1000 毫秒添加到当前时间)要好得多恕我直言。

于 2012-06-11T18:29:06.970 回答
1

获得“从现在起十天”的最佳方法是使用时间戳/毫秒。

您可以从这样的日历中获取当前时间(以毫秒为单位):

Calendar someCalendar = new Calendar();
long someTimestamp = someCalendar.getTimeInMillis();

一旦你得到它,你可以增加十天(同样以毫秒为单位):

long tenDays = 1000 * 60 * 60 * 24 * 10;
long tenDaysFromNow = someTimestamp + tenDays;
于 2012-06-11T18:28:58.110 回答