0

My Server is running on UTC timezone, I want to save time of any operation in database in IST format, I tried various ways including following.

Date ISTDate = Calendar.getInstance(TimeZone.getTimeZone("Asia/Kolkata")).getTime();
TimeZone.setDefault(TimeZone.getTimeZone("Asia/Kolkata"));
jobRepository.save(new IncremetalJob(ISTDate,ISTDate, JobState.IN_PROGRESS));

Everytime UTC date is being saved, can someone suggest here.

TheCurious
  • 593
  • 1
  • 4
  • 29
  • 2
    I recommend you keep it in UTC. Also, do not use the error-prone legacy date-time API. Check [this answer](https://stackoverflow.com/a/67752047/10819573) and [this answer](https://stackoverflow.com/a/67505173/10819573) to learn how to use `java.time` API with JDBC. – Arvind Kumar Avinash Dec 16 '22 at 11:31
  • 1
    [This](https://stackoverflow.com/q/67918503/10819573) and [this](https://stackoverflow.com/q/30651210/10819573) can also be helpful to you. – Arvind Kumar Avinash Dec 16 '22 at 11:35
  • 2
    A time zone aware datetime object in Java is a `ZonedDateTime` from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/index.html). – Ole V.V. Dec 16 '22 at 15:19
  • I strongly recommend you don’t use `Date`, `Calendar` and `SimpleDateFormat`. Those classes are poorly designed and long outdated, the last in particular notoriously troublesome. Instead as hinted use `ZonedDateTime` and `ZoneId`, both from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Dec 16 '22 at 20:10

0 Answers0