首页 > 解决方案 > Java中同一行的日期/时间和数据计数

问题描述

我正在尝试编辑我的代码以提供当前日期和时间,然后在同一行上计算一些数据。我附上了下面的java代码。

if(tglbtnWrite.isSelected() && (intervalCounter%requiredParams[2] == 0))

                        printw.printf(dtf.format(LocalDateTime.now()) + ","); //For every new line of counts, shows current date and and time of the system.
                    r.write(POLL_FLAG_ADDRESS,0); // put it back down
                    intervalCounter++; // increment interval counter (gets reset by GO/STOP toggle)
                    for(int i = 0; i < MAX_WIDGETS; i++) { // loop through all widgets
                        NumWidget tmp = numwid.get(i);
                        if((tmp.getState() != 0) && (tmp.getState() != 16)) { // check if widget is configured for a channel
                            tmp.setAcc(tmp.getAcc() + r.read(translate[tmp.getState() - 1])); // if it is, poll the respective register
                            if(intervalCounter%requiredParams[2] == 0) { // check if we have reached integration interval                                   
                                tmp.setTextFieldText(String.format("%,d", tmp.getAcc())); // if so, update display
                                if(tglbtnWrite.isSelected()) 
                                    //s.append(tmp.getAcc()+ ","); // if write enabled, write to disk
                                    printw.printf(tmp.getAcc()+ ",");
                                    tmp.setAcc(0); // reset accumulator


                            }


                        }

我想要的是这样的:

在此处输入图像描述

相反,我得到:

在此处输入图像描述

这个想法是我想要每行数据的日期和时间戳,这就是第二行的用途。

我应该进行什么编辑?

编辑:我删除了屏幕截图并发布了代码片段。

标签: javaeclipse

解决方案


您应该使用 StringBuffer 来构建您的字符串,然后对最终结果执行 printf

StringBuffer sb = new StringBuffer();
sb.append(dtf.format(LocalDateTime.now()) + ",");
// Add your other items with sb.append()
// When done write out the end result
printw.printf(sb.toString());

同样正如 Andreas 在评论中提到的那样,您应该从中间追加中删除 \r\n ,并且只在最后添加它们。


推荐阅读