java


How do I make java.sql.Timestamp UTC time? [duplicate]


This question already has an answer here:
Is java.sql.Timestamp timezone specific?
4 answers
public static void main(String[] args) {
LocalDateTime ldt = LocalDateTime.now();
ZonedDateTime zdt = ZonedDateTime.of(ldt, ZoneId.systemDefault());
Instant instant = Instant.from(zdt);
Timestamp timestamp = Timestamp.from(instant);
System.out.println(ldt + "\n");
System.out.println(zdt + "\n");
System.out.println(instant + "\n");
System.out.println(timestamp + "\n");
}
And it prints:
2017-05-07T18:13:26.969
2017-05-07T18:13:26.969-04:00[America/New_York]
2017-05-07T22:13:26.969Z
2017-05-07 18:13:26.969
How can I make an SQL Timestamp save with the same time as the Instant? I need to be able to get the Timestamp from anywhere and convert it to whatever time it happens to be in that part of the world. The problem is that it keeps saving as the same time as whatever my system clock happens to be set at.
You are best to get a Timestamp from a LocalDateTime, rather than from an Instant.
The first step is to take your ZonedDateTime and convert it to GMT:
ZonedDateTime gmt = zdt.withZoneSameInstant(ZoneId.of("GMT"));
Then you can convert it to a Timestamp via a LocalDateTime:
Timestamp timestamp = Timestamp.valueOf(gmt.toLocalDateTime());
You might need to use the specific overload of setTimestamp that takes a Calendar object if you want to represent a "TIMESTAMP WITH TIME ZONE" value.

Related Links

Having difficulty joining a ManyToMany Table in Spring to get hql to work
issues while assigning final arraylist some values and adding another value later
Android: onClickListener method does not reach end because it ends first
how to convert first hello SSL message into jdk ClientHello.java object?
How to Run a Local Java Batch Job with a WebSphere Server in IntelliJ?
how do we create custom annotation for rest service?
How does Java maintain function StackTrace internally
Java Exception: Default constructor needed? There is already one
SqlServer convert to mySql5.5 ,jpa #GeneratedValue does not work
How to append multiple comma separated values to same key
ClassNotFoundException: ContextLoaderListener while migrating Spring to 4.2.4
How to add and edit user in rest server
exception handling framework in spring java
Specifying a forward lookup jar in ant file
Spring AOP Proxy Issues while using Pointcut with javax.sql.DataSource.getConnection(..)
Why does my PolyLine keep drawing from origin (0,0)?

Categories

HOME
ms-access
sendgrid
image
netbeans
reflection
nullpointerexception
plot
kalman-filter
spring-cloud-stream
twitter-bootstrap-4
packages
jgroups
constraint-programming
podio
bs4
cross-validation
applepay
primary-key
quicklook
etl
spring-tool-suite
lombok
undefined
database-replication
amazonsellercentral
web-sql
core-text
cultureinfo
sqlcipher
smb
info.plist
spring-mybatis
minitab
gitignore
azure-sql-database
ghost4j
html5-fullscreen
suricata
adobe-premiere
jquery-bootgrid
dynamics-crm-2013
objectlistview
acoustics
slick-3.0
businessworks
lowpass-filter
slickedit
lync-client-sdk
eventkit
angular-strap
time-and-attendance
vmware-tools
blackberry-10
livequery
oauth2client
netmq
historian
feedback
rgeo
embedded-code
ibaction
composite
graph-api-explorer
device-orientation
directoryservices
javax.mail
android-nested-fragment
markers
sgen
fluentautomation
mechanize-ruby
access-rights
gridfs
coverflow
bulkloader
flash-builder4.5
eventual-consistency
android-screen-support
ember-app-kit
limejs
jelly
libstdc++
amazon-appstore
bigcouch
genshi
invite
self-extracting
jmock
remember-me
web-application-design
infrastructure
swing-app-framework
mdac
geneva-server
multiple-languages

Resources

Encrypt Message