Java Data Type How to - Get current date time in different timezone








Question

We would like to know how to get current date time in different timezone.

Answer

/*from w ww.  j  a v a 2  s  .c  o  m*/


import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;


public class Main {

    public static void main(String[] args) {
        LocalDateTime dtPrevistaBR = LocalDateTime.of(2014, 3, 26, 10, 35);
        
        ZonedDateTime localOrigemZone = ZonedDateTime.of(dtPrevistaBR, ZoneId.systemDefault());
        
        ZoneId localEntregaZoneId = ZoneId.of("America/Bogota"); 
        ZonedDateTime dtPrevista = localOrigemZone.withZoneSameInstant(localEntregaZoneId);
        
        System.out.println("Data Prevista (Brasileira): "+dtPrevistaBR);
        System.out.println("Data Prevista (Bogota): "+dtPrevista.toLocalDateTime());
    }
}

The code above generates the following result.