← Back to Core Java Course | Chapter 13: Advanced Topics & Reference | Lesson 2 of 10

Java Date & Time

Java's date and time tools let a program work with dates, clock times, and gaps between them, like a calendar and clock combined. You can add days, format them, or find how long between two dates.
Syntax
java
import java.time.*;

LocalDate date = LocalDate.now();
LocalDateTime dateTime = LocalDateTime.of(year, month, day, hour, minute);

LocalDate and LocalTime

LocalDate represents a calendar date with no time component, and LocalTime represents a time of day with no date — use whichever matches what you're actually modeling.

Example: LocalDate and LocalTime

java
// Import java.time.LocalDate so it can be used by its short name
import java.time.LocalDate;
// Import java.time.LocalTime so it can be used by its short name
import java.time.LocalTime;
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		LocalDate date = LocalDate.of(2024, 1, 15);
		LocalTime time = LocalTime.of(14, 30);
		// Print a line to the console
		System.out.println(date);
		// Print a line to the console
		System.out.println(time);
	}
}

LocalDateTime

LocalDateTime combines both into a single date-and-time value, useful for representing a specific moment without needing timezone information attached.

Example: LocalDateTime

java
// Import java.time.LocalDateTime so it can be used by its short name
import java.time.LocalDateTime;
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		LocalDateTime dt = LocalDateTime.of(2024, 1, 15, 14, 30);
		// Print a line to the console
		System.out.println(dt);
	}
}

Formatting Dates

DateTimeFormatter controls how a date or time is rendered to a string (and parsed back from one), letting you match locale-specific or application-specific formatting requirements.

Example: Formatting Dates

java
// Import java.time.LocalDate so it can be used by its short name
import java.time.LocalDate;
// Import java.time.format.DateTimeFormatter so it can be used by its short name
import java.time.format.DateTimeFormatter;
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		LocalDate date = LocalDate.of(2024, 1, 15);
		DateTimeFormatter fmt = DateTimeFormatter.ofPattern("dd/MM/yyyy");
		// Print a line to the console
		System.out.println(date.format(fmt));
	}
}

Modifying Dates

Because these classes are immutable, methods like plusDays() or minusMonths() return a *new* date/time object rather than modifying the original — you must capture the return value to see the change.

Example: Modifying Dates

java
import java.time.LocalDate;
public class Main {
	public static void main(String[] args) {
		LocalDate date = LocalDate.of(2024, 1, 15);
		LocalDate later = date.plusDays(10); // new object, original unchanged
		System.out.println(date);
		System.out.println(later);
	}
}

Duration and Period

Duration measures a span of time in seconds/nanoseconds (for time-based values), while Period measures a span in years/months/days (for date-based values) — pick based on what unit actually makes sense for your calculation.

Example: Duration and Period

java
// Import java.time.Duration so it can be used by its short name
import java.time.Duration;
// Import java.time.Period so it can be used by its short name
import java.time.Period;
import java.time.LocalDate;
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		Duration duration = Duration.ofMinutes(90);
		Period period = Period.between(LocalDate.of(2024, 1, 1), LocalDate.of(2024, 3, 1));
		// Print a line to the console
		System.out.println(duration);
		// Print a line to the console
		System.out.println(period);
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Using the old Date class when LocalDate is recommended.
  2. Creating an invalid date such as LocalDate.of(2023, 2, 30), which throws DateTimeException.
  3. Forgetting that date objects are immutable, so plusDays() returns a new object.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.