java.sql.Timestamp has a timezone [duplicate] - java

This question already has answers here:
Is java.sql.Timestamp timezone specific?
(7 answers)
Is java.util.Date using TimeZone?
(5 answers)
Closed 3 years ago.
This post was edited and submitted for review 6 months ago and failed to reopen the post:
Original close reason(s) were not resolved
Everything I've read says a Timestamp is UTC and has no offset or timezone. However, I'm 99% positive that the MS Sql Server JDBC is reading in the value from the DB and setting it in my local timezone.
Update: Please note the referenced possible duplicate question asks how to set a timestamp using UTC datetime. My question is how can I read the UTC timestamp value from a database.
The debugger shows the object held as a GregorianCalender object who's timezone is Denver.

This appears to solve the problem. I'm guessing when it returns a LocalDateTime it does not use the timezone.
LocalDateTime localDT = timestamp.toLocalDateTime();
odt = localDT.atOffset(ZoneOffset.UTC);
updated with suggestion from #Ole V.V.

Related

Offset not calculated correctly at OffsetDateTime to Instant conversion in Java 8 [duplicate]

This question already has answers here:
ZonedDateTime America/Phoenix zone to GMT having issue [duplicate]
(3 answers)
Java Date Time conversion to given timezone
(3 answers)
Closed 7 months ago.
I would need the community's help because I could not find the answer in the Java documentation. I don't understand how the offset is taken into the math calculations when I try to convert an OffsetDateTime (ex: 2022-07-09T11:30:34) object to an Instant object. For example:
If we would run on OpenJDK 1.8 the command in a main function: OffsetDateTime.parse("2022-07-09T12:30:34+01:00", DateTimeFormatter.ISO_OFFSET_DATE_TIME).toInstant() the outcome would be an Instance of date-and-time 2022-07-09T11:30:34 when I would had expected an Instant of 2022-07-09T13:30:34. The difference is the hour. Why do I get it like this?
And the opposite using -01:00 will do the revet.
I apologize for not formatting my text correctly or if I missed something. I would appreciate it if my post would not be marked us not worthy. And sorry if the answer was already answered in a different thread, which I could not find.
Thank you in advance.

Java convert utc time to Local time [duplicate]

This question already has answers here:
Java: How do you convert a UTC timestamp to local time?
(3 answers)
Closed 5 years ago.
Hi I have a problem : from server I get a time and it looks like this :
"date":"2017-05-24T07:56:22Z"
But now in my local time is 09:56:22 how I can convert this ?
First you need to parse the date, for example:
Instant instant = Instant.parse("2017-05-24T07:56:22Z");
Assuming your time zone is correctly set, you can then simply use:
LocalTime localTime = instant.atZone(ZoneId.systemDefault()).toLocalTime();
If you want to use a specific time zone instead of the system default time zone:
LocalTime localTime = instant.atZone(ZoneId.of("Europe/London")).toLocalTime();

How to guess unknown dateTime format? [duplicate]

This question already has answers here:
How to get date datatype from sql database to java?
(2 answers)
Closed 7 years ago.
I have a DateTime which I received as JSON via a REST Servce of the Couchbase's Sync Gateway:
"2015-05-20T13:32:25.9999478-07:00"
I do not have the access to the Sync Gateway's configs.
I did no find any documentation about the default format of Sync Gateway's dateTime format.
I do not understand what the .9999478-07:00" means.
Is there a way to guess that somehow?
That is the ISO standard notation for date time
YYYY-MM-DD,
then a 'T' for time, HH:MI:SS.S* (fractional seconds),
+/- time zone (there are half our zones!)
Time and also time zone optional.
Look in the wikipedia or javadoc.
When no time zone the date time representation can be sorted alphabetical to be naturally ordered.

Time Zone cannot be changed in Calendar util [duplicate]

This question already has answers here:
Working with various Calendar TimeZone in Java (without using Joda Time)
(3 answers)
Closed 9 years ago.
This sentence is supposed to get time information at Chicago time zone:
Calendar.getInstance(TimeZone.getTimeZone("America/Chicago")).getTime();
My problem is no matter what string I put in getTimeZone(), result would be changed.
Could anyone explain this situation?
The key thing to understand is that a java.util.Date represents UTC only - it has no time zone information. Time zones are presentation layer only - they are used to figure out how to display the time represented by the java.util.Date.
So if you use SimpleDateFormat or the Calendar.get(...) methods, the time zone will be taken into account.

Java parse oracle timestamp to date.util.date [duplicate]

This question already has answers here:
Conversion of string with AM/PM date-time, from Oracle database
(3 answers)
Closed 6 years ago.
i want to parse oracle timestamp (01-MAY-12 01.00.47.000000000 PM) to java.util.Date
i used this:
Date dateStart=new SimpleDateFormat("yy-MM-dd HH:mm:ss.S").parse("01-MAY-12 01.00.47.000000000 PM");
but i get this error
java.text.ParseException: Unparseable date: "2012-5-1.13.0. 47. 0"
You shouldn't have to parse anything. Use one of the ResultSet.getTimestamp() methods, and you'll have a java.sql.Timestamp object directly, which extends java.util.Date.
java.sql.Timestamp ts = myResultSet.getTimestamp( … );
And this will have the additional advantage of being portale across databases and locales.
"yy-MM-dd"?
"01-MAY-12"
Is your day number really "12" and your year "01"?
And how come your error shows "2012-5-1.13.0. 47. 0", which is presumably a date in yet another format?
If you are trying to access it using JDBC then as #JB Nizet suggested use getTimestamp() or if you just have String and need to parse to Date then do it by following
Try with following format
01-MAY-12 01.00.47.000000000 PM
yy-MMM-dd hh.mm.ss.SSSSSSSSSS a
Working demo

Categories

Resources