BAEL-1683-Guide to Java Clock class

This commit is contained in:
Senthil Kumar Subramanian 2018-04-12 07:42:42 +05:30
parent b2dfcd9469
commit b9d21e3210
2 changed files with 159 additions and 83 deletions

View File

@ -1,83 +0,0 @@
package com.baeldung.clock;
import java.time.Clock;
import java.time.Duration;
import java.time.Instant;
import java.time.ZoneId;
public class ClockExample {
public static void main(String[] args) {
// gets systemUTC
Clock clockUTC = Clock.systemUTC();
System.out.println("UTC time :: " + clockUTC.instant());
// using system()
Clock clockSystem = Clock.system(ZoneId.of("Asia/Kolkata"));
System.out.println(clockSystem.instant());
// gets systemDefaultZone
Clock clockSystemDefault = Clock.systemDefaultZone();
System.out.println(clockSystemDefault);
clockSystemDefault = Clock.system(ZoneId.systemDefault());
System.out.println(clockSystemDefault);
// gets instant from system default zone
Clock clockInstant = Clock.systemDefaultZone();
Instant instant = clockInstant.instant();
System.out.println(instant);
// gets millis
Clock millis = Clock.systemDefaultZone();
System.out.println(millis.millis());
System.out.println(System.currentTimeMillis());
// using offset
Clock baseClock = Clock.systemDefaultZone();
// result clock will be later than baseClock
Clock clock5 = Clock.offset(baseClock, Duration.ofHours(72));
System.out.println(clock5.instant());
// result clock will be same as baseClock
clock5 = Clock.offset(baseClock, Duration.ZERO);
System.out.println(clock5.instant());
// result clock will be earlier than baseClock
clock5 = Clock.offset(baseClock, Duration.ofHours(-72));
System.out.println(clock5.instant());
// using tick
Clock clockDefaultZone = Clock.systemDefaultZone();
Clock clocktick = Clock.tick(clockDefaultZone, Duration.ofSeconds(30));
System.out.println("Clock Default Zone : " + clockDefaultZone.instant());
System.out.println("Clock tick : " + clocktick.instant());
// using tickMinutes
Clock tickMinutes = Clock.tickMinutes(ZoneId.of("Asia/Kolkata"));
System.out.println(tickMinutes.instant());
tickMinutes = Clock.tick(Clock.system(ZoneId.of("Asia/Kolkata")), Duration.ofMinutes(1));
System.out.println(tickMinutes.instant());
// using tickSeconds
ZoneId zoneId = ZoneId.of("Asia/Calcutta");
Clock tickSeconds = Clock.tickSeconds(zoneId);
System.out.println(tickSeconds.instant());
tickSeconds = Clock.tick(Clock.system(ZoneId.of("Asia/Kolkata")), Duration.ofSeconds(1));
System.out.println(tickSeconds.instant());
// using withZone
ZoneId zone1 = ZoneId.of("Asia/Singapore");
Clock clock11 = Clock.system(zone1);
System.out.println(clock11.instant());
ZoneId zone2 = ZoneId.of("Asia/Kolkata");
Clock clock21 = clock11.withZone(zone2);
System.out.println(clock21.instant());
ZoneId zone = clockDefaultZone.getZone();
System.out.println(zone.getId());
}
}

View File

