首页 > 代码库 > 依据时间值 显示相应的字符串

依据时间值 显示相应的字符串

<pre name="code" class="java">/**
	 * 获取显示时间
	 * 
	 * @param time
	 * @return
	 */
	public String getDate(long time) {

		if (time <= 0) {
			return "";
		}

		// 日期处理
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		int day = calendar.get(Calendar.DAY_OF_MONTH);
		int year = calendar.get(Calendar.YEAR);
		int hour = calendar.get(Calendar.HOUR_OF_DAY);
		int miunte = calendar.get(Calendar.MINUTE);
		int millisecond = calendar.get(Calendar.SECOND);
		int month = calendar.get(Calendar.MONTH)+ 1;

		// 评论日期
		Calendar commentCale = Calendar.getInstance();
		commentCale.setTime(new Date(time));
		int commDay = commentCale.get(Calendar.DAY_OF_MONTH);
		int commYear = commentCale.get(Calendar.YEAR);
		int commHour = commentCale.get(Calendar.HOUR_OF_DAY);
		int commMiunte = commentCale.get(Calendar.MINUTE);
		int commMonth = commentCale.get(Calendar.MONTH)+ 1;
		int commMillisecond = commentCale.get(Calendar.SECOND);
		
		


		// 刚刚
		// X秒前
		// X分钟前
		// X小时前
		// 昨天
		// X月X日
		// 2014年

		//以往年份
		if (year - commYear >= 1) {// 去年 显示年
			return commYear + "年";
		}
		
		
		// 今年时间
		if (year - commYear == 0) {// X月X日
			// 同月
			if (month != commMonth) {
				return commMonth + "月" + commDay + "日";
			}
			// return commMonth + "月" + commDay + "日";

			// 以往天数
			if (day - commDay == 1) {// 昨天
				return "昨天";
			}

			if (day - commDay == 0) {// 今天
				if (hour == commHour) {// 同一小时
					if (miunte == commMiunte) {// 同一分钟
						if (millisecond == commMillisecond) {
							return "刚刚";
						}
						return millisecond - commMillisecond + "秒前";
					}
					return miunte - commMiunte + "分钟前";
				}

				return hour - commHour + "小时前";
			}

		}

		// 一般不会到这步
		DateFormat format = new SimpleDateFormat("yyyy年MM月dd日");
		return format.format(new Date(time));

	}

随机从缓存中取10个用户的方法,

/**
	 * 查询约歌红人
	 * 
	 * @param size
	 *            取多少条 10
	 * @return
	 */
	public SInviteToWhom.Builder getInviteReds(int size) {

		String key = RedisKeys.INVITE_SUCCEED_RECENT;
		Map<Long, Long> map = redisSet.zrevRangeWithScore(key, 0,
				REDS_STORE_SIZE);// 倒序取200
		SInviteToWhom.Builder whom = SInviteToWhom.newBuilder();

		if (map == null || map.size() <= 0) {
			return whom;
		}

		// 所有满足条件的玩家id集合
		List<Long> allPlayerId = new ArrayList<Long>();
		long nowDate = System.currentTimeMillis();

		// 去除时间小于24小时的
		for (Entry<Long, Long> entry : map.entrySet()) {
			// 1
			if (nowDate - entry.getValue() > 86400000) {
				continue;
			}
			allPlayerId.add(entry.getKey());
		}

		if (allPlayerId.size() <= 0) {
			return whom;
		}

		// 有满足的用户
		Set<Long> reslut = new HashSet<Long>();
		Random dom = new Random();
		if (allPlayerId.size() >= (size + 1)) {// 随机读取
			while (true) {
				if (reslut.size() >= size) {// 取到10个就结束
					break;
				}

				int nextId = dom.nextInt(allPlayerId.size());// 随机下标
				Long pid = allPlayerId.get(nextId);
				reslut.add(pid);
			}
			// reslut.add(e)

		} else {// 没有size+1 个 取所有
			reslut.addAll(allPlayerId);
		}

		// 依据用户id查询用户信息
		for (long pid : reslut) {
			PlayerInfo pinfo = playerCommonService.getPlayerInfoById((int) pid);

			if (pinfo == null) {
				continue;
			}
			InvitePlayerInfo.Builder ipi = InvitePlayerInfo.newBuilder();
			ipi.setNickname(pinfo.getNickname());
			//ipi.setAuthExplain(pinfo.getAuthExplain());
			ipi.setHeadImg(pinfo.getHeadimg());
			//ipi.setIsFx(pinfo.getIsFx());
			//ipi.setIsStar(pinfo.getIsStar());
			ipi.setPlayerId(pinfo.getPlayerId());
			ipi.setSex(pinfo.getSex().getId());
			//ipi.setShowFxIcon(SecretType.valueOf(pinfo.getShowFxIcon()));
			PlayerInvite pi=inviteCommonService.getPlayerInvite(pinfo.getPlayerId());
			ipi.setInviteCount(pi.getInviteTotal());

			whom.addPlayerList(ipi);

		}

		return whom;
	}




   

依据时间值 显示相应的字符串