Bonagiri Anvesh
Bonagiri Anvesh

Reputation: 3

I wanted to generate the date using a string with various time zones in java

My input: 2021-01-07T18:54:00.000 - UTC(TZ)
My expected output:
GMT +5:30 - 2021-01-08T00:24:00.000
GMT -12:00 - 2021-01-07T06:54:00.000

Calendar calendar = Calendar.getInstance();
// This line is creating the calendar instance for the current day, I need to create calendar instance for the string which i receive from API
calendar.setTime(new Date());
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
        
//Here you set to your timezone
sdf.setTimeZone(TimeZone.getTimeZone("GMT+05:30"));
String temp1 = sdf.format(calendar.getTime());
DateTimeFormatter inputFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS", Locale.ENGLISH);
DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern("dd-MM-yyy", Locale.ENGLISH);
LocalDate date = LocalDate.parse(temp1, inputFormatter);
String formattedDate = outputFormatter.format(date);
System.out.println(formattedDate);

Upvotes: 0

Views: 63

Answers (2)

Anonymous
Anonymous

Reputation: 86272

I’ll take the big step back and suggest other and probably better ways to do your things. I don’t know your big picture, so it may be that not all of my general suggestions apply to your situation and requirements. But I consider it likely that they do.

  1. Do not keep nor process dates and times as strings. Keep them as Instant objects. When you take string input, parse it into an Instant. Only when you need to give string output, convert your Instant to a ZonedDateTime in the appropriate time zone and format it into the required string.
  2. Your strings are in ISO 8601 format, which is good for data interchange. ISO 8601 also allows for a GMT offset (UTC offset) in the string. I suggest that you and the systems with which you communicate exploit this possibility to reduce the risk of misunderstandings. UTC is denoted Z, for example, 2021-01-07T18:54:00.000Z. Other offsets are typically given in +HH:MM format, for example 2021-01-08T00:24:00.000+05:30 or 2021-01-07T06:54:00.000-12:00.
  3. Distinguish between a time zone and an offset from UTC or GMT. In Java terminology, a time zone is a place on earth with the historic, present and known future offsets used by the people in that place. Since offsets change during the history and will do that before we know it, for people in a time zone, use their time zone ID for conversion, not the GMT offset that you believe was right last time you checked. Specify the time zone in the region/city format, for example, Asia/Kolkata or Asia/Colombo.
  4. Use java.time, the modern Java date and time API, for all of your date and time work. It is much nicer to work with than the old classes you used in the question and your own answer, SimpleDateFormat, Calendar and TimeZone. They were poorly designed and are long outdated. You were already on the way with DateTimeFormatter and LocalDate from java.time. Just go all-in.

I guessed that it was not user input because while ISO 8601 is readable by humans, for user input you would probably have got a still more user-friendly, localized format. So I am assuming that you get an ISO 8601 string in UTC from some other system. Convert to Instant like this:

    String sDate1="2021-01-05T00:00:00Z"; // The trailing Z means UTC
    Instant inst = Instant.parse(sDate1);
    System.out.println(inst);

Output so far is:

2021-01-05T00:00:00Z

Notice that we didn’t need to specify any formatter. Instant parses ISO 8601 format in UTC with the trailing Z just like that. An Instant is a point in time independent of timezone.

On the same note, I am assuming that you need to give string output in some time zone to another system (maybe some front-end).

    ZoneId zone = ZoneId.of("Asia/Colombo");
    String output = inst.atZone(zone)
            .format(DateTimeFormatter.ISO_OFFSET_DATE_TIME);
    System.out.println(output);

2021-01-05T05:30:00+05:30

Again we did not need to construct any formatter ourselves. I just used one that was built in.

Links

Upvotes: 1

Bonagiri Anvesh
Bonagiri Anvesh

Reputation: 3

    String sDate1="2021-01-05T00:00:00";
    SimpleDateFormat isoFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
    //UTC
    isoFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
    Date date = isoFormat.parse(sDate1);
    System.out.println(isoFormat.format(date));
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);     
    //Here you set to your timezone
    isoFormat.setTimeZone(TimeZone.getTimeZone("GMT+05:30"));
    System.out.println(isoFormat.format(calendar.getTime()));
    //Here you set to your timezone
    isoFormat.setTimeZone(TimeZone.getTimeZone("GMT-12:00"));
    System.out.println(isoFormat.format(calendar.getTime()));

Upvotes: 0

Related Questions