哈哈 今天周末有时间,再整理一篇博文上来,虽然已经不做游戏老长时间了,但还是要把以前做过的东西总结一下,借此可以回顾以前的东西,也可以分享给大家。
今天说一下游戏中VIP会员模块的实现思路。每款游戏的消费体系中必不可缺少的一个模块就是VIP,VIP用户可以获得额外的奖励,比如每日登陆奖励,游戏中各个功能针对VIP和普通用户都有所差异,这样可以使游戏运作更加良好,如果一款商业游戏不赚钱,那么这款游戏的路肯定不好走。本文的VIP模块的类似于QQ会员的成长体系,大家可去QQ会员网站中看看。
接下来进入正题:
成长体系图
VIP经验等级成长方案
VIP经验等级成长方案配置表1 |
VIP经验等级成长方案配置表2 |
|||||
VIP等级 |
VIP经验 |
VIP经验区段 |
VIP等级 |
VIP经验 |
VIP经验区段 |
|
0 |
0 |
0 |
0 |
0 |
0 |
|
1 |
16 |
0-16 |
1 |
16 |
0-16 |
|
2 |
68 |
16-68 |
2 |
68 |
16-84 |
|
3 |
158 |
68-158 |
3 |
158 |
84-242 |
|
4 |
618 |
158-618 |
4 |
618 |
242-860 |
|
5 |
1236 |
618-1236 |
5 |
1236 |
860-2096 |
|
6 |
2472 |
1236-2472 |
6 |
2472 |
2096-4568 |
|
7 |
2472- |
7 |
4568- |
VIP会员成长值
会员成长值 = 每日成长值 + 购买赠送成长值 - 非会员成长值下降
l 每日成长值:VIP普通会员每日 +10点;VIP超级会员每日 +15点;
l 购买赠送成长点:既购买VIP产品赠送的成长点
l 非会员成长值下降:VIP会员过期之后每天下降 -10点
l 会员成长值超过VIP7上限之后将不会继续增加成长值
VIP会员类型
n VIP普通会员
n VIP超级会员
VIP普通会员(现有VIP普通会员时间大于0)
如何成为VIP普通会员:购买VIP产品(月卡、季卡、年卡)
延长VIP普通会员时间:= 现有VIP普通会员时间 + 购买VIP产品中配置的时间
VIP普通会员每天增加成长值 + 10点(配置)
VIP超级会员(现有VIP超级会员时间大于0)
如何成为VIP超级会员:现有VIP(普通)会员时间大于60天购买VIP产品(月卡、季卡、年卡),或直接购买VIP产品(季卡、年卡)
延长VIP超级会员时间:
如果现有VIP普通会员时间大于60,则 = 现有VIP普通会员时间 - 60 + 购买VIP产品中配置的时间
如果现有VIP普通会员时间等于0,购买了季卡或年卡,则 = 购买VIP产品中配置的时间 - 60
VIP超级会员每天增加成长值 + 15点(配置)
VIP经验成长的情况
用户上次登陆时间:lastLoginTime; 用户今天登陆时间:loginTime;
普通VIP结束时间:vipEndTime; 超级VIP结束时间:superVipEndTime;
第一种情况;VIP超级会员
第二种情况:VIP超级会员已过期,成为VIP普通会员
第三种情况:VIP超级会员、VIP普通会员都已过期
第四种情况:上次登陆前VIP超级会员过期
第五种情况:VIP普通会员已过期
第六种情况:上次登陆前VIP普通会员已过期
VIP经验等级计算
用户本次登陆与上次登陆时间之差:loginT = loginTime - lastLogin
VIP普通会员到期时间与登陆时间之差:vipEndTimeT = vipEndTime - loginTime;
VIP普通会员到期时间与上次登陆时间之差:lastVipEndTimeT = vipEndTime - lastLoginTime;
VIP超级会员到期时间与登陆时间之差:superVipEndTimeT = superVipEndTime - loginTime;
VIP超级会员到期时间与上次登陆时间之差:lastSuperVipEndTimeT = superVipEndTime - lastLoginTime;
VIP普通会员到期时间与VIP超级会员到期时间之差:vipBetweenSuperT = vipEndTime - superVipEndTime;
public static final int COMMON_VIPEXP_DAY_ADD_VALUE = 10;
public static final int SUPER_VIPEXP_DAY_ADD_VALUE = 15;
public static final int VIPEXP_DAY_REMOVE_VALUE = 10;
int loginTime = 0,//用户本次登陆时间
lastLoginTime = 0,//用户上次登陆时间
vipEndTime = 0,//普通VIP结束时间
superVipEndTime = 0;//超级VIP结束时间
int loginT,vipEndTimeT ,lastVipEndTimeT ,superVipEndTimeT ,lastSuperVipEndTimeT ,vipBteweenSuperT;
//用户本次登陆与上次登陆时间之差
loginT = loginTime - lastLoginTime;
//VIP普通会员到期时间与登陆时间之差
vipEndTimeT = vipEndTime - loginTime;
//VIP普通会员到期时间与上次登陆时间之差
lastVipEndTimeT = vipEndTime - lastLoginTime;
//VIP超级会员到期时间与登陆时间之差
superVipEndTimeT = superVipEndTime - loginTime;
//VIP超级会员到期时间与上次登陆时间之差
lastSuperVipEndTimeT = superVipEndTime - lastLoginTime;
//VIP普通会员到期时间与VIP超级会员到期时间之差
vipBteweenSuperT = vipEndTime - superVipEndTime;
//计算VIP经验点
long vipExp = 0l;
//上次登陆前VIP已过期
if(lastVipEndTimeT < 0){
vipExp = loginT*VIPEXP_DAY_REMOVE_VALUE*(-1);
//VIP已过期
}else if(vipEndTimeT < 0 && lastVipEndTimeT > 0 && lastSuperVipEndTimeT < 0){
vipExp = lastVipEndTimeT*COMMON_VIPEXP_DAY_ADD_VALUE + vipEndTimeT*VIPEXP_DAY_REMOVE_VALUE;
//VIP超级会员、VIP普通会员都已过期
}else if(vipEndTimeT < 0 && lastVipEndTimeT > 0 && superVipEndTimeT < 0 && lastSuperVipEndTimeT > 0){
vipExp = lastSuperVipEndTimeT*SUPER_VIPEXP_DAY_ADD_VALUE + vipBteweenSuperT*COMMON_VIPEXP_DAY_ADD_VALUE + vipEndTimeT*VIPEXP_DAY_REMOVE_VALUE;
//上次登陆前VIP超级会员过期
}else if(vipEndTimeT > 0 && lastSuperVipEndTimeT < 0){
vipExp = loginT*COMMON_VIPEXP_DAY_ADD_VALUE;
//VIP超级会员已过期,成为VIP普通会员
}else if(vipEndTimeT > 0 && superVipEndTimeT < 0 && lastSuperVipEndTimeT > 0){
vipExp = lastSuperVipEndTimeT*SUPER_VIPEXP_DAY_ADD_VALUE + superVipEndTimeT*COMMON_VIPEXP_DAY_ADD_VALUE*(-1);
//VIP超级会员
}else if(lastSuperVipEndTimeT > 0){
vipExp = loginT*SUPER_VIPEXP_DAY_ADD_VALUE;
}
VIP等级奖励
n 每日登陆奖励
n 特权奖励
随后补上源码
今天已经整理好了代码 贴上来供分享
demo目录图如下
package com.game.vip.data.user; /**
* @author Administrator
* 用户
*/
public class User {
/**
* 用户Id
*/
private long userId; /**
* 用户名
*/
private String name; private UserVipInfo userVipInfo; private UserLoginLog userLoginLog; public long getUserId() {
return userId;
} public void setUserId(long userId) {
this.userId = userId;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public UserVipInfo getUserVipInfo() {
return userVipInfo;
} public void setUserVipInfo(UserVipInfo userVipInfo) {
this.userVipInfo = userVipInfo;
} public UserLoginLog getUserLoginLog() {
return userLoginLog;
} public void setUserLoginLog(UserLoginLog userLoginLog) {
this.userLoginLog = userLoginLog;
}
}
package com.game.vip.data.user; /**
* @author Administrator
* 用户登陆日志
*/
public class UserLoginLog { /**
* 用户Id
*/
private long userId; /**
* 上次登陆时间
*/
private long lastLoginTime; public long getUserId() {
return userId;
} public void setUserId(long userId) {
this.userId = userId;
} public long getLastLoginTime() {
return lastLoginTime;
} public void setLastLoginTime(long lastLoginTime) {
this.lastLoginTime = lastLoginTime;
}
}
package com.game.vip.data.user; import com.game.vip.strategy.VipLevelStrategy; /**
* @author Administrator
* 用户Vip的信息
*/
public class UserVipInfo { public static final int VIP_STATUS_N = 0;
public static final int VIP_STATUS_Y = 1; /**
* 用户id
*/
private long userId; /**
* vip标志[0:普通用户;1:普通VIP会员;2:白金VIP会员]
*/
private int vipStatus; /**
* 当前VIP级别
*/
private int curVipLevel; /**
* 之前VIP级别
*/
private int preVipLevel; /**
* 当前VIP经验
*/
private long curVipExp; /**
* 升级到下一级所需的VIP经验
*/
private long nextLevelNeedExp; /**
* 普通VIP截止时间
*/
private long vipEndTime; /**
* 超级VIP截止时间
*/
private long superVipEndTime; /**
* VIP等级策略
*/
private VipLevelStrategy vipLevelStrategy; public void init(){
this.vipStatus = VIP_STATUS_N;
this.vipEndTime = 0;
this.superVipEndTime = 0;
this.curVipExp = 0;
this.curVipLevel = 0;
this.preVipLevel = 0;
this.nextLevelNeedExp = 0;
} public void toVipUser(){
this.vipStatus = VIP_STATUS_Y;
this.curVipExp = 0;
this.curVipLevel = this.vipLevelStrategy.getMinLevel();
this.preVipLevel = 0;
this.nextLevelNeedExp = this.vipLevelStrategy.getNextLevelNeedExp(this.curVipLevel, this.curVipExp);
} public long getUserId() {
return userId;
} public void setUserId(long userId) {
this.userId = userId;
} public int getVipStatus() {
return vipStatus;
} public void setVipStatus(int vipStatus) {
this.vipStatus = vipStatus;
} public int getCurVipLevel() {
return curVipLevel;
} public void setCurVipLevel(int curVipLevel) {
this.curVipLevel = curVipLevel;
} public int getPreVipLevel() {
return preVipLevel;
} public void setPreVipLevel(int preVipLevel) {
this.preVipLevel = preVipLevel;
} public long getCurVipExp() {
return curVipExp;
} public void setCurVipExp(long curVipExp) {
this.curVipExp = curVipExp;
} public long getNextLevelNeedExp() {
return nextLevelNeedExp;
} public void setNextLevelNeedExp(long nextLevelNeedExp) {
this.nextLevelNeedExp = nextLevelNeedExp;
} public VipLevelStrategy getVipLevelStrategy() {
return vipLevelStrategy;
} public void setVipLevelStrategy(VipLevelStrategy vipLevelStrategy) {
this.vipLevelStrategy = vipLevelStrategy;
} public long getVipEndTime() {
return vipEndTime;
} public void setVipEndTime(long vipEndTime) {
this.vipEndTime = vipEndTime;
} public long getSuperVipEndTime() {
return superVipEndTime;
} public void setSuperVipEndTime(long superVipEndTime) {
this.superVipEndTime = superVipEndTime;
}
}
package com.game.vip.data.vip.product; /**
* @author Administrator
* VIP卡(月卡,季卡,年卡)
*/
public class VipProduct { /**
* 产品id
*/
private long id; /**
* 产品名称
*/
private String name; /**
* VIP时间
*/
private int vipTime; /**
* 赠送VIP经验点
*/
private long vipExpGift; public VipProduct(long id, String name, int vipTime, long vipExpGift) {
super();
this.id = id;
this.name = name;
this.vipTime = vipTime;
this.vipExpGift = vipExpGift;
} public long getId() {
return id;
} public void setId(long id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public int getVipTime() {
return vipTime;
} public void setVipTime(int vipTime) {
this.vipTime = vipTime;
} public long getVipExpGift() {
return vipExpGift;
} public void setVipExpGift(long vipExpGift) {
this.vipExpGift = vipExpGift;
}
}
package com.game.vip.data.vip; /**
* @author Administrator
* vip等级配置信息
*/
public class VipLevelInfo { /**
* vip等级
*/
private int vipLevel; /**
* 对应的vip的经验点
*/
private long vipExp; public VipLevelInfo(int vipLevel, long vipExp) {
super();
this.vipLevel = vipLevel;
this.vipExp = vipExp;
} public int getVipLevel() {
return vipLevel;
} public void setVipLevel(int vipLevel) {
this.vipLevel = vipLevel;
} public long getVipExp() {
return vipExp;
} public void setVipExp(long vipExp) {
this.vipExp = vipExp;
}
}
package com.game.vip.data.vip; import java.util.List; /**
* @author Administrator
* vip等级所对应的奖励
*/
public class VipLevelReward { /**
* 类型[1:数值;2:道具;3:真假(0:假;1:真)]
*/
private int rewardType; /**
* 编码(如vip升级礼包,登陆礼包,充值礼包)
*/
private int rewardCode; /**
* 对应VIP等级
*/
private int vipLevel; /**
* 奖励名称
*/
private String name; /**
* 描述
*/
private String desc; public VipLevelReward(int vipLevel, String name, String desc) {
super();
this.vipLevel = vipLevel;
this.name = name;
this.desc = desc;
} /**
* 具体了奖励物品
*/
private List<Object> rewardList; public int getRewardType() {
return rewardType;
} public void setRewardType(int rewardType) {
this.rewardType = rewardType;
} public int getRewardCode() {
return rewardCode;
} public void setRewardCode(int rewardCode) {
this.rewardCode = rewardCode;
} public int getVipLevel() {
return vipLevel;
} public void setVipLevel(int vipLevel) {
this.vipLevel = vipLevel;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getDesc() {
return desc;
} public void setDesc(String desc) {
this.desc = desc;
} public List<Object> getRewardList() {
return rewardList;
} public void setRewardList(List<Object> rewardList) {
this.rewardList = rewardList;
} public boolean isEnable(int vipLevel){
return this.getVipLevel() == vipLevel;
}
}
package com.game.vip.manager.impl; import java.text.SimpleDateFormat;
import java.util.Date; import com.game.vip.data.user.User;
import com.game.vip.data.user.UserLoginLog;
import com.game.vip.data.user.UserVipInfo;
import com.game.vip.data.vip.VipLevelReward;
import com.game.vip.data.vip.product.VipProduct;
import com.game.vip.manager.VipManager;
import com.game.vip.strategy.VipLevelStrategy;
import com.game.vip.util.DateUtil; /**
* @author Administrator
* vip管理实现类1
*/
public class VipManagerImpl1 implements VipManager{ @Override
public void userToVipUser(User user) {
System.out.println("普通会员转成VIP会员");
UserVipInfo userVipInfo = user.getUserVipInfo();
//设置vipStatus
userVipInfo.toVipUser();
} @Override
public void userLogin(User user) {
System.out.println("------------------------用户登陆 Start----------------------------");
try{
if(user.getUserVipInfo().getVipStatus() == UserVipInfo.VIP_STATUS_N){
System.out.println("当前用户:" + user.getName());
System.out.println("当前用户不是VIP用户!");
return;
}
UserLoginLog loginLog = user.getUserLoginLog();
UserVipInfo userVipInfo = user.getUserVipInfo();
System.out.println("当前用户:" + user.getName());
System.out.println("当前VIP等级:" + userVipInfo.getCurVipLevel());
System.out.println("当前VIP经验:" + userVipInfo.getCurVipExp());
System.out.println("VIP普通会员到期时间:" + DateUtil.format(userVipInfo.getVipEndTime()));
System.out.println("VIP超级会员到期时间:" + DateUtil.format(userVipInfo.getSuperVipEndTime()));
System.out.println("当前升级到下一级所需要的VIP经验:" + userVipInfo.getNextLevelNeedExp());
System.out.println("上次登陆时间:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(loginLog.getLastLoginTime()));
long updateVipExp = getUpdateVipExp(loginLog, userVipInfo);
System.out.println("更新VIP信息");
System.out.println("VIP经验成长值:" + updateVipExp);
updateUserVipExp(updateVipExp, userVipInfo);
System.out.println("当前VIP等级:" + userVipInfo.getCurVipLevel());
System.out.println("当前VIP经验:" + userVipInfo.getCurVipExp());
System.out.println("当前升级到下一级所需要的VIP经验:" + userVipInfo.getNextLevelNeedExp());
loginLog.setLastLoginTime(new Date().getTime());
}catch(Exception e){
}finally{
System.out.println("------------------------用户登陆 End----------------------------");
} } @Override
public void rewardVipUser(User user) {
System.out.println("------------------------奖励VIP用户 Start----------------------------");
try{
if(user.getUserVipInfo().getVipStatus() == UserVipInfo.VIP_STATUS_N || user.getUserVipInfo().getVipEndTime() < DateUtil.getNowTime()){
System.out.println("当前用户不是VIP用户或VIP已过期!");
return;
}
UserVipInfo userVipInfo = user.getUserVipInfo();
VipLevelReward reward = userVipInfo.getVipLevelStrategy().getLevelReward(userVipInfo.getCurVipLevel());
if(reward != null){
System.out.println("当前用户:" + user.getName());
System.out.println("VIP等级:" + user.getUserVipInfo().getCurVipLevel());
System.out.println("获取VIP奖励:" + reward.getName() + "(" +reward.getDesc() + ")");
}else{
System.out.println("还没有设置VIP奖品");
}
}catch(Exception e){
}finally{
System.out.println("------------------------奖励VIP用户 End----------------------------");
} } @Override
public void buyVipProduct(User user, VipProduct vipProduct) {
System.out.println("------------------------购买VIP产品 Start----------------------------");
try{
//普通用户转为VIP会员
//第一步初始化会员信息
userToVipUser(user);
//第二步更新会员时间
long updateVipTime = DateUtil.getTimeByDays(vipProduct.getVipTime());
updateUserVipTime(updateVipTime, user.getUserVipInfo());
//第三步赠送相应的VIP经验
if(vipProduct.getVipExpGift() > 0){
System.out.println("赠送VIP经验点:" + vipProduct.getVipExpGift());
updateUserVipExp(vipProduct.getVipExpGift(), user.getUserVipInfo());
System.out.println("当前VIP等级:" + user.getUserVipInfo().getCurVipLevel());
System.out.println("当前VIP经验:" + user.getUserVipInfo().getCurVipExp());
System.out.println("当前升级到下一级所需要的VIP经验:" + user.getUserVipInfo().getNextLevelNeedExp());
}
}catch(Exception e){
}finally{
System.out.println("------------------------购买VIP产品 End----------------------------");
}
} /**
* 更新用户VIP的信息
* @param updateVipExp
* @param userVipInfo
*/
@Override
public void updateUserVipExp(long updateVipExp, UserVipInfo userVipInfo){
System.out.println("更新用户VIP经验信息");
VipLevelStrategy vipLevelStrategy = userVipInfo.getVipLevelStrategy();
System.out.println("用户所使用的VIP等级策略:" + vipLevelStrategy.getClass().getSimpleName());
long vipExp = vipLevelStrategy.getExpByLevel(userVipInfo.getCurVipLevel(), userVipInfo.getCurVipExp());
vipExp += updateVipExp;
long curExp = vipLevelStrategy.getCurExpByVipExp(vipExp);
int curLevel = vipLevelStrategy.getLevelByExp(vipExp);
long nextLevelNeedExp = vipLevelStrategy.getNextLevelNeedExp(curLevel, curExp);
System.out.println("更新前VIP经验:" + userVipInfo.getCurVipExp());
System.out.println("更新前VIP等级:" + userVipInfo.getCurVipLevel());
int curVipLevel = userVipInfo.getCurVipLevel();
int preVipLevel = userVipInfo.getPreVipLevel();
if(curLevel != curVipLevel){
preVipLevel = curVipLevel;
curVipLevel = curLevel;
}
curExp = (curExp < 0)? 0 : curExp;
nextLevelNeedExp = (nextLevelNeedExp < 0)? 0 : nextLevelNeedExp; System.out.println("更新后VIP经验:" + curExp);
System.out.println("更新后VIP等级:" + curLevel);
userVipInfo.setCurVipExp(curExp);
userVipInfo.setNextLevelNeedExp(nextLevelNeedExp);
userVipInfo.setCurVipLevel(curVipLevel);
userVipInfo.setPreVipLevel(preVipLevel); } /**
* 更新用户VIP的信息
* VIP普通会员(现有VIP普通会员时间大于0)
* 如何成为VIP普通会员:购买VIP产品(月卡、季卡、年卡)
* 延长VIP普通会员时间:= 现有VIP普通会员时间 + 购买VIP产品中配置的时间
* VIP超级会员(现有VIP超级会员时间大于0)
* 如何成为VIP超级会员:现有VIP(普通)会员时间大于60天购买VIP产品(月卡、季卡、年卡),或直接购买VIP产品(季卡、年卡)
* 延长VIP超级会员时间:
* 如果现有VIP普通会员时间大于60,则 = 现有VIP普通会员时间 - 60 + 购买VIP产品中配置的时间
* 如果现有VIP普通会员时间等于0,购买了季卡或年卡,则 = 购买VIP产品中配置的时间 - 60
* @param updateVipTime
* @param userVipInfo
*/
@Override
public void updateUserVipTime(long updateVipTime, UserVipInfo userVipInfo){
System.out.println("更新用户VIP时间信息");
long curVipEndTime = userVipInfo.getVipEndTime();
long curSuperVipEndTime = userVipInfo.getSuperVipEndTime();
long nowTime = DateUtil.getNowTime();
int updateDays = DateUtil.getDays(updateVipTime);
System.out.println("购买的VIP卡的天数:" + updateDays);
//初始化VIP到期时间为当前时间
if((curVipEndTime == 0 && curSuperVipEndTime == 0) || curVipEndTime < nowTime){
curVipEndTime = nowTime;
curSuperVipEndTime = nowTime;
}
System.out.println("更新前VIP普通会员时间:" + DateUtil.format(curVipEndTime));
System.out.println("更新前VIP超级会员时间:" + DateUtil.format(curSuperVipEndTime));
//VIP超级会员时间
//第一种情况VIP普通会员已过期再购卡的,如果买的是季卡或年卡则增加VIP超级会员时间
if(curVipEndTime == nowTime && updateDays > 60){
curSuperVipEndTime = DateUtil.getNDayTimeByNow(updateDays - 60);
//第二种情况VIP普通会员未过期再购卡,且VIP普通会员到期时间大于60天
}else if(DateUtil.getBetweenDays(curVipEndTime) > 60){
curSuperVipEndTime = curVipEndTime - DateUtil.getTimeByDays(60) + updateVipTime;
}
//VIP普通会员时间
curVipEndTime += updateVipTime; System.out.println("更新后VIP普通会员时间:" + DateUtil.format(curVipEndTime));
System.out.println("更新后VIP超级会员时间:" + DateUtil.format(curSuperVipEndTime)); userVipInfo.setVipEndTime(curVipEndTime);
userVipInfo.setSuperVipEndTime(curSuperVipEndTime);
} private long getUpdateVipExp(UserLoginLog loginLog, UserVipInfo userVipInfo){
long loginTime = new Date().getTime(),//用户本次登陆时间
lastLoginTime = loginLog.getLastLoginTime(),//用户上次登陆时间
vipEndTime = userVipInfo.getVipEndTime(),//普通VIP结束时间
superVipEndTime = userVipInfo.getSuperVipEndTime();//超级VIP结束时间 int loginT,vipEndTimeT ,lastVipEndTimeT ,superVipEndTimeT ,lastSuperVipEndTimeT ,vipBteweenSuperT;
//用户本次登陆与上次登陆时间之差
loginT = DateUtil.getBetweenDays(loginTime, lastLoginTime);
//VIP普通会员到期时间与登陆时间之差
vipEndTimeT = DateUtil.getBetweenDays(vipEndTime, loginTime);
//VIP普通会员到期时间与上次登陆时间之差
lastVipEndTimeT = DateUtil.getBetweenDays(vipEndTime, lastLoginTime);
//VIP超级会员到期时间与登陆时间之差
superVipEndTimeT = DateUtil.getBetweenDays(superVipEndTime, loginTime);
//VIP超级会员到期时间与上次登陆时间之差
lastSuperVipEndTimeT = DateUtil.getBetweenDays(superVipEndTime, lastLoginTime);
//VIP普通会员到期时间与VIP超级会员到期时间之差
vipBteweenSuperT = DateUtil.getBetweenDays(vipEndTime, superVipEndTime); //计算VIP经验点
long vipExp = 0l;
//上次登陆前VIP已过期
if(lastVipEndTimeT < 0){
vipExp = loginT*VIPEXP_DAY_REMOVE_VALUE*(-1);
//VIP已过期,上次登陆VIP超级会员已过期
}else if(vipEndTimeT < 0 && lastVipEndTimeT > 0 && lastSuperVipEndTimeT < 0){
vipExp = lastVipEndTimeT*COMMON_VIPEXP_DAY_ADD_VALUE + vipEndTimeT*VIPEXP_DAY_REMOVE_VALUE;
//VIP超级会员、VIP普通会员都已过期
}else if(vipEndTimeT < 0 && lastVipEndTimeT > 0 && superVipEndTimeT < 0 && lastSuperVipEndTimeT > 0){
vipExp = lastSuperVipEndTimeT*SUPER_VIPEXP_DAY_ADD_VALUE + vipBteweenSuperT*COMMON_VIPEXP_DAY_ADD_VALUE + vipEndTimeT*VIPEXP_DAY_REMOVE_VALUE;
//VIP普通会员未过期,上次登陆前VIP超级会员过期
}else if(vipEndTimeT > 0 && lastSuperVipEndTimeT < 0){
vipExp = loginT*COMMON_VIPEXP_DAY_ADD_VALUE;
//VIP超级会员已过期,成为VIP普通会员
}else if(vipEndTimeT > 0 && superVipEndTimeT < 0 && lastSuperVipEndTimeT > 0){
vipExp = lastSuperVipEndTimeT*SUPER_VIPEXP_DAY_ADD_VALUE + superVipEndTimeT*COMMON_VIPEXP_DAY_ADD_VALUE*(-1);
//VIP超级会员
}else if(lastSuperVipEndTimeT > 0){
vipExp = loginT*SUPER_VIPEXP_DAY_ADD_VALUE;
}
return vipExp;
} public static void main(String[] args) {
long lastLoginTime = DateUtil.getNDayTimeByNow(-13);
long vipEndTime = 0l;
long superVipEndTime = 0l; UserLoginLog loginLog = new UserLoginLog();
loginLog.setLastLoginTime(lastLoginTime); UserVipInfo userVipInfo = new UserVipInfo();
int type = 6;
switch(type){
case 1:
//上次登陆前VIP已过期
vipEndTime = DateUtil.getNDayTimeByNow(-14);
superVipEndTime = DateUtil.getNDayTimeByNow(-50);
break;
case 2:
//VIP已过期,上次登陆VIP超级会员已过期
vipEndTime = DateUtil.getNDayTimeByNow(-10);
superVipEndTime = DateUtil.getNDayTimeByNow(-50);
break;
case 3:
//VIP超级会员、VIP普通会员都已过期
vipEndTime = DateUtil.getNDayTimeByNow(-5);
superVipEndTime = DateUtil.getNDayTimeByNow(-10);
break;
case 4:
//VIP普通会员未过期,上次登陆前VIP超级会员过期
vipEndTime = DateUtil.getNDayTimeByNow(5);
superVipEndTime = DateUtil.getNDayTimeByNow(-20);
break;
case 5:
//VIP超级会员已过期,成为VIP普通会员
vipEndTime = DateUtil.getNDayTimeByNow(20);
superVipEndTime = DateUtil.getNDayTimeByNow(-5);
break;
case 6:
//VIP超级会员
vipEndTime = DateUtil.getNDayTimeByNow(10);
superVipEndTime = DateUtil.getNDayTimeByNow(40);
break;
} userVipInfo.setVipEndTime(vipEndTime);
userVipInfo.setSuperVipEndTime(superVipEndTime); VipManagerImpl1 vipManager = new VipManagerImpl1();
long updateVipExp = vipManager.getUpdateVipExp(loginLog, userVipInfo);
System.out.println(updateVipExp);
}
}
package com.game.vip.manager; import com.game.vip.data.user.User;
import com.game.vip.data.user.UserVipInfo;
import com.game.vip.data.vip.product.VipProduct; /**
* @author Administrator
* vip管理接口
*/
public interface VipManager { public static final int COMMON_VIPEXP_DAY_ADD_VALUE = 10;
public static final int SUPER_VIPEXP_DAY_ADD_VALUE = 15;
public static final int VIPEXP_DAY_REMOVE_VALUE = 10; /**
* 普通用户转换成VIP用户(如充值)
* @param userVipInfo
*/
public void userToVipUser(User user); /**
* 购买了vip季卡或月卡
* @param userVipInfo
* @param vipProduct
*/
public void buyVipProduct(User user, VipProduct vipProduct); /**
* 用户VIP经验成长
* @param loginLog
* @param userVipInfo
*/
public void userLogin(User user); /**
* 奖励VIP用户
* @param userVipInfo
*/
public void rewardVipUser(User user); /**
* 更新用户VIP的信息
* @param updateVipExp
* @param userVipInfo
*/
public void updateUserVipExp(long updateVipExp, UserVipInfo userVipInfo); /**
* 更新用户VIP的信息
* @param updateVipTime
* @param userVipInfo
*/
public void updateUserVipTime(long updateVipTime, UserVipInfo userVipInfo);
}
package com.game.vip.strategy.impl; import java.util.ArrayList;
import java.util.List; import com.game.vip.data.vip.VipLevelInfo;
import com.game.vip.strategy.VipLevelStrategy; /**
* @author Administrator
* VIP等级经验段级策略1
*/
public class VipLevelStrategyImpl1 extends VipLevelStrategy{ public VipLevelStrategyImpl1(List<VipLevelInfo> vipLevelList) {
super(vipLevelList);
} @Override
public List<LevelNeedVipExp> initLevelNeedVipExp(
List<VipLevelInfo> vipLevelList) {
List<LevelNeedVipExp> list = null;
long vipExpStart = 0l;
long vipExpEnd = 0l;
if(vipLevelList != null && vipLevelList.size()>0){
list = new ArrayList<LevelNeedVipExp>();
for(int i = 0, len = vipLevelList.size(); i < len; i++){
vipExpStart = vipLevelList.get(i).getVipExp();
vipExpEnd =(i == len-1)? vipLevelList.get(i).getVipExp() : vipLevelList.get(i+1).getVipExp();
list.add(new LevelNeedVipExp(i+1, vipExpStart, vipExpEnd));
}
}
return list;
} @Override
public long getExpByLevel(int vipLevel, long curVipExp) {
if(curVipExp>0)
return curVipExp;
long vipExp = 0l;
List<VipLevelInfo> vipLevelList = getVipLevelList();
if(vipLevelList != null && vipLevelList.size()>0){
for(int i = 0, len = vipLevelList.size(); i < len; i++){
if(vipLevel == vipLevelList.get(i).getVipLevel()){
vipExp = vipLevelList.get(i).getVipExp();
break;
}
}
}
return vipExp;
} @Override
public long getCurExpByVipExp(long vipExp) {
return vipExp;
} }
package com.game.vip.strategy.impl; import java.util.ArrayList;
import java.util.List; import com.game.vip.data.vip.VipLevelInfo;
import com.game.vip.strategy.VipLevelStrategy; /**
* @author Administrator
* VIP等级经验段级策略2
*/
public class VipLevelStrategyImpl2 extends VipLevelStrategy{ public VipLevelStrategyImpl2(List<VipLevelInfo> vipLevelList) {
super(vipLevelList);
} @Override
public List<LevelNeedVipExp> initLevelNeedVipExp(
List<VipLevelInfo> vipLevelList) {
List<LevelNeedVipExp> list = null;
long vipExpStart = 0l;
long vipExpEnd = 0l;
if(vipLevelList != null && vipLevelList.size()>0){
list = new ArrayList<LevelNeedVipExp>();
for(int i = 0, len = vipLevelList.size(); i < len; i++){
vipExpStart = vipExpByLevel(i, vipLevelList);
vipExpEnd = (i == len-1)? vipExpByLevel(i, vipLevelList) : vipExpByLevel(i+1, vipLevelList);
list.add(new LevelNeedVipExp(i+1, vipExpStart, vipExpEnd));
}
} return list;
} @Override
public long getExpByLevel(int vipLevel, long curVipExp) {
long levelExp = vipExpByLevel(vipLevel, getVipLevelList());
return levelExp + curVipExp;
} private long vipExpByLevel(int level, List<VipLevelInfo> vipLevelList){
long vipExp = 0l;
if(vipLevelList != null && vipLevelList.size()>0){
for(int i = 0, len = vipLevelList.size(); i < len; i++){
if(level < (i+1)) break;
vipExp += vipLevelList.get(i).getVipExp();
}
}
return vipExp;
} @Override
public long getCurExpByVipExp(long vipExp) {
int level = getLevelByExp(vipExp);
LevelNeedVipExp levelNeedVipExp = getLevelNeedVipExpByLevel(level);
if(levelNeedVipExp != null && vipExp >= levelNeedVipExp.getVipExpStart()){
return vipExp - levelNeedVipExp.getVipExpStart();
}
return 0;
}
}
package com.game.vip.strategy; import java.util.Collections;
import java.util.List; import com.game.vip.data.vip.VipLevelInfo;
import com.game.vip.data.vip.VipLevelReward; /**
* @author Administrator
* vip等级策略接口
*/
public abstract class VipLevelStrategy {
private List<VipLevelReward> rewardList;
private List<VipLevelInfo> vipLevelList;
private List<LevelNeedVipExp> levelNeedVipExpList; public VipLevelStrategy(List<VipLevelInfo> vipLevelList) {
super();
this.vipLevelList = vipLevelList;
this.levelNeedVipExpList = initLevelNeedVipExp(vipLevelList);
Collections.sort(this.levelNeedVipExpList);
} /**
* 通过VIP经验获取对应的等级
* @param vipExp
* @return
*/
public int getLevelByExp(long vipExp){
int level = getMinLevel();
List<LevelNeedVipExp> levelNeedVipExpList = getLevelNeedVipExpList();
LevelNeedVipExp levelNeedVipExp = null;
for (int i=0, len = levelNeedVipExpList.size(); i<len; i++) {
levelNeedVipExp = levelNeedVipExpList.get(i);
if(vipExp >= levelNeedVipExp.vipExpStart && vipExp < levelNeedVipExp.vipExpEnd){
level = levelNeedVipExp.level;
break;
}else if(i == len-1 && vipExp > levelNeedVipExp.vipExpEnd){
level = levelNeedVipExp.level;
}
}
return level;
} /**
* 获取VIP最大等级
* @return
*/
public int getMaxLevel(){
int len = 0;
List<LevelNeedVipExp> list = getLevelNeedVipExpList();
len = list.size();
return list.get(len-1).getLevel();
} /**
* 获取VIP最小等级
* @return
*/
public int getMinLevel(){
int len = 0;
List<LevelNeedVipExp> list = getLevelNeedVipExpList();
len = list.size();
return len > 0? list.get(0).getLevel() : 1;
} /**
* 是否升级
* @param curLevel
* @param curVipExp
* @return
*/
public boolean isGrowUp(int curLevel, long curVipExp){
long vipExp = getExpByLevel(curLevel, curVipExp);
int vipLevel = getLevelByExp(vipExp);
return vipLevel != curLevel;
} /**
* 升级到下一级所需的VIP经验
* @param curVipLevel
* @param curVipExp
* @return
*/
public long getNextLevelNeedExp(int curVipLevel, long curVipExp) {
long vipExp = getExpByLevel(curVipLevel, curVipExp);
long nextLevelExp = getExpByLevel(curVipLevel+1, 0l);
return nextLevelExp - vipExp;
} /**
* 通过VIP等级获取LevelNeedVipExp
* @param level
* @return
*/ public LevelNeedVipExp getLevelNeedVipExpByLevel(int level){
List<LevelNeedVipExp> list = getLevelNeedVipExpList();
for(LevelNeedVipExp item : list){
if(level == item.getLevel()){
return item;
}
}
return null;
} /**
* 根据VIP等级获取相应的奖励
* @param level
* @return
*/
public VipLevelReward getLevelReward(int level){
List<VipLevelReward> list = getRewardList();
if(list == null || list.size() == 0)
return null;
for(VipLevelReward reward : list){
if(reward.getVipLevel() == level){
return reward;
}
}
return null;
}
/**
* 通过总VIP经验获取当前等级所对应剩余经验
* @param vipExp
* @return
*/
public abstract long getCurExpByVipExp(long vipExp);
/**
* 通过VIP等级和当前VIP经验获取总VIP经验
* @param vipLevel
* @param curVipExp
* @return
*/
public abstract long getExpByLevel(int vipLevel, long curVipExp); /**
* @param vipLevelList
* @return
*/
public abstract List<LevelNeedVipExp> initLevelNeedVipExp(List<VipLevelInfo> vipLevelList); /**
* @author Administrator
* 每个等级所对应的vip经验区段
*/
public static class LevelNeedVipExp implements Comparable<LevelNeedVipExp>{
private int level;
private long vipExpStart;
private long vipExpEnd; public LevelNeedVipExp(int level, long vipExpStart, long vipExpEnd) {
super();
this.level = level;
this.vipExpStart = vipExpStart;
this.vipExpEnd = vipExpEnd;
} public int getLevel() {
return level;
}
public void setLevel(int level) {
this.level = level;
}
public long getVipExpStart() {
return vipExpStart;
}
public void setVipExpStart(long vipExpStart) {
this.vipExpStart = vipExpStart;
}
public long getVipExpEnd() {
return vipExpEnd;
}
public void setVipExpEnd(long vipExpEnd) {
this.vipExpEnd = vipExpEnd;
} @Override
public int compareTo(LevelNeedVipExp o) {
return this.level-o.level;
}
} public List<VipLevelInfo> getVipLevelList() {
return vipLevelList;
} public List<LevelNeedVipExp> getLevelNeedVipExpList() {
return levelNeedVipExpList;
} public List<VipLevelReward> getRewardList() {
return rewardList;
} public void setRewardList(List<VipLevelReward> rewardList) {
this.rewardList = rewardList;
}
}
package com.game.vip.test; import java.util.ArrayList;
import java.util.Date;
import java.util.List; import com.game.vip.data.user.User;
import com.game.vip.data.user.UserLoginLog;
import com.game.vip.data.user.UserVipInfo;
import com.game.vip.data.vip.VipLevelInfo;
import com.game.vip.data.vip.VipLevelReward;
import com.game.vip.data.vip.product.VipProduct;
import com.game.vip.manager.VipManager;
import com.game.vip.manager.impl.VipManagerImpl1;
import com.game.vip.strategy.VipLevelStrategy;
import com.game.vip.strategy.impl.VipLevelStrategyImpl1;
import com.game.vip.util.DateUtil; /**
* @author Administrator
* 测试类
*/
public class VipTest { public static void main(String[] args) {
VipManager vipManager = new VipManagerImpl1(); User user = initUser(); List<VipProduct> productList = initVipProduct();
//购买VIP卡[i = 0:月卡;i = 1:季卡;i = 2:年卡]
vipManager.buyVipProduct(user, productList.get(1));
user.getUserLoginLog().setLastLoginTime(new Date().getTime()); //1: 上次登陆前VIP已过期
//2: VIP已过期,上次登陆VIP超级会员已过期
//3: VIP超级会员、VIP普通会员都已过期
//4: VIP普通会员未过期,上次登陆前VIP超级会员过期
//5: VIP超级会员已过期,成为VIP普通会员
//6: VIP超级会员
int type = 1;
updateUserVipInfo(user, type);
//登陆
vipManager.userLogin(user); //奖励
vipManager.rewardVipUser(user);
} public static List<VipLevelInfo> initVipLevelInfo(){
List<VipLevelInfo> list = new ArrayList<VipLevelInfo>();
list.add(new VipLevelInfo(1, 0));
list.add(new VipLevelInfo(2, 16));
list.add(new VipLevelInfo(3, 68));
list.add(new VipLevelInfo(4, 158));
list.add(new VipLevelInfo(5, 618));
list.add(new VipLevelInfo(6, 1236));
list.add(new VipLevelInfo(7, 2472));
return list;
} public static List<VipProduct> initVipProduct(){
List<VipProduct> list = new ArrayList<VipProduct>();
list.add(new VipProduct(1, "月卡", 30, 10));
list.add(new VipProduct(1, "季卡", 90, 30));
list.add(new VipProduct(1, "年卡", 360, 50));
return list;
} public static List<VipLevelReward> initVipLevelReward(){
List<VipLevelReward> list = new ArrayList<VipLevelReward>();
list.add(new VipLevelReward(1, "1级VIP等级奖励", "奖励"));
list.add(new VipLevelReward(2, "2级VIP等级奖励", "奖励"));
list.add(new VipLevelReward(3, "3级VIP等级奖励", "奖励"));
list.add(new VipLevelReward(4, "4级VIP等级奖励", "奖励"));
list.add(new VipLevelReward(5, "5级VIP等级奖励", "奖励"));
list.add(new VipLevelReward(6, "6级VIP等级奖励", "奖励"));
list.add(new VipLevelReward(7, "7级VIP等级奖励", "奖励"));
return list;
} public static User initUser(){
User user = new User();
user.setUserId(1l);
user.setName("小学生三号"); //登陆日志
UserLoginLog loginLog = new UserLoginLog();
loginLog.setUserId(user.getUserId());
//13天之前登陆了
loginLog.setLastLoginTime(DateUtil.getNDayTimeByNow(-13)); //用户VIP信息
UserVipInfo userVipInfo = new UserVipInfo();
userVipInfo.setUserId(user.getUserId());
List<VipLevelInfo> vipLevelList = initVipLevelInfo();
VipLevelStrategy vipLevelStrategy1 = new VipLevelStrategyImpl1(vipLevelList);
// VipLevelStrategy vipLevelStrategy2 = new VipLevelStrategyImpl2(vipLevelList); //设置VIP等级奖励
vipLevelStrategy1.setRewardList(initVipLevelReward());
//设置VIP等级策略
userVipInfo.setVipLevelStrategy(vipLevelStrategy1); user.setUserLoginLog(loginLog);
user.setUserVipInfo(userVipInfo); return user;
} public static void updateUserVipInfo(User user, int type){
System.out.println("=========================");
long lastLoginTime = DateUtil.getNDayTimeByNow(-13);
long vipEndTime = 0l;
long superVipEndTime = 0l; UserLoginLog loginLog = user.getUserLoginLog();
loginLog.setLastLoginTime(lastLoginTime); UserVipInfo userVipInfo = user.getUserVipInfo();
switch(type){
case 1:
//上次登陆前VIP已过期
System.out.println("更新类型:上次登陆前VIP已过期!");
vipEndTime = DateUtil.getNDayTimeByNow(-14);
superVipEndTime = DateUtil.getNDayTimeByNow(-50);
break;
case 2:
//VIP已过期,上次登陆VIP超级会员已过期
System.out.println("更新类型:VIP已过期,上次登陆VIP超级会员已过期!");
vipEndTime = DateUtil.getNDayTimeByNow(-10);
superVipEndTime = DateUtil.getNDayTimeByNow(-50);
break;
case 3:
//VIP超级会员、VIP普通会员都已过期
System.out.println("更新类型:VIP超级会员、VIP普通会员都已过期!");
vipEndTime = DateUtil.getNDayTimeByNow(-5);
superVipEndTime = DateUtil.getNDayTimeByNow(-10);
break;
case 4:
//VIP普通会员未过期,上次登陆前VIP超级会员过期
System.out.println("更新类型:VIP普通会员未过期,上次登陆前VIP超级会员过期!");
vipEndTime = DateUtil.getNDayTimeByNow(5);
superVipEndTime = DateUtil.getNDayTimeByNow(-20);
break;
case 5:
//VIP超级会员已过期,成为VIP普通会员
System.out.println("更新类型:VIP超级会员已过期,成为VIP普通会员!");
vipEndTime = DateUtil.getNDayTimeByNow(20);
superVipEndTime = DateUtil.getNDayTimeByNow(-5);
break;
case 6:
//VIP超级会员
System.out.println("更新类型:VIP超级会员!");
vipEndTime = DateUtil.getNDayTimeByNow(10);
superVipEndTime = DateUtil.getNDayTimeByNow(40);
break;
}
System.out.println("更新上次登陆时间为:" + DateUtil.format(lastLoginTime));
System.out.println("更新VIP普通会员到期时间为:" + DateUtil.format(vipEndTime));
System.out.println("更新VIP超级会员到期时间为:" + DateUtil.format(superVipEndTime));
userVipInfo.setVipEndTime(vipEndTime);
userVipInfo.setSuperVipEndTime(superVipEndTime);
System.out.println("=========================");
}
}
package com.game.vip.util; import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar; public class DateUtil { public static final long ONE_DAY_TIME = 24*60*60*1000;
/**
* 获取指定日期凌晨的时间
* @param time
* @return
*/
public static long getZeroHourTimeOfDay(long time){
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(time);
int y = calendar.get(Calendar.YEAR);
int m = calendar.get(Calendar.MONTH);
int d = calendar.get(Calendar.DATE);
calendar.clear();
calendar.set(y,m,d);
return calendar.getTimeInMillis();
} public static Date getWeekFristDay() {
Calendar c = new GregorianCalendar();
c.setFirstDayOfWeek(Calendar.SUNDAY);
c.setTime(new Date());
c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek());
return c.getTime();
} /**
* 过去下个星期几的时间
* 如获取下个星期一的9:30的时间:getTimeOfNextWeekByTime(1, 9,30,0)
* @param day 星期数【1:一;2:二;3:三;4:四;5:五;6:六;0:日】
* @param hour
* @param minute
* @param second
* @return
*/
public static Date getTimeOfWeekByTime(int day, int hour,int minute,int second){
Date curWeekFirstDay = getWeekFristDay();
Calendar c = Calendar.getInstance();
c.setTime(curWeekFirstDay);
//对应星期几就加几天
c.add(Calendar.DAY_OF_WEEK, day);
c.set(Calendar.HOUR_OF_DAY, hour);
c.set(Calendar.MINUTE, minute);
c.set(Calendar.SECOND, second);
return c.getTime();
} /**
* 过去下个星期几的时间
* 如获取下个星期一的9:30的时间:getTimeOfNextWeekByTime(1, 9,30,0)
* @param day 星期数【1:一;2:二;3:三;4:四;5:五;6:六;0:日】
* @param hour
* @param minute
* @param second
* @return
*/
public static Date getTimeOfNextWeekByTime(int day, int hour,int minute,int second){
Date curWeekFirstDay = getWeekFristDay();
Calendar c = Calendar.getInstance();
c.setTime(curWeekFirstDay);
//下一周加七天
c.add(Calendar.DAY_OF_WEEK, 7);
//对应星期几就加几天
c.add(Calendar.DAY_OF_WEEK, day);
c.set(Calendar.HOUR_OF_DAY, hour);
c.set(Calendar.MINUTE, minute);
c.set(Calendar.SECOND, second);
return c.getTime();
} public static Date getMonthFirstDay() {
Calendar c = new GregorianCalendar();
c.setTime(new Date());
c.set(Calendar.DAY_OF_MONTH, 1);
return c.getTime();
} public static Date getYearFirstDay() {
Calendar c = new GregorianCalendar();
c.setTime(new Date());
c.set(Calendar.DAY_OF_YEAR, 1);
return c.getTime();
} public static long getNowTime()
{
Calendar c = Calendar.getInstance();
int y = c.get(Calendar.YEAR);
int m = c.get(Calendar.MONTH);
int d = c.get(Calendar.DATE);
c.clear();
c.set(y, m, d);
return c.getTimeInMillis();
} public static long getDayTime(long time){
Calendar c = Calendar.getInstance();
c.setTimeInMillis(time);
int y = c.get(Calendar.YEAR);
int m = c.get(Calendar.MONTH);
int d = c.get(Calendar.DATE);
c.clear();
c.set(y, m, d);
return c.getTimeInMillis();
}
public static long getYesterdayTime()
{
Calendar c = Calendar.getInstance();
c.add(Calendar.DATE, -1);
int y = c.get(Calendar.YEAR);
int m = c.get(Calendar.MONTH);
int d = c.get(Calendar.DATE);
c.clear();
c.set(y, m, d);
return c.getTimeInMillis();
} public static long getNDayTimeByNow(int n)
{
Calendar c = Calendar.getInstance();
c.add(Calendar.DATE, n);
int y = c.get(Calendar.YEAR);
int m = c.get(Calendar.MONTH);
int d = c.get(Calendar.DATE);
c.clear();
c.set(y, m, d);
return c.getTimeInMillis();
} public static int getDateNumByNow(){
Calendar c = Calendar.getInstance();
int y = c.get(Calendar.YEAR);
int m = c.get(Calendar.MONTH)+1;
int d = c.get(Calendar.DATE);
String mStr = m+"";
String dStr = d+"";
if(m<10){
mStr = "0"+m;
}
if(d<10){
dStr = "0"+d;
}
String num = y+mStr+dStr;
return Integer.parseInt(num);
} /**
* 获取两个时间相差的天数
* @param time1
* @param time2
* @return
*/
public static int getBetweenDays(long time1, long time2){
// SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
long ztime1 = getZeroHourTimeOfDay(time1);
long ztime2 = getZeroHourTimeOfDay(time2);
// System.out.println(dateFormat.format(time1));
// System.out.println(dateFormat.format(ztime1));
// System.out.println(dateFormat.format(time2));
// System.out.println(dateFormat.format(ztime2));
return (int) ((ztime1 - ztime2)/ONE_DAY_TIME);
} /**
*
* @param time
* @return
*/
public static int getBetweenDays(long time){
return (int) ((getZeroHourTimeOfDay(time) - getZeroHourTimeOfDay(new Date().getTime()))/ONE_DAY_TIME);
} /**
* 根据时间段获取天数
* @param time
* @return
*/
public static int getDays(long time){
return (int) (time/ONE_DAY_TIME);
} /**
* 根据天数获取对应天数的时间
* @param days
* @return
*/
public static long getTimeByDays(int days){
return days*ONE_DAY_TIME;
} /**
* 格式化时间
* yyyy-MM-dd HH:mm:ss
* @param time
* @return
*/
public static String format(Object time){
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return dateFormat.format(time);
}
public static void main(String[] args) {
// Date date = new Date();
//// long time = date.getTime();
// long time1 = 1375127200200l;
// long time2 = 1374029202000l;
//// System.out.println(ONE_DAY_TIME);
//// System.out.println(getZeroHourTimeOfDay(time));
//// System.out.println(new Date(getZeroHourTimeOfDay(time)));
// System.out.println("time1:" + new Date(time1));
// System.out.println("time2:" + new Date(time2));
// System.out.println(getBetweenDays(time1, time2));
// System.out.println(getBetweenDays(time1)); System.out.println(getDays(6*ONE_DAY_TIME));
}
}
测试输出
------------------------购买VIP产品 Start----------------------------
普通会员转成VIP会员
更新用户VIP时间信息
购买的VIP卡的天数:90
更新前VIP普通会员时间:2013-07-31 00:00:00
更新前VIP超级会员时间:2013-07-31 00:00:00
更新后VIP普通会员时间:2013-10-29 00:00:00
更新后VIP超级会员时间:2013-08-30 00:00:00
赠送VIP经验点:30
更新用户VIP经验信息
用户所使用的VIP等级策略:VipLevelStrategyImpl1
更新前VIP经验:0
更新前VIP等级:1
更新后VIP经验:30
更新后VIP等级:2
当前VIP等级:2
当前VIP经验:30
当前升级到下一级所需要的VIP经验:38
------------------------购买VIP产品 End----------------------------
=========================
更新类型:上次登陆前VIP已过期!
更新上次登陆时间为:2013-07-18 00:00:00
更新VIP普通会员到期时间为:2013-07-17 00:00:00
更新VIP超级会员到期时间为:2013-06-11 00:00:00
=========================
------------------------用户登陆 Start----------------------------
当前用户:小学生三号
当前VIP等级:2
当前VIP经验:30
VIP普通会员到期时间:2013-07-17 00:00:00
VIP超级会员到期时间:2013-06-11 00:00:00
当前升级到下一级所需要的VIP经验:38
上次登陆时间:2013-07-18 00:00:00
更新VIP信息
VIP经验成长值:-130
更新用户VIP经验信息
用户所使用的VIP等级策略:VipLevelStrategyImpl1
更新前VIP经验:30
更新前VIP等级:2
更新后VIP经验:0
更新后VIP等级:1
当前VIP等级:1
当前VIP经验:0
当前升级到下一级所需要的VIP经验:16
------------------------用户登陆 End----------------------------
------------------------奖励VIP用户 Start----------------------------
当前用户不是VIP用户或VIP已过期!
------------------------奖励VIP用户 End----------------------------
------------------------购买VIP产品 Start----------------------------
普通会员转成VIP会员
更新用户VIP时间信息
购买的VIP卡的天数:90
更新前VIP普通会员时间:2013-07-31 00:00:00
更新前VIP超级会员时间:2013-07-31 00:00:00
更新后VIP普通会员时间:2013-10-29 00:00:00
更新后VIP超级会员时间:2013-08-30 00:00:00
赠送VIP经验点:30
更新用户VIP经验信息
用户所使用的VIP等级策略:VipLevelStrategyImpl1
更新前VIP经验:0
更新前VIP等级:1
更新后VIP经验:30
更新后VIP等级:2
当前VIP等级:2
当前VIP经验:30
当前升级到下一级所需要的VIP经验:38
------------------------购买VIP产品 End----------------------------
=========================
更新类型:VIP已过期,上次登陆VIP超级会员已过期!
更新上次登陆时间为:2013-07-18 00:00:00
更新VIP普通会员到期时间为:2013-07-21 00:00:00
更新VIP超级会员到期时间为:2013-06-11 00:00:00
=========================
------------------------用户登陆 Start----------------------------
当前用户:小学生三号
当前VIP等级:2
当前VIP经验:30
VIP普通会员到期时间:2013-07-21 00:00:00
VIP超级会员到期时间:2013-06-11 00:00:00
当前升级到下一级所需要的VIP经验:38
上次登陆时间:2013-07-18 00:00:00
更新VIP信息
VIP经验成长值:-70
更新用户VIP经验信息
用户所使用的VIP等级策略:VipLevelStrategyImpl1
更新前VIP经验:30
更新前VIP等级:2
更新后VIP经验:0
更新后VIP等级:1
当前VIP等级:1
当前VIP经验:0
当前升级到下一级所需要的VIP经验:16
------------------------用户登陆 End----------------------------
------------------------奖励VIP用户 Start----------------------------
当前用户不是VIP用户或VIP已过期!
------------------------奖励VIP用户 End----------------------------
------------------------购买VIP产品 Start----------------------------
普通会员转成VIP会员
更新用户VIP时间信息
购买的VIP卡的天数:90
更新前VIP普通会员时间:2013-07-31 00:00:00
更新前VIP超级会员时间:2013-07-31 00:00:00
更新后VIP普通会员时间:2013-10-29 00:00:00
更新后VIP超级会员时间:2013-08-30 00:00:00
赠送VIP经验点:30
更新用户VIP经验信息
用户所使用的VIP等级策略:VipLevelStrategyImpl1
更新前VIP经验:0
更新前VIP等级:1
更新后VIP经验:30
更新后VIP等级:2
当前VIP等级:2
当前VIP经验:30
当前升级到下一级所需要的VIP经验:38
------------------------购买VIP产品 End----------------------------
=========================
更新类型:VIP超级会员、VIP普通会员都已过期!
更新上次登陆时间为:2013-07-18 00:00:00
更新VIP普通会员到期时间为:2013-07-26 00:00:00
更新VIP超级会员到期时间为:2013-07-21 00:00:00
=========================
------------------------用户登陆 Start----------------------------
当前用户:小学生三号
当前VIP等级:2
当前VIP经验:30
VIP普通会员到期时间:2013-07-26 00:00:00
VIP超级会员到期时间:2013-07-21 00:00:00
当前升级到下一级所需要的VIP经验:38
上次登陆时间:2013-07-18 00:00:00
更新VIP信息
VIP经验成长值:45
更新用户VIP经验信息
用户所使用的VIP等级策略:VipLevelStrategyImpl1
更新前VIP经验:30
更新前VIP等级:2
更新后VIP经验:75
更新后VIP等级:3
当前VIP等级:3
当前VIP经验:75
当前升级到下一级所需要的VIP经验:83
------------------------用户登陆 End----------------------------
------------------------奖励VIP用户 Start----------------------------
当前用户不是VIP用户或VIP已过期!
------------------------奖励VIP用户 End----------------------------
------------------------购买VIP产品 Start----------------------------
普通会员转成VIP会员
更新用户VIP时间信息
购买的VIP卡的天数:90
更新前VIP普通会员时间:2013-07-31 00:00:00
更新前VIP超级会员时间:2013-07-31 00:00:00
更新后VIP普通会员时间:2013-10-29 00:00:00
更新后VIP超级会员时间:2013-08-30 00:00:00
赠送VIP经验点:30
更新用户VIP经验信息
用户所使用的VIP等级策略:VipLevelStrategyImpl1
更新前VIP经验:0
更新前VIP等级:1
更新后VIP经验:30
更新后VIP等级:2
当前VIP等级:2
当前VIP经验:30
当前升级到下一级所需要的VIP经验:38
------------------------购买VIP产品 End----------------------------
=========================
更新类型:VIP普通会员未过期,上次登陆前VIP超级会员过期!
更新上次登陆时间为:2013-07-18 00:00:00
更新VIP普通会员到期时间为:2013-08-05 00:00:00
更新VIP超级会员到期时间为:2013-07-11 00:00:00
=========================
------------------------用户登陆 Start----------------------------
当前用户:小学生三号
当前VIP等级:2
当前VIP经验:30
VIP普通会员到期时间:2013-08-05 00:00:00
VIP超级会员到期时间:2013-07-11 00:00:00
当前升级到下一级所需要的VIP经验:38
上次登陆时间:2013-07-18 00:00:00
更新VIP信息
VIP经验成长值:130
更新用户VIP经验信息
用户所使用的VIP等级策略:VipLevelStrategyImpl1
更新前VIP经验:30
更新前VIP等级:2
更新后VIP经验:160
更新后VIP等级:4
当前VIP等级:4
当前VIP经验:160
当前升级到下一级所需要的VIP经验:458
------------------------用户登陆 End----------------------------
------------------------奖励VIP用户 Start----------------------------
当前用户:小学生三号
VIP等级:4
获取VIP奖励:4级VIP等级奖励(奖励)
------------------------奖励VIP用户 End----------------------------
------------------------购买VIP产品 Start----------------------------
普通会员转成VIP会员
更新用户VIP时间信息
购买的VIP卡的天数:90
更新前VIP普通会员时间:2013-07-31 00:00:00
更新前VIP超级会员时间:2013-07-31 00:00:00
更新后VIP普通会员时间:2013-10-29 00:00:00
更新后VIP超级会员时间:2013-08-30 00:00:00
赠送VIP经验点:30
更新用户VIP经验信息
用户所使用的VIP等级策略:VipLevelStrategyImpl1
更新前VIP经验:0
更新前VIP等级:1
更新后VIP经验:30
更新后VIP等级:2
当前VIP等级:2
当前VIP经验:30
当前升级到下一级所需要的VIP经验:38
------------------------购买VIP产品 End----------------------------
=========================
更新类型:VIP超级会员已过期,成为VIP普通会员!
更新上次登陆时间为:2013-07-18 00:00:00
更新VIP普通会员到期时间为:2013-08-20 00:00:00
更新VIP超级会员到期时间为:2013-07-26 00:00:00
=========================
------------------------用户登陆 Start----------------------------
当前用户:小学生三号
当前VIP等级:2
当前VIP经验:30
VIP普通会员到期时间:2013-08-20 00:00:00
VIP超级会员到期时间:2013-07-26 00:00:00
当前升级到下一级所需要的VIP经验:38
上次登陆时间:2013-07-18 00:00:00
更新VIP信息
VIP经验成长值:170
更新用户VIP经验信息
用户所使用的VIP等级策略:VipLevelStrategyImpl1
更新前VIP经验:30
更新前VIP等级:2
更新后VIP经验:200
更新后VIP等级:4
当前VIP等级:4
当前VIP经验:200
当前升级到下一级所需要的VIP经验:418
------------------------用户登陆 End----------------------------
------------------------奖励VIP用户 Start----------------------------
当前用户:小学生三号
VIP等级:4
获取VIP奖励:4级VIP等级奖励(奖励)
------------------------奖励VIP用户 End----------------------------
------------------------购买VIP产品 Start----------------------------
普通会员转成VIP会员
更新用户VIP时间信息
购买的VIP卡的天数:90
更新前VIP普通会员时间:2013-07-31 00:00:00
更新前VIP超级会员时间:2013-07-31 00:00:00
更新后VIP普通会员时间:2013-10-29 00:00:00
更新后VIP超级会员时间:2013-08-30 00:00:00
赠送VIP经验点:30
更新用户VIP经验信息
用户所使用的VIP等级策略:VipLevelStrategyImpl1
更新前VIP经验:0
更新前VIP等级:1
更新后VIP经验:30
更新后VIP等级:2
当前VIP等级:2
当前VIP经验:30
当前升级到下一级所需要的VIP经验:38
------------------------购买VIP产品 End----------------------------
=========================
更新类型:VIP超级会员!
更新上次登陆时间为:2013-07-18 00:00:00
更新VIP普通会员到期时间为:2013-08-10 00:00:00
更新VIP超级会员到期时间为:2013-09-09 00:00:00
=========================
------------------------用户登陆 Start----------------------------
当前用户:小学生三号
当前VIP等级:2
当前VIP经验:30
VIP普通会员到期时间:2013-08-10 00:00:00
VIP超级会员到期时间:2013-09-09 00:00:00
当前升级到下一级所需要的VIP经验:38
上次登陆时间:2013-07-18 00:00:00
更新VIP信息
VIP经验成长值:195
更新用户VIP经验信息
用户所使用的VIP等级策略:VipLevelStrategyImpl1
更新前VIP经验:30
更新前VIP等级:2
更新后VIP经验:225
更新后VIP等级:4
当前VIP等级:4
当前VIP经验:225
当前升级到下一级所需要的VIP经验:393
------------------------用户登陆 End----------------------------
------------------------奖励VIP用户 Start----------------------------
当前用户:小学生三号
VIP等级:4
获取VIP奖励:4级VIP等级奖励(奖励)
------------------------奖励VIP用户 End----------------------------
哈哈 求代码的童鞋 发个评论留个邮箱 我发代码到邮箱中
透露下 哈 下一期打算写 游戏中竞技场或爬塔的实现 哈哈 敬请期待哦