-1

I am receiving a UTC timestamp "2021-02-11T09:00:00-07:00 --UTC" in a json Object which has to be converted to some other zone timestamp. Once converted to the desired zone timestamp I need to extract day, day number, date, month, year, time, timezone and store it in a map.

Kindly advise.

James Z
  • 12,209
  • 10
  • 24
  • 44
  • 2
    use https://docs.oracle.com/javase/8/docs/api/java/time/ZonedDateTime.html class it has all the method you need to convert to different timezone and to get the day and month and so on then use any suitable map you like to store the data –  Sep 22 '21 at 08:29
  • Look into `OffsetDateTime`, `ZoneId` and `ZonedDateTime` from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). That shouldn’t be too hard. Make an attempt, and if doesn’t go smoothly, please ask a new question about any trouble you’re having. – Ole V.V. Sep 22 '21 at 08:58
  • The date and time in your string are at UTC offset -07:00. The trailing ` --UTC` confuses me. Is it really part of the string? Do you know whether it means something? – Ole V.V. Sep 22 '21 at 09:00
  • (Offset -7 meaning that your “UTC timestamp” could for instance originate from America/Creston or America/Phoenix time zone.) – Ole V.V. Sep 22 '21 at 12:03
  • 1
    @OleV.V. -I think it doesn't have any meaning as the info is being passed down through multiple components it is just for an indication of the timezone. Thanks for your advise. I have seen your contribution alot on this platform as I was going through others solution.:) – Nikita Dixit Sep 22 '21 at 13:00
  • 1
    @justsomeone Thank you It is indeed a very helpful document. – Nikita Dixit Sep 22 '21 at 13:00

1 Answers1

1

Example:

public static void main(String[] args) {
    String input = "2021-02-11T09:00:01-07:00 --UTC";
    String timeStampCleanedUp = input.replaceFirst("\\s+--UTC", "");

    ZonedDateTime zonedDateTimeTmp = ZonedDateTime.parse(timeStampCleanedUp);

    //Conevert to other TimeZone, such as 'America/New_York':
    ZonedDateTime  zonedDateTime = zonedDateTimeTmp.withZoneSameInstant(ZoneId.of("America/New_York"));
    System.out.printf("Time stamp after zone time conversion: from '%s' to '%s'%n%n", zonedDateTimeTmp, zonedDateTime);

    //day, daynumber, date, month, year, time,timezone
    Map<String, String> result = new HashMap<>();

    result.put("DayOfWeek", zonedDateTime.getDayOfWeek().name()); //Day
    result.put("DayOfMonth", String.valueOf(zonedDateTime.getDayOfMonth())); //DayNumber
    result.put("Month", zonedDateTime.getMonth().name()); //Month
    result.put("Date", zonedDateTime.toLocalDate().toString()); //Date
    result.put("Month", zonedDateTime.getMonth().name()); //Month
    result.put("Year", String.valueOf(zonedDateTime.getYear())); //Year
    result.put("Time", zonedDateTime.toLocalTime().format(DateTimeFormatter.ISO_LOCAL_TIME)); //Time
    result.put("Offset", zonedDateTime.getOffset().getId()); //Offset
    result.put("TimeZone", zonedDateTime.getZone().getId()); //TimeZone

    result.keySet().stream().sorted().forEach(key -> System.out.printf("Key: '%s' => Value: '%s'%n", key, result.get(key)));
}

Output:

Time stamp after zone time conversion: from '2021-02-11T09:00:01-07:00' to '2021-02-11T11:00:01-05:00[America/New_York]'

Key: 'Date' => Value: '2021-02-11'
Key: 'DayOfMonth' => Value: '11'
Key: 'DayOfWeek' => Value: 'THURSDAY'
Key: 'Month' => Value: 'FEBRUARY'
Key: 'Offset' => Value: '-05:00'
Key: 'Time' => Value: '11:00:01'
Key: 'TimeZone' => Value: 'America/New_York'
Key: 'Year' => Value: '2021'

You can see Available Zone Ids by using ZoneId.getAvailableZoneIds():

ZoneId.getAvailableZoneIds().stream().forEach(System.out::println);

You can read more about ZonId and ZoneOffset here: https://docs.oracle.com/javase/10/docs/api/java/time/ZoneId.html https://docs.oracle.com/javase/10/docs/api/java/time/ZoneOffset.html

DigitShifter
  • 801
  • 5
  • 12
  • Thanks I appreciate the effort you put to solve my query. Will test it soon and will get back in case of any queries.:) – Nikita Dixit Sep 22 '21 at 13:02
  • Happy that you appreciate the effort. Thanks! – DigitShifter Sep 23 '21 at 10:29
  • Hey @DigitShifter just a quick question here you have added the timezone "America/New_York" but can this be more generic in regards to various offsets. I am not sure how can I fetch a timezone out of the given offset. This code will fail if the offset changes. Kindly advise. – Nikita Dixit Sep 27 '21 at 13:16
  • final List timeZoneByUtc = ZoneId.getAvailableZoneIds().stream().map(ZoneId::of) .filter(z -> z.getRules().getOffset(Instant.now()).equals(ZoneOffset.ofHours(-7))) .collect(Collectors.toList()); – Nikita Dixit Sep 27 '21 at 13:33
  • I tried the above code for -7 but it is a list and which one to pick. – Nikita Dixit Sep 27 '21 at 13:34
  • Yes, you can set zonedDateTime with an offset instead of a zone name, see: ZonedDateTime zonedDateTime = zonedDateTimeTmp.withZoneSameInstant(ZoneOffset.ofHours(-7)); – DigitShifter Sep 29 '21 at 11:07
  • Thanks @DigitShifter. Now we will be getting the direct timezone value like PST, CST etc.. as day light savings can make diffrence if only the offset is considered. – Nikita Dixit Oct 01 '21 at 13:34