Home  >  Java 8

Java 8 Time API Example: LocalTime, LocalDate and LocalDateTime

By Arvind Rai, September 05, 2014
Java 8 time API has introduced the API to represent local time by LocalTime that does not know date and time zone, LocalDate that does not know time and time zone and LocalDateTime represents the combination of LocalDate and LocalTime but does not know time zone.

LocalTime in Java 8

java.time.LocalTime is a time without time- zone that can be represented like hour-minute-second. LocalTime is immutable and represents only time. It does not represent date or time zone.
LocalTimeDemo.java
package com.cp.time;
import java.time.Clock;
import java.time.LocalTime;
import java.time.ZoneId;
public class LocalTimeDemo {
	public static void main(String[] args) {
		LocalTime localt1 = LocalTime.now();
		System.out.println(localt1);
		LocalTime localt2 = LocalTime.now(Clock.systemDefaultZone());
		System.out.println(localt2);
		System.out.println(LocalTime.now(ZoneId.of("Indian/Cocos")));
		System.out.println(LocalTime.now(ZoneId.of("America/Caracas")));
		System.out.println(LocalTime.now(ZoneId.of("Pacific/Norfolk")));
	}
} 
Output
Find the output.
18:30:59.474
18:30:59.474
19:30:59.475
08:30:59.475
00:30:59.475 



LocalDate in Java 8

java.time.LocalDate is an immutable class that represents local date like YYYY-MM-dd. This class does not store time and time zone.
LocalDateDemo.java
package com.cp.time;
import java.time.Clock;
import java.time.LocalDate;
import java.time.ZoneId;
public class LocalDateDemo {
	public static void main(String[] args) {
		LocalDate localDate1 = LocalDate.now();
		System.out.println(localDate1);
		LocalDate localDate2 = LocalDate.now(Clock.systemDefaultZone());
		System.out.println(localDate2);
		System.out.println(LocalDate.now(ZoneId.of("Indian/Cocos")));
		System.out.println(LocalDate.now(ZoneId.of("America/Caracas")));
		System.out.println(LocalDate.now(ZoneId.of("Pacific/Norfolk")));
	}
} 
Output
Find the output.
2014-09-05
2014-09-05
2014-09-05
2014-09-05
2014-09-06 

LocalDateTime in Java 8

java.time.LocalDateTime is an immutable class that represents the combination of local date and local time like 2009-11-06T10:18:30. LocalDateTime does not represent time zone.
LocalDateTimeDemo.java
package com.cp.time;
import java.time.Clock;
import java.time.LocalDateTime;
import java.time.ZoneId;
public class LocalDateTimeDemo {
	public static void main(String[] args) {
		LocalDateTime localdt1 = LocalDateTime.now();
		System.out.println(localdt1);
		LocalDateTime localdt2 = LocalDateTime.now(Clock.systemDefaultZone());
		System.out.println(localdt2);
		System.out.println(LocalDateTime.now(ZoneId.of("Indian/Cocos")));
		System.out.println(LocalDateTime.now(ZoneId.of("America/Caracas")));
		System.out.println(LocalDateTime.now(ZoneId.of("Pacific/Norfolk")));

	}
} 
Output
Find the output.
2014-09-05T18:31:09.128
2014-09-05T18:31:09.128
2014-09-05T19:31:09.128
2014-09-05T08:31:09.128
2014-09-06T00:31:09.128 
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us