简体   繁体   中英

How to add date and time separately in java

package Csv;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class JtoCModified {
    private static final String COMMA_DELIMITER = ",";
    private static final String NEW_LINE_SEPARATOR = "\n";
    private static final String FILE_HEADER = "OPV,IKW,OKW,OPI,IPI,NA1,BVV,BVI,NA2,NA3,IPV,_IF,_OF,NA4,SERIAL,NA5,NA6,STATUS,Date,Time,Device_id";
    private static final String path ="D:\\Data1.csv";
    private static final String A001 = null;
    String Device_id = A001;
    private static final int RECORD_COUNT = 20; // * 1000;
  //  static final String DATEFORMAT = "yyyy-MM-dd HH:mm:ss";
    static final String DATE_FORMAT = "yyyy-MM-dd";
    static final String TIME_FORMAT = "HH:mm:ss";
    private static final int ADD_MINUTES = 2;
    static final String FromDate = "2016-01-01 00:00:00";



    public static void main(String[] args) throws Exception {
        File file = new File(path);
        if (file.exists()) {
            file.delete();
        }

        List<String> records = new ArrayList<String>();
        StringBuffer record = new StringBuffer();
        DateFormat df = new SimpleDateFormat(DATE_FORMAT);
        DateFormat d_f = new SimpleDateFormat(DATE_FORMAT);
        DateFormat tf = new SimpleDateFormat(TIME_FORMAT);
        Calendar cal = Calendar.getInstance();
        cal.setTime(df.parse(FromDate));
        for (int i = 1; i <= RECORD_COUNT; i++) {
            if (i % 100000 == 0) {
                records = new ArrayList<String>(RECORD_COUNT);
            }
            for (int j = 0; j < 100; j++) {
                int OPV = 230 + j % 15; // 230 - 244 by 1
                double IKW = 1.3 + j % 17 * 0.1; // 1.3 - 2.9 by 0.1
                double OKW = 0.01 + j % 49 * 0.01; // 0.01 - 0.49 by 0.01
                double OPI = 0.05 + j % 105 * 0.01; // 0.05 - 1.09 by 0.01
                double IPI = 0.00 + j % 8 * 0.01;
                int NA1 = 000;
                int BVV = 104 + j % 13;
                double BVI = 1.3 + j % 15 * 0.8;
                int NA2 = 000;
                int NA3 = 000;
                int IPV = 241 + j % 1;
                int _IF = 000;
                int _OF = 000;
                int NA4 = 000;
                int SERIAL = 12345;
                int NA5 = 000;
                int NA6 = 000;
                int STATUS = 00 + j % 01;
/*              record.delete(0, record.length());
                record.append(d_f.format(cal.getTime()));
                record.append("\t");
                record.append(tf.format(cal.getTime()));
                record.append("\t");
                record.append("\t");

*/              record.delete(0, record.length());
                addToBuffer(record, OPV);
                addToBuffer(record, IKW);
                addToBuffer(record, OKW);
                addToBuffer(record, OPI);
                addToBuffer(record, IPI);
                addToBuffer(record, NA1);
                addToBuffer(record, BVV);
                addToBuffer(record, BVI);
                addToBuffer(record, NA2);
                addToBuffer(record, NA3);
                addToBuffer(record, IPV);
                addToBuffer(record, _IF);
                addToBuffer(record, _OF);
                addToBuffer(record, SERIAL);
                addToBuffer(record, NA5);
                addToBuffer(record, NA6);

                record.append(STATUS);
                record.append("\t");
                record.append("\t");

                record.append("\n");
                record.append("\t");
                record.append("\t");
                cal.setTime(df.parse(FromDate));

                cal.add(Calendar.MINUTE, ADD_MINUTES);
                record.append("\t");
                record.append(d_f.format(cal.getTime()));
                record.append("\t");
                record.append(tf.format(cal.getTime()));
                record.append("\t");
                record.append("\t");


                records.add(record.toString());
                // System.out.println(record);
            }
      }
        record.append("\t");

        writeRaw(records);
    }

    private static void writeRaw(List<String> records) throws IOException {
        File file = new File(path);
        try {
            FileWriter writer = new FileWriter(file, true);
            writer.append(FILE_HEADER.toString());
            writer.append(NEW_LINE_SEPARATOR);
            boolean alreadyExists = false;
            if (!alreadyExists) {
                writer.append(NEW_LINE_SEPARATOR);
                write(records, writer);

            }
            System.out.println("CSV file was created successfully !!!");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
        }
    }

    private static void write(List<String> records, FileWriter writer) throws IOException {
        long start = System.currentTimeMillis();
        for (String record : records) {
            System.out.println(record);

            writer.append(record);
        }
        writer.flush();
        writer.close();
        long end = System.currentTimeMillis();
        System.out.println((end - start) / 1000f);

        System.out.println("records");

    }

    private static void addToBuffer(StringBuffer buffer, Object data) {

        buffer.append(data);
        buffer.append(", ");
    }

}

I want to add date and time separated by a tab space, used \\t still i am not getting the required output.Here, I am getting date and time together(without a tab separation) like 2016-01-01 00:02:00 . I want to print both separately, for example: 2016-01-01 00:02:00 .

I have run your program and the output is a little weird, since your overall format of the csv is not right, but I think you will handle that later.

Coming to your question, the data & time are actually separated by a tab space in the output as required . It doesn't show up properly on excel due to formatting issues.

So you may use this:

record.append((d_f.format(cal.getTime()))+"        "+tf.format(cal.getTime()));

It you want to show the date & time separately in different columns use this :

record.append((d_f.format(cal.getTime()))+", "+tf.format(cal.getTime()));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM