(This is a pretty good walk-through of Java 8 lamba stuff so you can see what it's doing: http://java.dzone.com/articles/java-8-will-revolutionize, but that's not the topic here yet.)
This was the extent of an interface previous to Java 8.
package com.etretatlogiciels.defaultmethods;
import java.time.LocalDateTime;
public interface OldTimeClient
{
void setTime( int hour, int minute, int second );
void setDate( int day, int month, int year );
void setDateAndTime( int day, int month, int year, int hour, int minute, int second );
LocalDateTime getLocalDateTime();
}
package com.etretatlogiciels.defaultmethods;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
public class OldSimpleTimeClient implements OldTimeClient
{
private LocalDateTime dateAndTime;
public OldSimpleTimeClient()
{
dateAndTime = LocalDateTime.now();
}
public void setTime( int hour, int minute, int second )
{
LocalDate currentDate = LocalDate.from( dateAndTime );
LocalTime timeToSet = LocalTime.of( hour, minute, second );
dateAndTime = LocalDateTime.of( currentDate, timeToSet );
}
public void setDate( int day, int month, int year )
{
LocalDate dateToSet = LocalDate.of( day, month, year );
LocalTime currentTime = LocalTime.from( dateAndTime );
dateAndTime = LocalDateTime.of( dateToSet, currentTime );
}
public void setDateAndTime( int day, int month, int year, int hour, int minute, int second )
{
LocalDate dateToSet = LocalDate.of( day, month, year );
LocalTime timeToSet = LocalTime.of( hour, minute, second );
dateAndTime = LocalDateTime.of( dateToSet, timeToSet );
}
public LocalDateTime getLocalDateTime()
{
return dateAndTime;
}
public String toString()
{
return dateAndTime.toString();
}
public static void main( String... args )
{
TimeClient myTimeClient = new SimpleTimeClient();
System.out.println( "Testing how this code had to work prior to Java 8...");
System.out.println( "Current time: " + myTimeClient.toString() );
System.out.println( "Time in California: " + myTimeClient.getZonedDateTime( "Blah blah" ).toString() );
}
}
Output:
Testing how this code had to work prior to Java 8... Current time: 2014-04-03T08:22:53.277 Invalid time zone: Blah blah; using default time zone instead. Time in California: 2014-04-03T08:22:53.277-06:00[America/Denver]
With Java 8, it's possible to add default and static methods to fall back on. This example demonstrates those in Java 8 and is mostly from http://docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html.
This URL continues on to demonstrate effects of extending an interface containing a default method...
public interface AnotherTimeClient extends TimeClient
{
...
}
...by:
package com.etretatlogiciels.defaultmethods;
import java.time.DateTimeException;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
public interface TimeClient
{
void setTime( int hour, int minute, int second );
void setDate( int day, int month, int year );
void setDateAndTime( int day, int month, int year, int hour, int minute, int second );
LocalDateTime getLocalDateTime();
static ZoneId getZoneId( String zoneString )
{
try
{
return ZoneId.of( zoneString );
}
catch( DateTimeException e )
{
System.err.println( "Invalid time zone: " + zoneString + "; using default time zone instead." );
return ZoneId.systemDefault();
}
}
default ZonedDateTime getZonedDateTime( String zoneString )
{
return ZonedDateTime.of( getLocalDateTime(), getZoneId( zoneString ) );
}
}
package com.etretatlogiciels.defaultmethods;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
public class SimpleTimeClient implements TimeClient
{
private LocalDateTime dateAndTime;
public SimpleTimeClient()
{
dateAndTime = LocalDateTime.now();
}
public void setTime( int hour, int minute, int second )
{
LocalDate currentDate = LocalDate.from( dateAndTime );
LocalTime timeToSet = LocalTime.of( hour, minute, second );
dateAndTime = LocalDateTime.of( currentDate, timeToSet );
}
public void setDate( int day, int month, int year )
{
LocalDate dateToSet = LocalDate.of( day, month, year );
LocalTime currentTime = LocalTime.from( dateAndTime );
dateAndTime = LocalDateTime.of( dateToSet, currentTime );
}
public void setDateAndTime( int day, int month, int year, int hour, int minute, int second )
{
LocalDate dateToSet = LocalDate.of( day, month, year );
LocalTime timeToSet = LocalTime.of( hour, minute, second );
dateAndTime = LocalDateTime.of( dateToSet, timeToSet );
}
public LocalDateTime getLocalDateTime()
{
return dateAndTime;
}
public String toString()
{
return dateAndTime.toString();
}
public static void main( String... args )
{
TimeClient myTimeClient = new SimpleTimeClient();
System.out.println( "Testing use of static and default methods in Java 8...");
System.out.println( "Current time: " + myTimeClient.toString() );
System.out.println( "Time in California: " + myTimeClient.getZonedDateTime( "Blah blah" ).toString() );
}
}
Output:
Invalid time zone: Blah blah; using default time zone instead. Testing use of static and default methods in Java 8... Current time: 2014-04-03T08:21:56.550 Time in California: 2014-04-03T08:21:56.550-06:00[America/Denver]