Java Tutorial
Java Time
java.time gives you LocalDate, LocalTime, and LocalDateTime — immutable types for dates and clocks without the old Date pitfalls.
LocalDate
LocalDate is a calendar date with no time zone — year, month, day. Create withLocalDate.of or LocalDate.now(), then query or adjust.
Example
import java.time.LocalDate;
public class Main {
public static void main(String[] args) {
LocalDate today = LocalDate.now();
LocalDate birthday = LocalDate.of(2000, 5, 12);
System.out.println(today);
System.out.println(birthday.getYear());
}
}LocalTime and LocalDateTime
LocalTime is a clock time; LocalDateTime combines date and time, still without a zone. All three types are immutable — methods like plusDays return a new instance.
Example
import java.time.LocalDateTime;
import java.time.LocalTime;
public class Main {
public static void main(String[] args) {
LocalTime noon = LocalTime.of(12, 0);
LocalDateTime meeting = LocalDateTime.of(2026, 8, 21, 14, 30);
System.out.println(noon);
System.out.println(meeting.plusHours(2));
}
}Compare and calculate
isBefore / isAfter compare. Period measures calendar gaps between dates; Duration measures elapsed time between instants or times.
Example
import java.time.LocalDate;
import java.time.Period;
public class Main {
public static void main(String[] args) {
LocalDate start = LocalDate.of(2026, 1, 1);
LocalDate end = LocalDate.of(2026, 8, 21);
Period gap = Period.between(start, end);
System.out.println(gap.getMonths() + " months, " + gap.getDays() + " days");
System.out.println(end.isAfter(start));
}
}Prefer java.time over java.util.Date and Calendar in new code — the modern API is clearer and thread-safe because instances are immutable.
Format and parse
DateTimeFormatter turns dates into display strings and parses text back into dates. Use a pattern that matches your input.
Example
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class Main {
public static void main(String[] args) {
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("dd/MM/yyyy");
LocalDate d = LocalDate.parse("21/08/2026", fmt);
System.out.println(d);
System.out.println(d.format(fmt));
}
}LocalDate.now() uses the system default zone for “today.” For global apps, be explicit withZoneId and ZonedDateTime when zone matters.
Try It Yourself
Exercise: Create a LocalDate for your next birthday (pick any year), add one week with plusWeeks(1), and print both dates.
Show solution
import java.time.LocalDate;
LocalDate birthday = LocalDate.of(2027, 3, 15);
LocalDate weekLater = birthday.plusWeeks(1);
System.out.println(birthday);
System.out.println(weekLater);plusWeeks returns a new date; birthday itself stays unchanged.
Key Takeaways
LocalDate,LocalTime, andLocalDateTimeare the core java.time types.- Instances are immutable — adjusters return new values.
- Use
Period/Durationfor gaps; formatters for parse and display. - Prefer java.time over legacy
Date/Calendar.
Worked examples
The short listings above are there so you can see the grammar. The programs here use the same statements on quantities that already have units: a speed, a pH, a count of bases. They are classroom numbers. Air resistance is ignored. g is 9.81 m/s² unless a line says otherwise.
Open them in the Java editor at /java/try. Change one measurement and check whether the result still has the right unit.
Astronomy
Add observing nights
LocalDate.plusDays shifts a calendar date. Seven days later is the next weekly window, not a raw hour count.
Example
import java.time.LocalDate;
public class Main {
public static void main(String[] args) {
LocalDate start = LocalDate.of(2026, 8, 21);
System.out.println(start.plusDays(7));
}
}Physics
A lab start time
LocalTime holds hour and minute without a time zone. Useful for when the run began, not which planet you are on.
Example
import java.time.LocalTime;
public class Main {
public static void main(String[] args) {
LocalTime t = LocalTime.of(14, 30);
System.out.println(t);
}
}