Home > JDK 8

Java 8 Clock Example Using Dependency Injection Framework

June 20, 2014
Java 8 Clock represents current instant, date or time using time zone. A clock can be used in place of System.currentTimeMillis() and TimeZone.getDefault(). Clock has protected constructor. To instantiate the object of Clock we call different methods. Some of them are as below.

1. Clock.systemDefaultZone() : This method returns Clock object. This method returns current instant using best available system clock. It uses default time zone.
2. Clock.systemUTC() : This method also returns Clock object. This method returns current instant using UTC time zone.

Also there are different method in Clock that returns clock instance. To use Clock, best way is to use Dependency Injection Framework. In our example we have used Dependency Injection Framework for our demo. Now find Interface to process Clock.
IProcessClock.java
package com.cp.time.clock;
import java.time.LocalDate;
public interface IProcessClock {
	public void process(LocalDate localDate);
} 
Implementation of IProcessClock.
ProcessClock.java
package com.cp.time.clock;
import java.time.Clock;
import java.time.LocalDate;
public class ProcessClock implements IProcessClock {
	private Clock clock;
	public ProcessClock(Clock clock){
		this.clock = clock;
	}
	public void process(LocalDate localDate) {
	      if (localDate.isEqual(LocalDate.now(clock))) {
	    	  System.out.println(clock.getZone());
	      }else{
	    	  System.out.println("Does not Match");
	      }
   }
} 
To use dependency injection, find the interface.
IClockInjector.java
package com.cp.time.clock;
public interface IClockInjector {
       public IProcessClock getDemoClockInstance();
} 
We have two implantation for dependency injection. One is systemDefaultZone().
SystemDefaultZoneInjector.java
package com.cp.time.clock;
import java.time.Clock;
public class SystemDefaultZoneInjector implements IClockInjector {
	@Override
	public IProcessClock getDemoClockInstance() {
		return new ProcessClock(Clock.systemDefaultZone()); 
	}
} 
Second implementation is systemUTC.
SystemUTCInjector.java
package com.cp.time.clock;
import java.time.Clock;
public class SystemUTCInjector implements IClockInjector {
	@Override
	public IProcessClock getDemoClockInstance() {
		return new ProcessClock(Clock.systemUTC());
	}
} 
Now run the demo for Clock.
ClockDemo.java
package com.cp.time.clock;
import java.time.LocalDate;
public class ClockDemo {
   public static void main(String[] args) {
	   IClockInjector clockInjector = new SystemDefaultZoneInjector();
	   IProcessClock processClock = clockInjector.getDemoClockInstance();
	   processClock.process(LocalDate.MAX);
	   
	   clockInjector = new SystemUTCInjector();
	   processClock = clockInjector.getDemoClockInstance();
	   processClock.process(LocalDate.now());
   }
} 
The output of the program is as below.
Does not Match
Z 

comments powered by Disqus


Home    Core Java    Spring    Hibernate    Jquery    My Computer    News    Forum    Quiz    Interview Q & A     Maven     Eclipse IDE

Copyright ©2014 concretepage.com | All rights reserved |Privacy