(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.
01.
package
com.etretatlogiciels.defaultmethods;
02.
03.
import
java.time.LocalDateTime;
04.
05.
public
interface
OldTimeClient
06.
{
07.
void
setTime(
int
hour,
int
minute,
int
second );
08.
void
setDate(
int
day,
int
month,
int
year );
09.
void
setDateAndTime(
int
day,
int
month,
int
year,
int
hour,
int
minute,
int
second );
10.
LocalDateTime getLocalDateTime();
11.
}
01.
package
com.etretatlogiciels.defaultmethods;
02.
03.
import
java.time.LocalDate;
04.
import
java.time.LocalDateTime;
05.
import
java.time.LocalTime;
06.
07.
public
class
OldSimpleTimeClient
implements
OldTimeClient
08.
{
09.
private
LocalDateTime dateAndTime;
10.
11.
public
OldSimpleTimeClient()
12.
{
13.
dateAndTime = LocalDateTime.now();
14.
}
15.
16.
public
void
setTime(
int
hour,
int
minute,
int
second )
17.
{
18.
LocalDate currentDate = LocalDate.from( dateAndTime );
19.
LocalTime timeToSet = LocalTime.of( hour, minute, second );
20.
dateAndTime = LocalDateTime.of( currentDate, timeToSet );
21.
}
22.
23.
public
void
setDate(
int
day,
int
month,
int
year )
24.
{
25.
LocalDate dateToSet = LocalDate.of( day, month, year );
26.
LocalTime currentTime = LocalTime.from( dateAndTime );
27.
dateAndTime = LocalDateTime.of( dateToSet, currentTime );
28.
}
29.
30.
public
void
setDateAndTime(
int
day,
int
month,
int
year,
int
hour,
int
minute,
int
second )
31.
{
32.
LocalDate dateToSet = LocalDate.of( day, month, year );
33.
LocalTime timeToSet = LocalTime.of( hour, minute, second );
34.
dateAndTime = LocalDateTime.of( dateToSet, timeToSet );
35.
}
36.
37.
public
LocalDateTime getLocalDateTime()
38.
{
39.
return
dateAndTime;
40.
}
41.
42.
public
String toString()
43.
{
44.
return
dateAndTime.toString();
45.
}
46.
47.
public
static
void
main( String... args )
48.
{
49.
TimeClient myTimeClient =
new
SimpleTimeClient();
50.
System.out.println(
"Testing how this code had to work prior to Java 8..."
);
51.
System.out.println(
"Current time: "
+ myTimeClient.toString() );
52.
System.out.println(
"Time in California: "
+ myTimeClient.getZonedDateTime(
"Blah blah"
).toString() );
53.
}
54.
}
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:
01.
package
com.etretatlogiciels.defaultmethods;
02.
03.
import
java.time.DateTimeException;
04.
import
java.time.LocalDateTime;
05.
import
java.time.ZoneId;
06.
import
java.time.ZonedDateTime;
07.
08.
public
interface
TimeClient
09.
{
10.
void
setTime(
int
hour,
int
minute,
int
second );
11.
void
setDate(
int
day,
int
month,
int
year );
12.
void
setDateAndTime(
int
day,
int
month,
int
year,
int
hour,
int
minute,
int
second );
13.
LocalDateTime getLocalDateTime();
14.
15.
static
ZoneId getZoneId( String zoneString )
16.
{
17.
try
18.
{
19.
return
ZoneId.of( zoneString );
20.
}
21.
catch
( DateTimeException e )
22.
{
23.
System.err.println(
"Invalid time zone: "
+ zoneString +
"; using default time zone instead."
);
24.
return
ZoneId.systemDefault();
25.
}
26.
}
27.
28.
default
ZonedDateTime getZonedDateTime( String zoneString )
29.
{
30.
return
ZonedDateTime.of( getLocalDateTime(), getZoneId( zoneString ) );
31.
}
32.
}
01.
package
com.etretatlogiciels.defaultmethods;
02.
03.
import
java.time.LocalDate;
04.
import
java.time.LocalDateTime;
05.
import
java.time.LocalTime;
06.
07.
public
class
SimpleTimeClient
implements
TimeClient
08.
{
09.
private
LocalDateTime dateAndTime;
10.
11.
public
SimpleTimeClient()
12.
{
13.
dateAndTime = LocalDateTime.now();
14.
}
15.
16.
public
void
setTime(
int
hour,
int
minute,
int
second )
17.
{
18.
LocalDate currentDate = LocalDate.from( dateAndTime );
19.
LocalTime timeToSet = LocalTime.of( hour, minute, second );
20.
dateAndTime = LocalDateTime.of( currentDate, timeToSet );
21.
}
22.
23.
public
void
setDate(
int
day,
int
month,
int
year )
24.
{
25.
LocalDate dateToSet = LocalDate.of( day, month, year );
26.
LocalTime currentTime = LocalTime.from( dateAndTime );
27.
dateAndTime = LocalDateTime.of( dateToSet, currentTime );
28.
}
29.
30.
public
void
setDateAndTime(
int
day,
int
month,
int
year,
int
hour,
int
minute,
int
second )
31.
{
32.
LocalDate dateToSet = LocalDate.of( day, month, year );
33.
LocalTime timeToSet = LocalTime.of( hour, minute, second );
34.
dateAndTime = LocalDateTime.of( dateToSet, timeToSet );
35.
}
36.
37.
public
LocalDateTime getLocalDateTime()
38.
{
39.
return
dateAndTime;
40.
}
41.
42.
public
String toString()
43.
{
44.
return
dateAndTime.toString();
45.
}
46.
47.
public
static
void
main( String... args )
48.
{
49.
TimeClient myTimeClient =
new
SimpleTimeClient();
50.
System.out.println(
"Testing use of static and default methods in Java 8..."
);
51.
System.out.println(
"Current time: "
+ myTimeClient.toString() );
52.
System.out.println(
"Time in California: "
+ myTimeClient.getZonedDateTime(
"Blah blah"
).toString() );
53.
}
54.
}
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]