-3

I have a time for example 25 May 2021 02:00:00 PM EDT (it is not the local time now), and I need to convert that time to time in different time zone (for ex: Paris time) which should be 25 May 2021 08:00:00 PM CET How can I do this using java?

harmatii1
  • 21
  • 2
  • Does this answer your question? [Timezone conversion](https://stackoverflow.com/questions/6567923/timezone-conversion) – OldProgrammer May 27 '21 at 19:06
  • 2
    0. Don't use `Date`, `Calendar` or `SimpleDateFormat`. 1. Parse to a `ZonedDateTime`. 2. Specify the new timezone with something like `atZone`. – MC Emperor May 27 '21 at 19:12
  • Why not just consult time API from the internet rather than trying reinventing the wheel? – Malakai May 27 '21 at 19:18
  • the issue is when I use ZonedDateTime zonedDateTime = sentTime.atZone(ZoneId.of("Europe/Paris")); It just simply gives me this: 2021-05-27T15:17:50.805+02:00[Europe/Paris] , which is my current time and not converted to the "Europe/Paris" – harmatii1 May 27 '21 at 19:20
  • 1
    @reborn I would be happy to, but that's what I'm asking how to? – harmatii1 May 27 '21 at 19:21

1 Answers1

1

Avoid EDT and CET as these are not real time zones. Real time zones have a name in format of Continent/Region. For example, America/New_York and Europe/Paris.

ZonedDateTime zdt = 
    ZonedDateTime.of (
        2021 , 
        5 , 
        25 ,
        2 ,
        0 ,
        0 , 
        0 ,
        ZoneId.of( "America/Montreal" ) 
    )
;

Adjust to another zone.

ZoneId zoneTunis = ZoneId.of( "Africa/Tunis" ) ; 
ZonedDateTime zdtTunis = zdt.withZoneSameInstant( zoneTunis ) ;

Search to learn more. These topics have been addressed many times already on Stack Overflow.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • this looks like what I was looking for, I was missing using withZoneSameInstant and that's why it didn't work. Also any suggestions how to convert EDT to "America/New_York"? because that's what I have as an input – harmatii1 May 27 '21 at 19:42
  • @harmatii1 You will find many existing Questions on Stack Overflow about parsing such strings. The *java.time* classes can make a *guess* about those pseudo-zones but cannot perfectly map them to real time zones as some are not unique: CST, IST, etc. And educate the publisher of your data about proper data-exchange of date-time values: (a) using UTC (offset of zero) and (b) using only ISO 8601 formats. – Basil Bourque May 27 '21 at 22:08