You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Java代码中为每行添加类似计数的时间戳

没问题!咱们可以通过三个简单的步骤实现给HTML表格每行开头添加时间戳的需求——核心思路是在读取CSV每行数据时生成时间戳、存储到数据模型里,最后在生成HTML时替换原来的计数位置为时间戳。

1. 扩展LineInfo数据模型,添加时间戳字段

先在LineInfo类里新增存储时间戳的字段,同时更新构造方法接收这个参数:

package display;
/////////////////////////////////////////////////////////////////////////////////////////////////////
import java.io.IOException;

public class LineInfo {
 int seq = 0; // system sequence number
 String ser = ""; 
 String subject = ""; 
 String title1 = ""; 
 String title2 = ""; 
 String title3 = ""; 
 String timestamp = ""; // 新增:存储每行的时间戳

 //////////////////////////////////////////////////////////////////////////////////////////////////////
 // 修改构造方法,新增timestamp参数
 public LineInfo(int sn, String sr, String sbj, String t1, String t2, String t3, String ts) throws IOException {
 this.seq = sn;
 this.ser = sr;
 this.subject = sbj;
 this.title1 = t1;
 this.title2 = t2;
 this.title3 = t3;
 this.timestamp = ts; // 赋值时间戳
 }
 ///////////////////////////////////////////////////////////////////////////////////////////////////////
}

2. 在读取CSV时生成时间戳

在Play类的loadInfo方法里,每次读取一行CSV数据时生成格式化的时间戳(推荐用Java 8+的DateTimeFormatter,线程安全且易用),并传入LineInfo构造方法:

package display;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter; // 新增:导入日期格式化类
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class Play {
 public static List <LineInfo> INFO = new ArrayList<LineInfo>();
 // 新增:定义时间戳格式化规则,比如"yyyy-MM-dd HH:mm:ss"
 private static final DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

 ///////////////////////////////////////////////////////////////////////////////////////////
 public static void loadInfo() throws IOException {
 int count = 0;
 String filePath = "C:\\folder\\";
 String fileName = "file.csv";
 BufferedReader csvReader = new BufferedReader(new FileReader(filePath + fileName));
 String row = "";
 /////////////////////////////////////////////////////////////////////////////////
 while ((row = csvReader.readLine()) != null) {
 count++;
 String[] raw = row.split(",");
 //System.out.println(row);
 String[] data = new String[20];
 for (int i = 0; i < raw.length; i++) {
 data[i] = raw[i];
 }
 for (int i = 0; i < data.length; i++) {
 if(data[i] == null) data[i] = "";
 }
 // 新增:生成当前行的时间戳
 String currentTimestamp = LocalDateTime.now().format(TIME_FORMATTER);
 // 修改:LineInfo构造方法新增timestamp参数
 LineInfo line = new LineInfo(count, data[0], data[1], data[2], data[3], data[4], currentTimestamp);
 INFO.add(line);
 }
 //////////////////////////////////////////////////////////////////////////////
 csvReader.close();
 System.out.println("File loaded successfully total records : " + (INFO.size() -1));
 }
 // 剩余代码不变...
}

3. 在生成HTML时替换计数为时间戳

最后在HTMLify类的ddHTML方法里,把原来显示递增计数的位置替换成存储的时间戳:

package display;
// 剩余导入不变...
public class HTMLify {
 // 剩余代码不变...
 public static void ddHTML() throws IOException {
 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 StringBuilder htmlDD = new StringBuilder();
 // 剩余HTML头部代码不变...

 for (int i = 0; i < SUBJECT.size(); i++) {
 // 移除原来的count变量,不再需要递增计数
 // int count = 0;
 htmlDD.append("<center><tr><th class=\"tg-smpa\" colspan=\"7\">" + SUBJECT.get(i) + "</th></tr></center>");
 htmlDD.append("<tr><td class=\"tg-jmap\">" + INFO.get(0).ser + "</td><td class=\"tg-jmap\">" + INFO.get(0).title1 + "</td><td class=\"tg-jmap\">" + INFO.get(0).title2 + "</td><td class=\"tg-jmap\">" + INFO.get(0).title3 + " </td></tr>");
 for (int z = 1; z < INFO.size(); z++) {
 if(SUBJECT.get(i).equals(INFO.get(z).subject)) {
 // 移除count递增代码
 // count++;
 // 修改:把count替换为INFO.get(z).timestamp
 htmlDD.append("<tr><td class=\"tg-cgn1\">" + INFO.get(z).timestamp + "</td><td class=\"tg-cgn1\">" +"<img src=\"\\folder\\images\\Alerts\\" + INFO.get(z).title1 + ".gif\" style=\"float:center;\">" + "</td><td class=\"tg-cgn1\">" + INFO.get(z).title2 + "</td><td class=\"tg-cgn1\">" + INFO.get(z).title3 + "</td></tr>");
 }
 }
 htmlDD.append("<tr><td></td></tr>");
 }
 // 剩余HTML尾部代码不变...
 }
 // 剩余代码不变...
}

可选调整:如果需要类似计数的时间偏移

如果你想要的是“自加载开始的秒数”这种更贴近原计数逻辑的时间戳,可以把步骤2里的时间戳生成逻辑改成:

// 在loadInfo方法开头记录起始时间
long startTime = System.currentTimeMillis();

// 在循环里计算时间偏移
long elapsedSeconds = (System.currentTimeMillis() - startTime) / 1000;
String currentTimestamp = String.format("00:00:%02d", elapsedSeconds);

内容的提问来源于stack exchange,提问作者Cisco_Fiery

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 19:18:13