相隔、相差几天--算法

xiaoxiao2021-02-28  70

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();         }     } }
转载请注明原文地址: https://www.6miu.com/read-1450351.html

最新回复(0)