fork download
  1. import java.time.OffsetDateTime;
  2. import java.time.ZoneOffset;
  3. import java.time.format.DateTimeFormatter;
  4. import java.time.format.DateTimeFormatterBuilder;
  5. import java.util.Locale;
  6.  
  7. class Main {
  8. public static void main(String[] args) {
  9. String strDateTime = "2023-01-09T23:51:44.595Z";
  10. String utcOffset = "UTC +6";
  11. DateTimeFormatter offsetFormatter = new DateTimeFormatterBuilder()
  12. .appendLiteral("UTC ")
  13. .appendOffset("+H:mm", "+0")
  14. .toFormatter(Locale.ROOT);
  15. ZoneOffset zoneOffset = offsetFormatter.parse(utcOffset, ZoneOffset::from);
  16. OffsetDateTime odtGiven = OffsetDateTime.parse(strDateTime);
  17. OffsetDateTime odtDesired = odtGiven.withOffsetSameLocal(zoneOffset)
  18. .withOffsetSameInstant(zoneOffset.UTC);
  19. System.out.println(odtDesired);
  20. }
  21. }
Success #stdin #stdout 0.12s 49452KB
stdin
Standard input is empty
stdout
2023-01-09T17:51:44.595Z