1.需求
在目前项目中有个txt文本是这样的。
希望转化为正常的格式并且按照时间排序
bus3 2015-12-23 23:27:25
bus2 2018-12-23 23:17:25
bus4 2019-12-23 23:37:25
bus1 2020-09-23 13:20:53
2.分析
以时间排序为例,来实现java对类对象的排序,设java某class的定义如下:
class cmpData
{
String Name;
String sDate;
public cmpData(String Name,String strDate) {
this.Name=Name;
this.sDate=strDate;
}
}
这里sDate表示某个日期,格式为"yyyy-mm-dd hh:mm:ss",若使得类具有比较大小功能,可以继承接口Comparable,并重写接口方法compareTo:
class cmpData implements Comparable<cmpData>
{
String Name;
String sDate;
public cmpData(String Name,String strDate) throws ParseException {
this.Name=Name;
this.sDate=strDate;
}
@Override
public int compareTo(cmpData user) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss");
Date date1 = null;
try {
date1 = sdf.parse(this.sDate);
} catch (ParseException e) {
e.printStackTrace();
}
Date date2 = null;
try {
date2 = sdf.parse(user.sDate);
} catch (ParseException e) {
e.printStackTrace();
}
return (int) ((date1.getTime() - date2.getTime())/1000);
}
}
这里的比较函数compareTo里,对于日期的比较先调用SimpleDateFormat的parse函数解析字符串表示的日期并转换为Date类对象,然后调用Date的getTIme()函数将其表示的日期转化为时间戳,注意这里返回的是long型的时间戳,且单位为毫秒,而compareTo得返回值为int故需除以1000并转为int。这样就可以调用Collections得sort函数进行排序了。
3.代码
package geym.conc.ch9;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import java.io.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
@Data
@Slf4j
public class SocketServer {
public static void main(String[] args) throws Exception {
String fileName = "C:\\Users\\DELL\\Desktop\\1.txt";
List<cmpData> returnList = readFileByLines(fileName);
if(returnList != null){
Collections.sort(returnList);
for (SocketServer.cmpData cmpData : returnList) {
System.out.println(cmpData.Name + " " + cmpData.sDate);
}
}
}
/**
* 以行为单位读取文件,常用于读面向行的格式化文件
*/
public static List<cmpData> readFileByLines(String fileName) throws Exception {
List<cmpData> list = new ArrayList<>();
File file = new File(fileName);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); //格式化规则
//判断输入的是不是txt
if(!fileName.endsWith("txt")) {
log.error("文件格式不对!");
//判断输入的文档是否存在,不存在则提示退出
throw new Exception("文件格式不对");
}
BufferedReader reader;
try {
System.out.println("以行为单位读取文件内容,一次读一整行:");
reader = new BufferedReader(new FileReader(file));
String tempString;
int line = 1;
String[] strs;
// 一次读入一行,直到读入null为文件结束
while ((tempString = reader.readLine()) != null) {
if(tempString.equals(""))//判断是否是空行
{
continue;//是空行则不进行操作
}
System.out.println("line " + line + ": " + tempString);
// 显示行号
strs = tempString.trim().split(" {4}");
Date date1 = new SimpleDateFormat("dd MMM yyyy HH:mm:ss.SSS", Locale.US).parse(strs[1]);
Date date2 = new SimpleDateFormat("dd MMM yyyy HH:mm:ss.SSS", Locale.US).parse(strs[2]);
String strDate1= sdf.format(date1); //格式化成yyyy-MM-dd格式的时间字符串
String strDate2= sdf.format(date2); //格式化成yyyy-MM-dd格式的时间字符串
list.add(new cmpData("Time+"+line,strDate1));
list.add(new cmpData("Time+"+line,strDate2));
line++;
}
reader.close();
return list;
} catch (IOException | ParseException e) {
log.info("geym.conc.ch9.SocketServer.readFileByLines"+e);
log.debug("geym.conc.ch9.SocketServer.readFileByLines"+e);
e.printStackTrace();
}
return null;
}
static class cmpData implements Comparable<cmpData> {
String Name;
String sDate;
public cmpData(String Name, String strDate) throws ParseException {
this.Name = Name;
this.sDate = strDate;
}
public int compareTo(cmpData user) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date date1 = null;
try {
date1 = sdf.parse(this.sDate);
} catch (ParseException e) {
e.printStackTrace();
}
Date date2 = null;
try {
date2 = sdf.parse(user.sDate);
} catch (ParseException e) {
e.printStackTrace();
}
if(date1 != null && date2 != null) {
return (int) ((date1.getTime() - date2.getTime())/1000);
}
log.error("有误");
return Integer.MIN_VALUE;
}
}
}
4.打印结果
以行为单位读取文件内容,一次读一整行:
line 1: 1 1 Aug 2019 00:00:19.729 1 Aug 2019 00:20:15.541
line 2: 2 1 Aug 2019 00:00:19.842 1 Aug 2019 00:05:00.688
line 3: 3 1 Aug 2019 00:00:31.672 1 Aug 2019 00:17:57.556
line 4: 4 1 Aug 2019 00:00:34.534 1 Aug 2019 00:18:01.391
line 5: 5 1 Aug 2019 00:00:42.030 1 Aug 2019 00:09:42.111
line 6: 6 1 Aug 2019 00:00:43.346 1 Aug 2019 00:20:03.807
line 7: 7 1 Aug 2019 00:01:24.309 1 Aug 2019 00:20:31.593
line 8: 8 1 Aug 2019 00:03:57.673 1 Aug 2019 00:13:09.924
line 9: 9 1 Aug 2019 00:04:58.809 1 Aug 2019 00:18:49.089
line 10: 10 1 Aug 2019 00:05:36.641 1 Aug 2019 00:18:15.085
line 11: 11 1 Aug 2019 00:10:44.598 1 Aug 2019 00:21:04.951
line 12: 12 1 Aug 2019 00:10:45.898 1 Aug 2019 00:14:04.472
line 13: 13 1 Aug 2019 00:10:53.942 1 Aug 2019 00:12:48.138
line 14: 14 1 Aug 2019 00:12:38.525 1 Aug 2019 00:20:07.046
line 15: 15 1 Aug 2019 00:13:57.108 1 Aug 2019 00:14:00.240
line 16: 16 1 Aug 2019 00:15:36.210 1 Aug 2019 00:17:54.284
line 17: 17 1 Aug 2019 00:15:39.966 1 Aug 2019 00:17:20.098
line 18: 18 1 Aug 2019 00:15:57.959 1 Aug 2019 00:19:18.725
转化后======================================================================================
Time+1 2019-08-01 00:00:19.729
Time+2 2019-08-01 00:00:19.842
Time+3 2019-08-01 00:00:31.672
Time+4 2019-08-01 00:00:34.534
Time+5 2019-08-01 00:00:42.030
Time+6 2019-08-01 00:00:43.346
Time+7 2019-08-01 00:01:24.309
Time+8 2019-08-01 00:03:57.673
Time+9 2019-08-01 00:04:58.809
Time+2 2019-08-01 00:05:00.688
Time+10 2019-08-01 00:05:36.641
Time+5 2019-08-01 00:09:42.111
Time+11 2019-08-01 00:10:44.598
Time+12 2019-08-01 00:10:45.898
Time+13 2019-08-01 00:10:53.942
Time+14 2019-08-01 00:12:38.525
Time+13 2019-08-01 00:12:48.138
Time+8 2019-08-01 00:13:09.924
Time+15 2019-08-01 00:13:57.108
Time+15 2019-08-01 00:14:00.240
Time+12 2019-08-01 00:14:04.472
Time+16 2019-08-01 00:15:36.210
Time+17 2019-08-01 00:15:39.966
Time+18 2019-08-01 00:15:57.959
Time+17 2019-08-01 00:17:20.098
Time+16 2019-08-01 00:17:54.284
Time+3 2019-08-01 00:17:57.556
Time+4 2019-08-01 00:18:01.391
Time+10 2019-08-01 00:18:15.085
Time+9 2019-08-01 00:18:49.089
Time+18 2019-08-01 00:19:18.725
Time+6 2019-08-01 00:20:03.807
Time+14 2019-08-01 00:20:07.046
Time+1 2019-08-01 00:20:15.541
Time+7 2019-08-01 00:20:31.593
Time+11 2019-08-01 00:21:04.951
本公众号分享自己从程序员小白到经历春招秋招斩获10几个offer的面试笔试经验,其中包括【Java】、【操作系统】、【计算机网络】、【设计模式】、【数据结构与算法】、【大厂面经】、【数据库】期待你加入!!!
1.计算机网络----三次握手四次挥手
2.梦想成真-----项目自我介绍
3.你们要的设计模式来了
4.震惊!来看《这份程序员面试手册》!!!
5.一字一句教你面试“个人简介”
6.接近30场面试分享
7.你们要的免费书来了