相隔、相差幾天--演算法

思而後行發表於2017-11-13
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

public class Test {
    // 1秒鐘 的毫秒數
    public static long sec_milliseconds = 1000L * 1;
    // 1分鐘的毫秒數
    public static long min_milliseconds = sec_milliseconds * 60;
    // 1小時的毫秒數
    public static long hour_milliseconds = min_milliseconds * 60;
    // 1天的 毫秒數
    public static long day_milliseconds = hour_milliseconds * 24;

    /**
     * 可購買一元只換不修時間區間: [16,45]
     */
    private static final int PERIOD_START = 16;

    private static final int PERIOD_END = 45;

    /**
     * 暫定 一元只換不修權益 PackageType為 6
     */
    private static final int PACKAGE_TYPE = 6;

    /**
     * 尚未擁有購買許可權
     */
    public static final int STATUS_NO_RIGHTS = 1;
    /**
     * 已購買
     */
    public static final int STATUS_PAID = 2;
    /**
     * 未購買
     */
    public static final int STATUS_UNPAID = 3;
    /**
     * 已失去購買許可權
     */
    public static final int STATUS_EXPIRED = 4;

    /**
     * <b>
     *     計算距離裝置首次登入會員過去多少天;精確到毫秒<br>
     * </b>
     *
     * @param current_timestamp
     * @param firstLoginTimestamp
     * @return
     */
    public static int milliseconds2day(long current_timestamp, long firstLoginTimestamp) {

        if (current_timestamp < firstLoginTimestamp) {
            System.out.println("Parameter Error!  本地時間 < 首次登入時間");
            return 0;
        }

        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        sdf1.setTimeZone(TimeZone.getTimeZone("GMT+8"));
        String beginDate = sdf1.format(firstLoginTimestamp);
        String endDate = sdf1.format(current_timestamp);

        return milliseconds2day(beginDate, endDate);
    }

    public static int milliseconds2day(String loginDate, String currentDate) {

        int day = 0;

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        TimeZone time_zone = TimeZone.getTimeZone("GMT+8");
        sdf.setTimeZone(time_zone);

        Date date1;
        Date date2;
        try {
            date1 = sdf.parse(loginDate);
            date2 = sdf.parse(currentDate);
            day = (int) ((date2.getTime() - date1.getTime()) / Test.day_milliseconds);
            day++;
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return day;
    }

    public static String[] calculateValidPurchaseDate(String pattern, long firstLoginTimestamp) {

            TimeZone time_zone = TimeZone.getTimeZone("GMT+8");

            String[] date = new String[2];

            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(firstLoginTimestamp);
            calendar.setTimeZone(time_zone);

            long offset = calendar.get(Calendar.HOUR_OF_DAY) *  hour_milliseconds
                    + calendar.get(Calendar.MINUTE) * min_milliseconds
                    + calendar.get(Calendar.SECOND) * sec_milliseconds
                    + calendar.get(Calendar.MILLISECOND);

            System.out.println("offset=" + offset);

            /**
             * fixbug: 購買開始時間 為第16天的 00:00:00.000
             */
            long start_milliseconds = firstLoginTimestamp + ((PERIOD_START - 1) * day_milliseconds - offset);

            long expire_milliseconds = firstLoginTimestamp + (PERIOD_END * day_milliseconds - offset - 1);


            SimpleDateFormat sdf = new SimpleDateFormat(pattern);
            sdf.setTimeZone(time_zone);

            date[0] = sdf.format(new Date(start_milliseconds));

            date[1] = sdf.format(new Date(expire_milliseconds));
            return date;
    }

    public static void main(String[] args) {
        
        //2017-10-01 00:00:00          1506787200000L

        try {

            System.out.println(milliseconds2day("2017-10-01 00:00:00","2017-10-16 00:00:00") + " days");
            
            System.out.println(new Date(1506787201000L));
            
            String[] valid_date = calculateValidPurchaseDate("yyyy-MM-dd HH:mm:ss", 1506787200000L);
            System.out.println(valid_date[0] + ", " + valid_date[1]);
            
            System.out.println(new Date(1510278073920L));
            
            System.out.println(new Date(1510278079375L));
            
        } catch (/* Parse */Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}


相關文章