0

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)
chaitanya gupta
  • 302
  • 5
  • 25
  • It looks like you need to store the date only, not the time, is that correct? And if so, has your database got a `date` data type? Use it. And then use `LocalDate` in Java. – Ole V.V. Jun 08 '21 at 15:57
  • I recommend you don’t use `TimeZone`, `Calendar` and `SimpleDateFormat`. Those classes are poorly designed and long outdated, the last in particular notoriously troublesome. Instead use `LocalDate` or other classes from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Jun 08 '21 at 15:58

2 Answers2

2

You can parse the input date time into LocalDateTime and convert ZoneDateTime

 String inputDate = "2021-06-08T00:00:00";

 ZonedDateTime zonedDateTime = LocalDateTime.parse(inputDate).atZone(ZoneId.of("Asia/Colombo"));

 System.out.println(zonedDateTime); //2021-06-08T00:00+05:30[Asia/Colombo]
Ryuzaki L
  • 37,302
  • 12
  • 68
  • 98
1

java.time

The java.util Date-Time API and their formatting API, SimpleDateFormat are outdated and error-prone. It is recommended to stop using them completely and switch to the modern Date-Time API*.

Demo using java.time, the modern Date-Time API:

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        String strDateTime = "2021-06-08T00:00:00";
        ZonedDateTime zdtUtc = LocalDateTime.parse(strDateTime).atZone(ZoneId.of("Etc/UTC"));
        ZonedDateTime zdtColombo = zdtUtc.withZoneSameInstant(ZoneId.of("Asia/Colombo"));
        System.out.println(zdtColombo);

        // Output in a custom format
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss", Locale.ENGLISH);
        String formatted = dtf.format(zdtColombo);
        System.out.println(formatted);
    }
}

Output:

2021-06-08T05:30+05:30[Asia/Colombo]
2021-06-08 05:30:00

ONLINE DEMO

If your column type is DATE, check this answer.

If your column type is TIME WITH TIMEZONE, check this answer.

How to convert LocalDate to ZonedDateTime?

import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        // A sample LocalDate (to be retrieved with JDBC)
        LocalDate date = LocalDate.of(2021, 6, 8);
        
        ZonedDateTime zdtUtc = date.atStartOfDay(ZoneId.of("Etc/UTC"));
        ZonedDateTime zdtColombo = zdtUtc.withZoneSameInstant(ZoneId.of("Asia/Colombo"));
        System.out.println(zdtUtc);
        System.out.println(zdtColombo);

        // Output in a custom format
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss", Locale.ENGLISH);
        System.out.println(dtf.format(zdtUtc));
        System.out.println(dtf.format(zdtColombo));
    }
}

Output:

2021-06-08T00:00Z[Etc/UTC]
2021-06-08T05:30+05:30[Asia/Colombo]
2021-06-08 00:00:00
2021-06-08 05:30:00

ONLINE DEMO

Learn more about the modern Date-Time API from Trail: Date Time.


* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110