Preface:

Among programmers, zero-crossing is often involved in resetting player data. At present, when I am doing game development, I happen to come across a function module to prevent addiction, which involves the judgment of accumulated time. If the reset time is zero, the underlying library can be suspended directly. Currently, the game is set to reset the player data at 4 am, so it will involve the problem of determining whether it is the same day and getting the next reset.

Although they are small and simple problems, most of them are estimated to come out as soon as they are written, so my article here is still a note, just as the tool code is written down

Customize the reset time to get the reset time after a few days (the next few times)

The following code

public static int getHour(Calendar calendar) { return calendar.get(Calendar.HOUR_OF_DAY); } public static long getFutureResetMills(long mills, int resetHour){ return getFutureResetMills(mills, resetHour, 1); } /** * custom reset time, get the reset time after a few days * example * day: 1: curMills: 1605081716747(2020-11-11 16:01:56), mills: 1605124800000(2020-11-12 04:00:00) * day: 1 : curMills: 1605034209000(2020-11-11 02:50:09), mills: 1605038400000(2020-11-11 04:00:00) * day: 1 : curMills: 1605024000000(2020-11-11 00:00:00), mills: 1605038400000(2020-11-11 04:00:00) * day: 1 : curMills: 1605023940000(2020-11-10 23:59:00), mills: 1605038400000(2020-11-11 04:00:00) * day: 1 : curMills: 1605038340000(2020-11-11 03:59:00), mills: 1605038400000(2020-11-11 04:00:00) * * @Param Mills Current Time * @Param Resethour Reset Hour (24h), default 4 * @Param Day, number of times, Public static long getFutureSetMills (long mills, int resetHour, int resetHour) int day) { Calendar calendar = Calendar.getInstance(); Date date = new Date(); if (mills > 0) { date.setTime(mills); } calendar.setTime(date); int curHour = getHour(calendar); if (curHour >= resetHour) { calendar.add(Calendar.DAY_OF_YEAR, day); } calendar.set(Calendar.HOUR_OF_DAY, resetHour); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MILLISECOND, 0); return calendar.getTimeInMillis(); }

Gets the timestamp for the next reset

It’s actually the top one

public static long getFutureResetMills(long mills, int resetHour){
    return getFutureResetMills(mills, resetHour, 1);
} 

Of course, you can also set a different name

/** * customize reset time to get the next reset timestamp * <p> * For example * Curmills: 1605079957880(2020-11-11 15:32:37), mills: 1605124800000(2020-11-12 04:00:00) * curMills: 1605034209000(2020-11-11 02:50:09), mills: 1605038400000(2020-11-11 04:00:00) * curMills: 1605024000000(2020-11-11 00:00:00), mills: 1605038400000(2020-11-11 04:00:00) * curMills: 1605023940000(2020-11-10 23:59:00), mills: 1605038400000(2020-11-11 04:00:00) * curMills: 1605038340000(2020-11-11 03:59:00), mills: 1605038400000(2020-11-11 04:00:00) * * @Param Mills Current Time * @Param Resethour Reset Hour (24h), Public static long getNextResetMills(long mills, long mills, int resetHour) { return getFutureResetMills(mills, resetHour); }

Customize the reset time to determine if it is the same day

Simply get the next reset time of the two timestamps and determine whether they are the same or not

/** * ReseThour: 4: * Amills: 1605038340000(2020-11-11 03:59:00) * BMills: 1605038400000(2020-11-11 04:00:00) - same day: false * <p> * resetHour: 4: * aMills: 1605024000000(2020-11-11 00:00:00) * bMills: 1605020400000(2020-11-10 23:00:00) - same day: True * * @param aMills long * @param bMills long * @param resetHour reset time (24h) * @return Boolean: Public static Boolean isSameday (long aMills, long bMills, int resetHour) {return getNextResetMills(aMills, int resetHour); resetHour) == getNextResetMills(bMills, resetHour); }

This is the note taken yesterday. Thank you for your reference

If you have any questions, you are welcome to correct me

thank you

:)

Starting link