-1

Basically, I am trying to obtain the system current timestamp, including milliseconds too with the following method

Timestamp timestamp = new Timestamp(System.currentTimeMillis()); System.out.println(timestamp);

Problem is sometimes it would be display 2018-08-26 14:17:16.94

I want it to display 2018-08-26 14:17:16.940 instead, so there is always 3 digits in the milliseconds.

How can I do that ?

Thank you very much.

mynameisJEFF
  • 4,073
  • 9
  • 50
  • 96
  • 1
    Use a date/time formatter - but the real question is, why are you using `Timestamp`? You need to understand that `Timestamp#toString` is simply make a "human readable" result of the value it represents, typically based on the current JVM's configuration, it's not meant for "display", that's what formatters are for – MadProgrammer Aug 26 '18 at 06:31
  • The `java.sql.Timestamp` class is not appropriate here. Intended only for exchanging data with database. Now supplanted by `java.time.Instant` as of JDBC 4.2. For current moment in your time zone, use `ZonedDateTime`. Generate a `String` having text representing the date-time value by using `DateTimeFormatter`. This has been covered many many many times already on Stack Overflow. Search before posting. – Basil Bourque Aug 26 '18 at 15:27

1 Answers1

3

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
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • 1
    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 ? – mynameisJEFF Aug 26 '18 at 06:46
  • 2
    How are you printing it to the CSV? `LocalDateTime.now().format(dtf)` will print the current date/time in the specified format of `"yyyy-MM-dd HH:mm:ss.SSS"` – MadProgrammer Aug 26 '18 at 06:47
  • 1
    I am using `fw = new FileWriter(f,false);bw = new BufferedWriter(fw);out = new PrintWriter(bw);out.print(LocalDateTime.now().format(dtf))` – mynameisJEFF Aug 26 '18 at 06:49
  • 1
    Well, based on that, it should be working. Consider providing a [runnable example which demonstrates your problem](https://stackoverflow.com/help/mcve). Since you're writing to a CSV file, you should also consider using an appropriate library, it's a little more complicated then just putting a `,` between your value – MadProgrammer Aug 26 '18 at 06:51