Removed ZoneId code
This commit is contained in:
parent
3b17051337
commit
7064a7e309
@ -1,5 +1,7 @@
|
|||||||
package com.baeldung.datetime;
|
package com.baeldung.datetime;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
import java.sql.Timestamp;
|
import java.sql.Timestamp;
|
||||||
import java.text.DateFormat;
|
import java.text.DateFormat;
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
@ -16,22 +18,15 @@ public class ConvertInstantToTimestamp {
|
|||||||
public static void run() {
|
public static void run() {
|
||||||
Instant instant = Instant.now();
|
Instant instant = Instant.now();
|
||||||
Timestamp timestamp = Timestamp.from(instant); // same point on the time-line as Instant
|
Timestamp timestamp = Timestamp.from(instant); // same point on the time-line as Instant
|
||||||
|
assertEquals(instant.toEpochMilli(), timestamp.getTime());
|
||||||
|
|
||||||
instant = timestamp.toInstant();
|
instant = timestamp.toInstant();
|
||||||
|
assertEquals(instant.toEpochMilli(), timestamp.getTime());
|
||||||
System.out.println("Instant (milliseconds from epoch): " + instant.toEpochMilli());
|
|
||||||
System.out.println("Timestamp (milliseconds from epoch): " + timestamp.getTime());
|
|
||||||
System.out.print("\n");
|
|
||||||
|
|
||||||
// Local TimeZone of the machine at the time of running this code is GMT +5:30 a.k.a IST
|
|
||||||
ZoneId zoneId = ZoneId.of(ZoneId.SHORT_IDS.get("IST"));
|
|
||||||
|
|
||||||
DateFormat df = DateFormat.getDateTimeInstance();
|
DateFormat df = DateFormat.getDateTimeInstance();
|
||||||
df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss:SS'Z'");
|
df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SS'Z'");
|
||||||
df.setTimeZone(TimeZone.getTimeZone("UTC"));
|
df.setTimeZone(TimeZone.getTimeZone("UTC"));
|
||||||
|
|
||||||
System.out.println("Instant (in UTC): " + instant);
|
assertEquals(instant.toString(), df.format(timestamp).toString());
|
||||||
System.out.println("Timestamp (in UTC): " + df.format(timestamp));
|
|
||||||
System.out.println("Instant (in GMT +05:30): " + instant.atZone(zoneId));
|
|
||||||
System.out.println("Timestamp (in GMT +05:30): " + timestamp);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user