Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

TimeZone Converter 3

import java.time.*;
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;

//BST - UK time - I we run this code in BAT SAP CPI we should get UK BST time
ZoneId defaultZoneId = ZoneId.systemDefault();
println("System Default TimeZone : " + defaultZoneId);
//Date date = new Date("2018-08-19");//YYYY-MM-DDTHH:mm:ss.SSSZ //
//DateFormat.parse(String s)
DateFormat df = new SimpleDateFormat ("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
Date d1 = df.parse("2018-08-19T22:09:25.100Z");
//Date d1 = df.parse("2018-08-19T22:09:25.123Z");
//1. Convert Date -> Instant
Instant instant = d1.toInstant();
println("Local instant : " + instant); //Zone : UTC+0

ZoneId UKZoneId = ZoneId.of("Europe/London"); //BST time zone

//2. Instant + system default time zone + toLocalDate() = LocalDate
LocalDate localDate = instant.atZone(UKZoneId).toLocalDate();
//LocalDate localDate = d1.toLocalDate();
println("UK localDate : " + localDate);

//3. Instant + system default time zone + toLocalDateTime() = LocalDateTime
LocalDateTime localDateTime = instant.atZone(UKZoneId).toLocalDateTime();
println("UK localDateTime : " + localDateTime);

//4. Instant + system default time zone = ZonedDateTime
ZonedDateTime zonedDateTime = instant.atZone(UKZoneId);
println("UK zonedDateTime : " + zonedDateTime);

//localDateTime = instant.atZone(UKZoneId).toLocalDateTime();
//--------------------
//d1 = LocalDateTime.parse(localDateTime.toString(), DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"));
//**--- **println("Gets the nano-of-second field: " + String.format("%03.0f", (int) localDateTime.getNano()/1000000 ));
//d1 = df.parse(localDateTime.toString() + localDateTime.getNano()/1000000 + 'Z');
//println String.format("%03.0f", (int) localDateTime.getNano()/1000000).equals("000")
if(String.format("%03.0f", (int) localDateTime.getNano()/1000000 ).equals("000"))
    d1 = df.parse(localDateTime.toString() + "."  + String.format("%03.0f", (int) localDateTime.getNano()/1000000 ) + 'Z');
else
    d1 = df.parse(localDateTime.toString() + 'Z');
//d1 = df.parse(localDateTime.toString() + 'Z'); //Initial I used this
instant = d1.toInstant();// Get instance for UK timezone
println("UK Instant : " + instant);



NZZoneId = ZoneId.of("NZ");
//2. Instant + system default time zone + toLocalDate() = LocalDate
localDate = instant.atZone(NZZoneId).toLocalDate();
//LocalDate localDate = d1.toLocalDate();
println("Newzeland (NZ) localDate : " + localDate);

//3. Instant + system default time zone + toLocalDateTime() = LocalDateTime
localDateTime = instant.atZone(NZZoneId).toLocalDateTime();
println("NZ localDateTime : " + localDateTime);

//4. Instant + system default time zone = ZonedDateTime
zonedDateTime = instant.atZone(NZZoneId);
println("NZ zonedDateTime : " + zonedDateTime);
//-----------------
//println String.format("%03.0f", (int) localDateTime.getNano()/1000000).equals("000")
if(String.format("%03.0f", (int) localDateTime.getNano()/1000000 ).equals("000"))
    d1 = df.parse(localDateTime.toString() + "."  + String.format("%03.0f", (int) localDateTime.getNano()/1000000 ) + 'Z');
else
    d1 = df.parse(localDateTime.toString() + 'Z');    
//d1 = df.parse(localDateTime.toString() + 'Z');
instant = d1.toInstant();// Get instance for NZ timezone
println("NZ Instant : " + instant);

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.