1

I have a timestamp saved in Azure SQL Database in the format of 2004-09-23 10:52:00. When I fetch the value, I am using sqlrowset where I am using getString("last_updated_user") and getting output like this "2004-09-23 10:52:00.0". When I tried using getTimeStamp("last_updated_user"), I am getting in microseconds. Can someone help me on formatting it to "2004-09-23 10:52:00" based on some in-built functions like .format or something and not by just removing the decimal by replace or substring?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
anto clinton
  • 87
  • 1
  • 7
  • 2
    Java a look at the `java.time` API which should provide all you need. – Thomas Aug 27 '21 at 11:51
  • Does this answer your question? [java.util.date to String using DateTimeFormatter](https://stackoverflow.com/questions/42210257/java-util-date-to-string-using-datetimeformatter) – Hulk Aug 27 '21 at 11:55
  • Or perhaps https://stackoverflow.com/questions/4772425/change-date-format-in-a-java-string – Hulk Aug 27 '21 at 11:56
  • 3
    What is the actual data type of your column `last_updated_user`? – Basil Bourque Aug 27 '21 at 13:19
  • 1
    Don’t get your date and time as a string from your database. Get a `LocalDateTime`. See [Arvind Kumar Avinash’ good answer here](https://stackoverflow.com/a/67505173/5772882). – Ole V.V. Aug 27 '21 at 18:13

2 Answers2

2

Solution using java.time, the modern Date-Time API:

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        String d = "2004-09-23 10:52:00.0";
        DateTimeFormatter dtfInput = DateTimeFormatter.ofPattern("u-M-d H:m:s.S", Locale.ENGLISH);
        LocalDateTime ldt = LocalDateTime.parse(d, dtfInput);

        DateTimeFormatter dtfOutput = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss", Locale.ENGLISH);
        String str = ldt.format(dtfOutput);
        System.out.println(str);
    }
}

Output:

2004-09-23 10:52:00

Online demo

Learn more about the modern Date-Time API* from Trail: Date Time.


* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
2

JDBC 4.2 and java.time

I am guessing a bit here since I don’t know Azure SQL database. What you are reporting sounds like you are having an SQL timestamp or equivalent in your database. Assuming that your JDBC driver is JDBC 4.2 compliant get a LocalDateTime (not a string) from your database:

    LocalDateTime lastUpdatedUser
            = sqlrowset.getObject("last_updated_user", LocalDateTime.class);

I have further assumed that sqlrowset is a javax.sql.RowSet. To format into your desired string use a formatter like this one:

private static final DateTimeFormatter FORMATTER
        = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss");

Now we may do:

    System.out.println("LocalDateTime retrieved: " + lastUpdatedUser);
    String formatted = lastUpdatedUser.format(FORMATTER);
    System.out.println("Formatted:               " + formatted);

Example output:

LocalDateTime retrieved: 2004-09-23T10:52
Formatted:               2004-09-23 10:52:00
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161