jeudi 1 octobre 2015

Date and writing issues when exporting data to text file in JAVA

I'm running this piece of code to get the date and store it in a log file

String myDate ="";

                Date date = new Date();
                DateFormat df = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss");


                df.setTimeZone(TimeZone.getTimeZone("America/Chicago"));
                myDate= df.format(date);


                sql = "UPDATE download SET ts_" + ae_num + " = DateTime('now','localtime')" + " WHERE tipid = ?;";
                System.out.println(sql);
                stmt = c.prepareStatement(sql);
                stmt.setString(1, tipNum);

                stmt.executeUpdate();
                //c.commit();

                stmt.close();

                c.close();

                try
                {
                    String content = "Username: "+ uname + " File_Name: "+ filename+" Downloaded_Time: " + myDate;
                    File file = new File("C:\\log\\log.txt");

                    if(!file.exists())
                    {
                        file.createNewFile();
                    }

                    PrintWriter pw =new PrintWriter(new FileWriter(file,true));
                    pw.println(content);
                    pw.close();


                }catch (IOException e)
                {
                    e.printStackTrace();
                }

The problem that inside the text file the month is messed up Example:

Downloaded_Time: 2015-48-01 08:48:45

It should be

Downloaded_Time: 2015-10-01 08:48:45

The second problem is, if there is any second webserver that runs the same exact code, it won't be able to write the log file unless it was renamed. Why?

I need to store some information (including date) to the text file and I need all users to be able to write on the same one. Any tips ?

Aucun commentaire:

Enregistrer un commentaire