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, and LocalDateTime are the core java.time types.
  • Instances are immutable — adjusters return new values.
  • Use Period / Duration for 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);
  }
}

FAQ: Java Time

Common questions about this page.

What is the StudyGrid Java tutorial?

The StudyGrid Java tutorial is a full beginner-to-advanced track: syntax, types, input, loops, methods, classes, collections, generics, maps, and lambdas. Each chapter has copy-and-run examples.

Should I run java time examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn java time in this Java Java lesson (Java Time).

Is the Java editor the same as Try Python or Try C++?

No. Try Java compiles with javac at /java/try and shows stdout plus compiler messages. Try Python stays at /try. Try C++ stays at /cpp/try. Java lessons never open those editors.

Do I need to install a JDK to learn Java?

No. Open a chapter, click Try it in Java, and compile in the browser. You can also download a .java file and compile locally with javac.

Where should I start the Java tutorial?

Start at Java Intro, then Get Started and Syntax. After the first program, continue to output, variables, and if-else. After classes, open Java Examples, then generics, map, and lambdas. Use Next at the bottom of each chapter.

Is the Java tutorial free?

Yes. The Java workshop on StudyGrid (studygrid.in) is free: dashboard, chapters, and the compile-and-run editor.