【问题标题】:How to add Timestamp in JAVA code like count (in each line) [closed]如何在 JAVA 代码中添加时间戳,如计数(在每一行中)[关闭]
【发布时间】:2020-08-09 08:01:25
【问题描述】:

我有一个 java 代码,它从 CSV 文件中获取数据并以表格形式显示为 html。

我想添加时间戳应该像计数一样自动出现在每行的开头。

它有三个 JAVA 文件,我将它们粘贴在下面。

请告知我应该添加什么代码以及满足时间戳要求的确切位置。

下面是代码示例:

package display;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class Play 
{
    public static List <LineInfo> INFO = new ArrayList<LineInfo>();
    
    ///////////////////////////////////////////////////////////////////////////////////////////
    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] = "";
            }
            
            LineInfo line = new LineInfo(count, data[0], data[1], data[2], data[3], data[4]);
            INFO.add(line);
            
        }
        //////////////////////////////////////////////////////////////////////////////
        
        
        csvReader.close();
        System.out.println("File loaded successfully total records : " + (INFO.size() -1));
    }

    
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    public static void htmlIT() throws IOException 
    {
        HTMLify html = new HTMLify(INFO);
    }
    
    
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    public static void main(String[] args) throws IOException, InterruptedException
    {
        int i = 0;
        while (i < 1)
        {
            loadInfo();
            htmlIT();
            
            INFO.clear();
            //wait
            TimeUnit.SECONDS.sleep(300);
        }
        
        

    }

}
----------------------------------------------------
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 = ""; // 

    
//////////////////////////////////////////////////////////////////////////////////////////////////////
    public LineInfo(int sn, String sr, String sbj, String t1, String t2, String t3) throws IOException
    {
        this.seq = sn;              
        this.ser = sr;          
        this.subject = sbj;     
        this.title1 = t1;
        this.title2 = t2;
        this.title3 = t3;

    }
    
///////////////////////////////////////////////////////////////////////////////////////////////////////

}

-------------------------------------------------------

package display;


import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;



public class HTMLify 

{
    public static List <LineInfo> INFO = new ArrayList<LineInfo>();
    public static List <String> SUBJECT = new ArrayList<String>();
    
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    public static void ddHTML() throws IOException
    {

        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        StringBuilder htmlDD = new StringBuilder();
        htmlDD.append("<html><head><meta http-equiv=\"refresh\" content=\"30\"/><title> SHEET </title>");
        htmlDD.append("<body bgcolor=\"#ffffff\">");

        //Report style
        htmlDD.append("<style type=\"text/css\">");
        htmlDD.append(".tg  {border-collapse:collapse;border-spacing:0;}");
        htmlDD.append(".tg td{font-family:Arial, sans-serif;font-size:22px;padding:10px 5px;border-style:0;border-width:0px;overflow:hidden;word-break:normal;}");
        htmlDD.append(".tg th{font-family:Arial, sans-serif;font-size:22px;font-weight:normal;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;}");
        htmlDD.append(".tg .tg-smpa{font-weight:bold;font-size:40px;bold;background-color:#e8e8e8;color:#000000;text-align:center}");
        htmlDD.append(".tg .tg-w672{font-weight:bold;font-size:22px;background-color:#5B5B5B;color:#ffffff;text-align:center;text-align:center;vertical-align:top}");
        htmlDD.append(".tg .tg-jmap{font-weight:bold;font-size:22px;text-shadow: rgba(245,245,245,0.5) 1px 2px 1px;background-color:#000000;text-align:center;color:#ffffff;text-align:center;vertical-align:center}");
        htmlDD.append(".tg .tg-cgn1{background-color:#FFFFFF;text-align:center;color:#585858;vertical-align:center}");
        htmlDD.append("</style>");

        htmlDD.append("</head>");
        

        htmlDD.append("<table align=\"center\" class=\"tg\">");
        htmlDD.append("<center><tr><td class=\"tg-smpa\" colspan=\"13\"><img src=\"\\4FW\\images\\header.png\" style=\"float:center;\"></td></tr></center>");

        
        
        for (int i = 0; i < SUBJECT.size(); i++)
        {
            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++;
                htmlDD.append("<tr><td class=\"tg-cgn1\">" + count + 
                        "</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>");
            
            
            
        }
            
        //htmlDD.append("<tr><td></td></tr>");
        
        
        htmlDD.append("<center><tr><td class=\"tg-smpa\" colspan=\"7\"><img src=\"\\4FW\\images\\footer.png\" style=\"float:center;\"></td></tr></center>");
        htmlDD.append("</table></body></html>");

    
        File saveLocal = new File("C:\\xampp\\htdocs\\newfolder\\index.html");
        BufferedWriter writer = new BufferedWriter(new FileWriter(saveLocal));
        writer.write(htmlDD.toString());
        writer.close();
        System.out.println("Display file generated.");

    }
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    public HTMLify (List <LineInfo> info) throws IOException
    {
        this.INFO = info;
        List <String> subj = new ArrayList<String>();
        
        for (int i =1; i < info.size(); i++)
        {
            String sbj = info.get(i).subject;
            subj.add(sbj);
        }
        
        SUBJECT = subj.stream().distinct().collect(Collectors.toList()); 
        ddHTML();
    }
    
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    
}

提前致谢。

【问题讨论】:

  • 我找不到在哪里可以添加以下时间戳代码:Timestamp ts = new Timestamp(System.currentTimeMillis());

标签: java html timestamp


【解决方案1】:

你可以这样做:

htmlDD.append("<tr><td>" + System.currentTimeMillis() + "</td><td class=\"tg-cgn1\">" + count + 
                        "</td>....

确保为时间戳添加标题列(即&lt;th&gt;Timestamp&lt;/th&gt;),否则表格会出现扭曲。

更新

(回答评论中的更多问题)

它以 14 位连续数字的毫秒数出现。我怎样才能 将其转换为 yyyy-mm-dd hh:mm 等格式。

您可以使用DateTimeFormatter 转换为您喜欢的任何格式,但我认为您想要的只是显示日期时间的所有部分(年、月、日、小时、分钟、秒、纳秒),因此,您可以简单地将System.currentTimeMillis() 替换为Instant.now()

我只想要新行的新时间戳,之前添加的行应该 在 CSV 文件中添加该行时具有固定时间戳。但 截至目前,当我在添加新行并运行程序后保存 CSV 文件时。 它将所有行的时间更改为当前时间。有可能吗?

这没什么大不了的。为了做到这一点,您可以将Instant.now() 的值存储在某个文件或数据库中,然后在该程序中获取相同的值,而不是生成时间戳作为该程序的一部分。

【讨论】:

  • 感谢 arvind 的及时回复。我已按照您的建议在标题栏下添加了一行。它有效,但有 2 个问题。它以 14 位连续数字的毫秒数出现。如何将其转换为 yyyy-mm-dd hh:mm 等格式。第二,我只想要新行的新时间戳,并且之前添加的行在该行添加到 CSV 文件时应该具有固定的时间戳。但是到目前为止,当我在添加新行并运行程序后保存 CSV 文件时。它将所有行的时间更改为当前时间。有可能吗?
  • 不客气。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-18
  • 2021-12-12
  • 2012-07-28
  • 1970-01-01
相关资源
最近更新 更多