相隔、相差幾天--演算法
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();
}
}
}
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();
}
}
}
相關文章
- sql server 計算兩個時間 相差的 幾天幾時幾分幾秒SQLServer
- JavaScript計算兩個日期相差天數JavaScript
- JavaScript 計算兩個時間相差天數JavaScript
- javascript計算兩個日期相差的天數JavaScript
- Calendar:計算兩個日期相差的天數
- C語言實現,計算兩天之間相差的天數C語言
- js獲取兩個時間相差的天數JS
- 計算兩個日期相差的天數,Calendar用法
- php 計算兩個日期之間相差多少天PHP
- javascript計算兩個時間日期相差的天數JavaScript
- Java8 學習之計算日期相差天數Java
- 計算兩個時間點相差天數例項程式碼
- javascript計算兩個日期相差的天數程式碼例項JavaScript
- 加入distinct 和不加入執行時間相差幾千倍
- js計算指定日期幾天前或者幾天後的日期JS
- java獲取日期差以及幾天前和幾天後的時間Java
- js計算兩個日期相差的天數(不包含小時分鐘秒)JS
- 相差數十倍的SQL效能分析(r11筆記第98天)SQL筆記
- 計算兩個時間日期相差的天數、分鐘數或者秒數
- 相差最大;及逆序分析
- taobao網競猜遊戲,幾年幾月到現在是幾天。遊戲
- 東北公司使用新套件將幾周變成幾天套件
- 計算兩個NSDate之間,相隔多少秒數
- Vista與XP安全性相差無幾 僅個別專案佔優勢
- 這幾天找工作的經歷
- 這幾天遇到的 jQuery 問題jQuery
- 表的欄位名轉為逗號相隔的字串字串
- 相差甚遠;及Magic Index分析Index
- 在Oracle中取得某一天前後幾天的字串的方法Oracle字串
- 前幾天在找實習,所以就空了三天沒寫部落格
- 如何用Python投機倒把幾天“暴富”Python
- PLSQL實現顯示當天是星期幾SQL
- JS 時間戳轉換成幾天前JS時間戳
- 最近幾天做oracle stream遇到很多問題Oracle
- 指定刪除幾天前的索引資料索引
- 須彌之鏡;及相差甚遠分析
- 幾種常用的排序演算法排序演算法
- Python幾種加密演算法Python加密演算法