对String,Date,编码格式转换等基本操作的总结收藏(三)

/**
	 * 处理时间
	 * 
	 * @param timestamp
	 * @return
	 */
	public static String fixTime(String timestamp) {
		if (timestamp == null || "".equals(timestamp)) {
			return "";
		}

		try {
			long _timestamp = Long.parseLong(timestamp) * 1000;
			if (System.currentTimeMillis() - _timestamp < 1 * 60 * 1000) {
				return "刚刚";
			} else if (System.currentTimeMillis() - _timestamp < 30 * 60 * 1000) {
				return ((System.currentTimeMillis() - _timestamp) / 1000 / 60)
						+ "分钟前";
			} else {
				Calendar now = Calendar.getInstance();
				Calendar c = Calendar.getInstance();
				c.setTimeInMillis(_timestamp);
				if (c.get(Calendar.YEAR) == now.get(Calendar.YEAR)
						&& c.get(Calendar.MONTH) == now.get(Calendar.MONTH)
						&& c.get(Calendar.DATE) == now.get(Calendar.DATE)) {
					SimpleDateFormat sdf = new SimpleDateFormat("今天 HH:mm");
					return sdf.format(c.getTime());
				}
				if (c.get(Calendar.YEAR) == now.get(Calendar.YEAR)
						&& c.get(Calendar.MONTH) == now.get(Calendar.MONTH)
						&& c.get(Calendar.DATE) == now.get(Calendar.DATE) - 1) {
					SimpleDateFormat sdf = new SimpleDateFormat("昨天 HH:mm");
					return sdf.format(c.getTime());
				} else if (c.get(Calendar.YEAR) == now.get(Calendar.YEAR)) {
					SimpleDateFormat sdf = new SimpleDateFormat("M月d日 HH:mm:ss");
					return sdf.format(c.getTime());
				} else {
					SimpleDateFormat sdf = new SimpleDateFormat(
							"yyyy年M月d日 HH:mm:ss");
					return sdf.format(c.getTime());
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
	}

	/**
	 * 参数编码
	 * 
	 * @param value
	 * @return
	 */
	public static String encode(String s) {
		if (s == null) {
			return "";
		}
		try {
			return URLEncoder.encode(s, "UTF-8").replace("+", "%20")
					.replace("*", "%2A").replace("%7E", "~")
					.replace("#", "%23");
		} catch (UnsupportedEncodingException e) {
			throw new RuntimeException(e.getMessage(), e);
		}
	}

	/**
	 * 参数反编码
	 * 
	 * @param s
	 * @return
	 */
	public static String decode(String s) {
		if (s == null) {
			return "";
		}
		try {
			return URLDecoder.decode(s, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			throw new RuntimeException(e.getMessage(), e);
		}
	}

将日志文件一行一行的读入,可以根据需求对日志进行筛选

/**
	 * 抽取日志类
	 */
	public static void printLog() {
	
		File file = new File("D:/MyWord", "airailways.log"); // 创建文件对象
		if (file.exists()) {
			boolean Directory = file.isDirectory(); // 判定文件是否为目录
			if (Directory) {
				System.out.println("指定的文件是目录");
			} else {
				System.out.println("指定的文件存在"); // 判定文件是否存在
				String name = file.getName(); // 若文件存在,则获取文件名
				long length = file.length(); // 若文件存在,则获取文件的长度
				System.out.println("文件的名字是: " + name);
				System.out.println("文件的长度是: " + length);
				System.out.println();
				System.out.println("==================================");
				try { // 异常处理机制
					FileReader fr = new FileReader(file); // 创建FileReader类对象
					BufferedReader bufr = new BufferedReader(fr); // 创建BufferedReader类对象
					String s = null; // 创建字符串对象
					int i = 0; // 创建int型变量
					System.out.println("********begin*******************************");
					while ((s = bufr.readLine()) != null) { // 如果文件的文本行数不为null,则进入循环
						i++;
						if (i <= 1000) {
							System.out.println(s);

						}/*
						 * 若文件内容少于10行,则可以很容易从运行结果中看出,因为标定了行数;若多于10行或者是
						 * 想更改显示的行数,则只需把10改变即可
						 */

					}
					System.out.println("*********end******************************");
					bufr.close(); // 将FileReader流关闭
					fr.close(); // 将FileReader流关闭
				} catch (Exception e) {
					e.printStackTrace();
				} // 异常处理机制
			}
		} else {
			System.out.println("文件不存在");
		}
		System.out.println();

	}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值