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

<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;
	}
				
时间: 2024-08-26 10:48:11

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

根据时间值 显示对应的字符串

/** * 获取显示时间 * * @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 ye

Jsp开发自定义标签,自定义标签将字符串转成指定的时间格式显示

本例以将 字符串格式的时间转成指定的时间格式显示. 第一步.定义一个标签处理程序类,需要集成javax.servlet.jsp.tagext.TagSupport,代码如下: import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Calendar; import javax.servlet.jsp.JspException; import javax.servlet.jsp.tagext.T

依据不同区时显示对应的时间

如今项目基本上告一段落了,难得有一定的闲暇,今天利用数小时完毕了一个功能模块--依据不同区时显示对应的时间,这方面网上基本没有现成的样例,如今将代码粘贴例如以下: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">        <html>                <head>                        <meta http-equiv=&qu

关于时间的操作(JavaScript版)——依据不同区时显示对应的时间

如今项目基本上告一段落了,难得有一定的闲暇,今天利用数小时完毕了一个功能模块--依据不同区时显示对应的时间,这方面网上基本没有现成的样例,如今将代码粘贴例如以下: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <meta http-equiv="content-type" content="text/html

【php】读取&quot;文件列表&quot;按时间倒序显示,并递归显示各层目录、!

思路: 1.读取该php所在目录的文件列表,用"修改时间.文件名"做键值对,塞入数组.对"修改时间"倒序.(貌似不能直接按时间倒序读取文件列表,此处为间接方法) 2.读取的若为文件直接输出,为目录就输出目录并递归扫描其下文件. <?php //遍历当前目录下所有文件的和目录,并以树装形式显示 //1.打开目录句柄,获取句柄资源 //2.读取句柄资源,并显示当前和子目录下的(目录和文件名称) function getDirFile($path){ if(!($f

python datatime日期和时间值模块

datetime.time():是一个时间类,这个类接受4个参数,分别代表时,分,秒,毫秒.参数的默认值是为0 1 #!/usr/bin/env python 2 #coding:utf8 3 import datetime 4 t=datetime.time(20, 00, 13, 00) 5 print t 6 print '*'*20 7 print t.hour 8 print t.minute 9 print t.second 10 print t.microsecond 11 12

2016-06-02 获取系统当前日期和时间并显示在某个元素上

1. <script> window.onload=function(){ getDateAndTime(); setInterval(getDateAndTime,1000); } //获取系统的日期和时间并显示在某个元素上 function getDateAndTime(){ var myDate = new Date(); var year = myDate.getFullYear(); var month = myDate.getMonth()+1; var day = myDate.

js获取当前日期时间同时显示星期

JavaScript获取当前日期时间同时显示星期几,具体代码如下: <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/

Fiddler 中请求时间的显示列

在Tool bar上面找到 Rules->CustomRules 在class Handlers{   里面添加 //----------------------------显示请求时间,显示毫秒格式------------- function BeginRequestTime(oS: Session) { if (oS.Timers != null) { return oS.Timers.ClientBeginRequest.ToString(); } return String.Empty;