1

Java8 Instant.now() gives the current time already formatted in UTC. I created a simple sandbox test on CompileJava.net to verify my intented results:

Timestamp createdDateUtc = Timestamp.from(Instant.now());
System.out.println(createdDateUtc);

LocalDateTime databaseUtcLocal = createdDateUtc.toLocalDateTime();
ZonedDateTime databaseUtcZoned = databaseUtcLocal.atZone(ZoneId.of("UTC"));
ZonedDateTime userTimezoneTime = databaseUtcZoned.withZoneSameInstant(ZoneId.of("Asia/Qatar")); 
System.out.println(userTimezoneTime.format(DateTimeFormatter.ofPattern("MM/dd/yyyy hh:mm:ss a")));

Note: it is currently 11:16 AM UTC on 12/01/2018.

When I run this code on the online compiler, I get the output:

2018-12-01 11:17:43.637
12/01/2018 02:17:43 PM

But when I run this code on my local JBOSS server, I get this output instead:

2018-12-01 03:18:07.464
12/01/2018 06:18:07 AM

What could be going wrong here? Is my server UTC clock incorrect? My local server's physical location is in California. While that shouldn't matter, it does seem odd that when I'm trying to display the UTC time using Instant.now() I get Pacific Time instead.

Any help much appreciated!

UPDATE

As pointed out by the accepted answer, the issue was my Timestamp.from() method:

System.out.println(Instant.now());
System.out.println(Timestamp.from(Instant.now()));

Output:

2018-12-01T11:48:33.153Z
2018-12-01 03:48:33.171

So now my issue has changed. If you have a similar issue, I found my answer on this Stack Overflow here. Instead of Timestamp.from(Instant.now()) I used this ugly mess:

Timestamp.valueOf(ZonedDateTime.of(LocalDateTime.now(), ZoneId.systemDefault()).withZoneSameInstant(ZoneId.of("UTC")).toLocalDateTime())
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
amallard
  • 1,189
  • 1
  • 12
  • 24
  • The `Timestamp` class has design ptoblems and is long outdated. And you have nothing to use it for here. For the simple way of converting from UTC to a specific time zone see for example [Java 8 – Convert Instant to ZonedDateTime](https://www.mkyong.com/java8/java-8-convert-instant-to-zoneddatetime/). Or to obtain the current time in a time zone, just use `ZonedDateTime.now(ZoneId.of("Asia/Qatar"))`. – Ole V.V. Dec 01 '18 at 23:52
  • The issue in not only with `Timestamp.from()`, but with all of the `Timestamp` class. Avoid it. If you thought you needed a `Timestamp` for your database, you probably don’t. A not too outdated JDBC driver should accept either an `Instant`, an `OFfsetDateTime` or a `LocalDateTime`. Try for example `yourPreparedStatement.setObject(6. Instant.now())` or `yourPreparedStatement.setObject(6. OffsetDateTime.now(ZoneOffset.UTC))`.. – Ole V.V. Dec 02 '18 at 00:11

1 Answers1

5

I think there is a problem in Timestamp.toString() method. If you will look inside it you will see:

public String toString () {

    int year = super.getYear() + 1900;
    int month = super.getMonth() + 1;
    int day = super.getDate();
    int hour = super.getHours();
    int minute = super.getMinutes();
    int second = super.getSeconds();
    // other code
}

Methods getHours(), getMinutes() and other are from Date class and based on documentation they interpret it by local timezone Date.getHours().

Ulad
  • 1,083
  • 8
  • 17