I stored timezone in string format in my database and I am rendering a table in which I have start date and end date and below is my implementation
Suppose I have time zone Asia/Colombo I am converting a date 2021-06-08T00:00:00 to time zone of Asia/Colombo
By this logic, I am getting date in this format Tue Jun 08 00:00:00 IST 2021 but I want output in this format 2021-06-08T00:00:00 (with specific timezone)
What should I change in the current implementation to get desired output?
and also whenever I am returning date in Date type it is getting changed
Which is better returning date in string or Date
List<TimeZone> timeZoneList = TimeZone.findAll()
String zone = timeZoneList ? timeZoneList[0]?.currentTimeZone : null
String zoneId = zone ? TimeZone.getTimeZone(zone).ID : TimeZone.getDefault().ID
Calendar calendar = Calendar.getInstance();
calendar.setTime(dateToConvert);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
System.out.println(sdf.format(calendar.getTime()));
sdf.setTimeZone(TimeZone.getTimeZone(zoneId));
String convertedDate = sdf.format(calendar.getTime())
println("converted date in string format ----->>>>>>>>>>> " + convertedDate)