-1

I want to convert date and time to user requested timezone. date and time is in GMT format. i tried got the solution but the final string contains GMT String in resultant date like (2019-09-18T01:44:35GMT-04:00). i don't want GMT String in the resultant output.

public static String cnvtGMTtoUserReqTZ(String date, String format, String timeZone) {
    // null check
    if (date == null)
        return null;

    // create SimpleDateFormat object with input format
    SimpleDateFormat sdf = new SimpleDateFormat(format);

    // set timezone to SimpleDateFormat
    sdf.setTimeZone(TimeZone.getTimeZone(timeZone));
    try {
        // converting date from String type to Date type
        Date _date = sdf.parse(date);

        // return Date in required format with timezone as String
        return sdf.format(_date);

    } catch (ParseException e) {
        //log.info("Exception in cnvtGMTtoUserReqTime ::: " + e);
    }
    return null;
}

Actual Output : 2019-09-18T01:44:35GMT-04:00

Expected Output: 2019-09-18T01:44:35-04:00

xtratic
  • 4,600
  • 2
  • 14
  • 32
Naveen
  • 346
  • 2
  • 10
  • Dude.. what even *is* the format you used? Work with us here please.. – xtratic Sep 18 '19 at 14:10
  • 1
    Also, if you're parsing with `sdf` and formatting the resulting date with `sdf` then nothing will have changed.. – xtratic Sep 18 '19 at 14:13
  • If the ONLY thing you want to do is to remove the `GMT` string, why even parse the date. Can't you just remove that from the string? – DanielBarbarian Sep 18 '19 at 14:17
  • Hi @DanielBarbarian I can replace `GMT` with '' but I want to use SimpleDateFormatter Calss – Naveen Sep 18 '19 at 15:24
  • 1
    You shouldn’t want to use `SimpleDateFormat`, `TimeZone` and `Date`. Those classes are poorly designed and long outdated, the first in particular notoriously troublesome. Instead use `DateTimeFormatter`, `ZoneId` and other classes from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Sep 18 '19 at 19:02
  • [A Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example), please? From what you’ve posted I can’t understand the details of how your code is working nor what goes wrong. – Ole V.V. Sep 18 '19 at 19:05

2 Answers2

0

Use these formats:
fromFormat = "yyyy-mm-dd'T'HH:mm:sszXXX"
toFormat = "yyyy-mm-dd'T'HH:mm:ssXXX"

For more details see examples listed here

Dulaj Kulathunga
  • 1,248
  • 2
  • 9
  • 19
sgrpwr
  • 431
  • 3
  • 12
0

Proces datetime objects, not strings

Your question is put in the wrong way, which is most likely due to a design flaw in your program. You should not handle date and time as strings in your program. Always keep date and time in proper datetime objects such as Instant, OffsetDateTime and ZonedDateTime. The mentioned classes are from java.time, the modern Java date and time API, which is the best we have for keeping and processing datetime data.

So your question may for example become: How to convert a moment in time to user requested timezone? A moment in time is represented by an Instant object. And the answer to the question is:

    ZoneId userRequestedTimeZone = ZoneId.of("America/New_York");

    Instant moment = Instant.parse("2019-09-18T05:44:35Z");
    ZonedDateTime userDateTime = moment.atZone(userRequestedTimeZone);
    System.out.println(userDateTime);

Please substitute your user’s desired time zone where I put America/New_York. Always give time zone in this format (region/city). Output from the snippet as it stands is:

2019-09-18T01:44:35-04:00[America/New_York]

Assuming that you don’t want the [America/New_York] part of the output, format the datetime to the string that you want:

    String dateTimeWithNoZoneId = userDateTime.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME);
    System.out.println(dateTimeWithNoZoneId);

2019-09-18T01:44:35-04:00

The latter output is in ISO 8601 format. This format is good for serialization, that is, if you need to convert the datetime to a machine readable textual format, for example for persistence or exchange with other systems. While also human readable, it’s not what your user prefers to see. And as I said, it’s certainly not what you should be handling and processing inside your program.

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161