/**
* 处理时间
*
* @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();
}