I want to add UTC date and time to my mysql database as timestamp. Previously I tried using SimpleDateFormat as:
//UTC based issued date and expiry date SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); sdf.setTimeZone(TimeZone.getTimeZone("UTC")); final String currentUtc= sdf.format(new Date()); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date currentDate = (Date) simpleDateFormat.parse(currentUtc); //java.util.date It worked but I found that use of SimpleDateFormat is not recommendated. So now I am using Instant date time api as:
Instant currentInstant = Instant.now().truncatedTo(ChronoUnit.SECONDS); //gives UTC datetime which is correctly giving me current time in UTC. The problem is then when I add this instant to my database it gets converted to local date by adding my UTC time offset to that instant which I don't want.
//adding UTC time to database but not working ps.setTimestamp(1, Timestamp.from(currentInstant)); So how to add UTC date without adding UTC time offset to my database? Thank you in advance.
timestampto work better thandatetimefor UTC datetimes.