@ -0,0 +1,159 @@
package com.baeldung.java.clock;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.time.Clock;
import java.time.Duration;
import java.time.ZoneId;
import java.time.ZoneOffset;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ClockUnitTest {
private static final Logger LOGGER = LoggerFactory.getLogger(ClockUnitTest.class);
@Test
public void givenClock_withSytemUTC_retrievesInstant() {
Clock clockUTC = Clock.systemUTC();
assertEquals(clockUTC.getZone(), ZoneOffset.UTC);
assertEquals(clockUTC.instant().equals(null), false);
LOGGER.debug("UTC instant :: " + clockUTC.instant());
}
@Test
public void givenClock_withSytem_retrievesInstant() {
Clock clockSystem = Clock.system(ZoneId.of("Asia/Calcutta"));
assertEquals(clockSystem.getZone(), ZoneId.of("Asia/Calcutta"));
assertEquals(clockSystem.instant().equals(null), false);
LOGGER.debug("System zone :: " + clockSystem.getZone());
}
@Test
public void givenClock_withSytemDefaultZone_retrievesInstant() {
Clock clockSystemDefault = Clock.systemDefaultZone();
assertEquals(clockSystemDefault.getZone().equals(null), false);
assertEquals(clockSystemDefault.instant().equals(null), false);
LOGGER.debug("System Default instant :: " + clockSystemDefault.instant());
}
@Test
public void givenClock_withSytemUTC_retrievesTimeInMillis() {
Clock clockMillis = Clock.systemDefaultZone();
assertEquals(clockMillis.instant().equals(null), false);
assertTrue(clockMillis.millis() > 0);
LOGGER.debug("System Default millis :: " + clockMillis.millis());
}
@Test
public void givenClock_usingOffset_retrievesFutureDate() {
Clock baseClock = Clock.systemDefaultZone();
// result clock will be later than baseClock
Clock futureClock = Clock.offset(baseClock, Duration.ofHours(72));
assertEquals(futureClock.instant().equals(null), false);
assertTrue(futureClock.millis() > baseClock.millis());
LOGGER.debug("Future Clock instant :: " + futureClock.instant());
}
@Test
public void givenClock_usingOffset_retrievesPastDate() {
Clock baseClock = Clock.systemDefaultZone();
// result clock will be later than baseClock
Clock pastClock = Clock.offset(baseClock, Duration.ofHours(-72));
assertEquals(pastClock.instant().equals(null), false);
assertTrue(pastClock.millis() < baseClock.millis());
LOGGER.debug("Past Clock instant :: " + pastClock.instant());
}
@Test
public void givenClock_usingTick_retrievesInstant() {
Clock clockDefaultZone = Clock.systemDefaultZone();
Clock clocktick = Clock.tick(clockDefaultZone, Duration.ofSeconds(300));
assertEquals(clockDefaultZone.instant().equals(null), false);
assertEquals(clocktick.instant().equals(null), false);
assertTrue(clockDefaultZone.millis() > clocktick.millis());
LOGGER.debug("Clock Default Zone instant : " + clockDefaultZone.instant());
LOGGER.debug("Clock tick instant: " + clocktick.instant());
}
@Test(expected=IllegalArgumentException.class)
public void givenClock_usingTickDurationNegative_throwsException() {
Clock clockDefaultZone = Clock.systemDefaultZone();
Clock.tick(clockDefaultZone, Duration.ofSeconds(-300));
}
@Test
public void givenClock_usingTickSeconds_retrievesInstant() {
ZoneId zoneId = ZoneId.of("Asia/Calcutta");
Clock tickSeconds = Clock.tickSeconds(zoneId);
assertEquals(tickSeconds.instant().equals(null), false);
LOGGER.debug("Clock tick seconds instant :: " + tickSeconds.instant());
tickSeconds = Clock.tick(Clock.system(ZoneId.of("Asia/Calcutta")), Duration.ofSeconds(100));
assertEquals(tickSeconds.instant().equals(null), false);
}
@Test
public void givenClock_usingTickMinutes_retrievesInstant() {
Clock tickMinutes = Clock.tickMinutes(ZoneId.of("Asia/Calcutta"));
assertEquals(tickMinutes.instant().equals(null), false);
LOGGER.debug("Clock tick seconds instant :: " + tickMinutes.instant());
tickMinutes = Clock.tick(Clock.system(ZoneId.of("Asia/Calcutta")), Duration.ofMinutes(5));
assertEquals(tickMinutes.instant().equals(null), false);
}
@Test
public void givenClock_usingWithZone_retrievesInstant() {
ZoneId zoneSingapore = ZoneId.of("Asia/Singapore");
Clock clockSingapore = Clock.system(zoneSingapore);
assertEquals(clockSingapore.instant().equals(null), false);
LOGGER.debug("clockSingapore instant : " + clockSingapore.instant());
ZoneId zoneCalcutta = ZoneId.of("Asia/Calcutta");
Clock clockCalcutta = clockSingapore.withZone(zoneCalcutta);
assertEquals(clockCalcutta.instant().equals(null), false);
LOGGER.debug("clockCalcutta instant : " + clockSingapore.instant());
}
@Test
public void givenClock_usingGetZone_retrievesZoneId() {
Clock clockDefaultZone = Clock.systemDefaultZone();
ZoneId zone = clockDefaultZone.getZone();
assertEquals(zone.getId().equals(null), false);
LOGGER.debug("Default zone instant : " + clockDefaultZone.instant());
}
}