-3

I am trying to get the timestamp value for 2018-09-04 13:43:32.922000 by doing Timestamp.valueOf("2018-09-04 13:43:32.922000")

my expected output is 2018-09-04 13:43:32.922

but I am getting 2018-09-04 01:13:32.922

It might be due to different timezone because my team in India got the exact result but I am here in California gets the different result. Suggest the changes that can solve this problem.

Hulk
  • 6,399
  • 1
  • 30
  • 52
  • 3
    Don't use `Timestamp.valueOf`. Use `DateTimeFormatter` it allow to specify format and timezone of your data. – talex Jan 10 '19 at 07:18
  • 3
    The difference being 12 hours 30 minutes and India being involved (at offset +05:30) makes it very likely that this is indeed a time zone issue. The outdated `Timestamp` class is no friend of time zones. – Ole V.V. Jan 10 '19 at 07:41
  • 4
    Without code we can’t answer. Could you [create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve), please? – Ole V.V. Jan 10 '19 at 07:42

2 Answers2

7

tl;dr

myPreparedStatement.setObject( 
    Instant
    .parse( 
        "2018-09-04 13:43:32.922000"
        .replace( " " , "T" )
        .concat( "Z" )  
    ) 
    .atZone(
        ZoneOffset.UTC
    )
)

java.time

Suggest the changes that can solve this problem.

Never use java.sql.Timestamp.

Among the many flaws of that class is that the method you call is not documented to explain its behavior while parsing. It appears your JVM’s current default time zone is being silently applied with some adjustment. But the issue is moot.

That terribly-designed class was supplanted years ago by the modern java.time classes with the adoption of JSR 310, specifically Instant and OffsetDateTime.

Change your input string to standard ISO 8601 format by replacing the SPACE in the middle with a T.

String input = "2018-09-04 13:43:32.922000".replace( " " , "T" ) ;

Was your input intended to represent a moment in UTC, an offset of zero? If so, append a Z (pronounced Zulu).

String input = "2018-09-04 13:43:32.922000".replace( " " , "T" ).concat( "Z" ) ;

The Instant class represents a moment in UTC, always in UTC by definition.

Instant instant = Instant.parse( input ) ;

Your JDBC driver may optionally accept a Instant object.

myPreparedStatement.setObject( instant ) ;

If your JDBC driver does not support Instant, use OffsetDateTime. Support is required in JDBC 4.2 and later.

OffsetDateTime odt = instant.atOffset( ZoneOffset.UTC ) ; 
myPreparedStatement.setObject( odt ) ;

Notice how your JVM’s current default time zone at runtime is irrelevant, with no impact on the code above.


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
-3

I do not think the issue is due to different time zones. Its just that the output that you are getting is in 24 hour format and that needs to be converted to 12 hour format. Please refer How to convert 24 hr format time in to 12 hr Format? to convert the time to 12 hour format.

Chaitanya
  • 3,590
  • 14
  • 33
  • 3
    is it possible that in one machine i am getting it in 24 hr format and in another one, I get in12 hr format? Also if you see, the difference is 12hrs 30 mins. So, i believe that's not the case otherwise there will be 12 hrs of difference. – Ashish Mittal Jan 10 '19 at 07:20
  • 1
    `Timestamp.toString` always prints in 24 hour format. – Ole V.V. Jan 10 '19 at 07:44