Loading...

Java Dates

Store one calendar day and print it so the screen shows year, month, and day in order. In Stamp.java, import java.time.LocalDate, keep the public class Stamp, store LocalDate day = LocalDate.of(2026, 3, 15);, and print day. Compile with javac Stamp.java and run java Stamp. The screen should show 2026-03-15. A second demo prints day.getYear(), day.getMonthValue(), and day.getDayOfMonth() so the screen shows 2026, then 3, then 15. Leave Random for the next page — this lesson is a fixed calendar day, not chance numbers.

Store one fixed day

The formatting page tidied indent and operator spaces on a sum. This page stores a calendar value. Open Stamp.java. Keep the public class name Stamp. Delete leftover Yard sum demos if they bury the date story, leftover ticketCount naming files, leftover Javadoc-only add files, and any keyboard Scanner. The program must not wait for typing.
Add the import at the top of the file:
import java.time.LocalDate;
Inside Stamp.main, build one day with year, month, and day-of-month. The month number starts at 1 for January. March is 3. Write:
LocalDate day = LocalDate.of(2026, 3, 15);
System.out.println(day);
LocalDate.of needs three integers in that order. The first is the year. The second is the month from 1 to 12. The third is the day of the month. This demo freezes 15 March 2026 so every run prints the same line. You are not asking the clock for “today” on this page. A fixed day keeps the quiz and the screen aligned.

Stamp print LocalDate day

java
import java.time.LocalDate;

public class Stamp {
    public static void main(String[] args) {
        LocalDate day = LocalDate.of(2026, 3, 15);
        System.out.println(day);
    }
}

From the folder that holds Stamp.java, run javac Stamp.java. When parentheses, braces, and semicolons match, javac writes no line of its own. Then run java Stamp — not java Stamp.java. Leave the .java ending off the run command. Keep one public class in the file so the name matches Stamp.
The screen shows 2026-03-15. Walk the run once. Java starts at Stamp.main. The assignment builds a LocalDate for year 2026, month 3, day 15. The print writes that day in year-month-day form with hyphens. The zeros in 03 mark March as a two-digit month on the screen. The day stays 15.
Say the beginner rule while you watch the screen: import LocalDate, call LocalDate.of with year then month then day, print the value, confirm the ISO line. Do not invent Random on this page. Random waits for the next lesson. Do not treat the month as zero-based here — 3 means March.
If you still see 12 from a leftover Yard formatting file, switch to the dates teaching file and run java Stamp again. If javac complains that LocalDate cannot be found, check the import line at the top of Stamp.java.

Read year, month, and day

Printing the whole day is useful. Reading each part is useful when you need the numbers alone. Keep the same LocalDate value. Call three getters and print each result on its own line.
System.out.println(day.getYear());
System.out.println(day.getMonthValue());
System.out.println(day.getDayOfMonth());
getYear returns the year as an int. getMonthValue returns the month from 1 to 12. getDayOfMonth returns the day inside that month. For this fixed day the three prints are 2026, then 3, then 15.

Stamp print year month day parts

java
import java.time.LocalDate;

public class Stamp {
    public static void main(String[] args) {
        LocalDate day = LocalDate.of(2026, 3, 15);
        System.out.println(day.getYear());
        System.out.println(day.getMonthValue());
        System.out.println(day.getDayOfMonth());
    }
}

Compile with javac Stamp.java and run java Stamp again. The screen shows three lines: 2026, 3, and 15. Walk the run once. The same LocalDate.of call builds the day. The getters pull each field. Nothing waits for the keyboard. Nothing rolls a random number.
Keep the two demos as two teaching shapes, not as one crowded file if that confuses you. First prove the ISO print. Then prove the three parts. Both shapes share the same import and the same of call. The second shape only adds the getters.
If the month print shows 2 when you meant March, you probably passed a zero-based month by habit. On this page March is 3. Fix the of arguments, compile again, and re-check the three lines.

A short LocalDate map

The short map below is the short map of this page. First you import LocalDate. Then you build a day with LocalDate.of. Then you either print the day or print its parts. Skip guessing that months start at zero here. Skip deleting the import before you know why LocalDate fails to compile.

📊 Beginner LocalDate habits on Stamp

Habit What you do
Import Write import java.time.LocalDate; at the top
Build Call LocalDate.of(year, month, day) with month 1–12
Read Print the day, or print getYear, getMonthValue, getDayOfMonth

Keep the table beside the teaching file while you edit. Point at the import, then at the of line, then at the print. That matching game is the whole beginner idea in one pass. Later pages can add clocks and zones. This page stays on one calendar day with clear numbers.
If leftover formatting demos still sit nearby, keep them as history. For this lesson run the dates teaching file so the screen shows a calendar line or three part lines, not a tidy sum 12. If javac complains about duplicate public classes, keep only Stamp public.

Dates are not formatting and not Random

Formatting arranged braces and spaces so readers could scan a sum. Dates store a calendar value you can print and split. Random will invent chance numbers on the next page. Do not mix those three jobs in one beginner file today.
Legacy type names from older books are outside this lesson. You do not need java.util.Date here. You do not need a keyboard prompt to type a day. You freeze one day with LocalDate.of so the screen is predictable for practice and for the quiz.
When the import is missing, javac fails before any screen line appears. When the month is wrong, the ISO line and the part lines disagree with the day you meant. Fix the arguments, keep the import, and run java Stamp until the lines match 2026-03-15 or the three parts 2026, 3, 15.

Before Random

You can import LocalDate, store LocalDate.of(2026, 3, 15), print 2026-03-15, and print the year, month, and day parts. Compile Stamp.java and run java Stamp for the kept demos. The calendar value stayed fixed; the getters matched the same day. The next page is Random, where you work with chance numbers in Java.
Keep the fixed-day demos for a clear date-plus-parts pair. Keep formatting files off this page when you practice LocalDate. Keep Random files off this page until that lesson. Those three habits are the checkpoint before you move on. When the ISO line or the three parts match the of call, dates did their job for this lesson and you are ready for the next topic about Random.
One last pass through the folder: one public Stamp, import present, LocalDate.of(2026, 3, 15), no keyboard wait, no forced Random calls. Run javac Stamp.java and java Stamp once more on the demo you keep open. If the screen still shows 2026-03-15 or the three part lines, the dates shape is solid and the course can move to Random on the next page. Prefer one clear calendar habit per read so each pass teaches the same map.

🧠 Test Your Knowledge

Ready to Start

Test Your Knowledge

Challenge yourself with this interactive quiz and see how well you understand the topic

❓
6
Questions
🎯
70%
To Pass
♾️
∞
Time
🔄
∞
Attempts

📝 Instructions

  • Read each question carefully
  • Select the best answer for each question
  • You can retake the quiz as many times as you want
  • Your progress will be shown at the top