Caveat: This answer does not use Timestamp
, because I don't know wha the OP is using Timestamp
when they really should be using the newer date/time APIs available in Java 8+
All the date/time objects of Java are simply containers for the amount of time which has passed since a given anchor point in time (ie the number of milliseconds since the Unit epoch)
Their toString
implementations tend to simply provide a human readable representation of the container based on the JVM's currently configured locale. This isn't really meant for "display" purpose, this is why the date/time formatters exist.
For example...
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
System.out.println(LocalDateTime.now().format(dtf));
But I want the date/time format in a specific format for insertion/retrieval from the database
Actually, you don't. This is the point of databases using date/time column types and JDBC date/time objects - the format is irrelevant - it's the data that's important. Again, the solution is the same, use a "object" to represent the "data" and a formatter to "display" it
I actually tried your method before but when i print it to csv file. It shows up as 2018-08-26T14:42:19.514864900, which is different from the output of system.out.println. How can i mitigate that ?
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class Test {
public static void main(String[] args) {
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
File file = new File("test.txt");
try (PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(file, false)))) {
pw.print("date,");
pw.print(LocalDateTime.now().format(dtf));
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
Which generates a file containing...
date,2018-08-26 16:55:16.032