《Head First Java》P314月相代码循环部分解析:Java新手求讲解
Hey there! Let's break down this full moon calculation code from Head First Java page 314 step by step—especially that loop you're curious about. I'll keep things straightforward since you're new to Java 😊
What This Code Does Overall
This program calculates and prints 60 consecutive full moon dates, starting from January 7, 2004, 3:40 PM. It uses the average lunar cycle (29.52 days) to jump from one full moon to the next.
Line-by-Line Breakdown
1. Imports & Constant Setup
import java.util.*; import static java.lang.System.out; class FullMoons { static int DAY_IM = 1000 * 60 * 60 * 24;
import java.util.*: Pulls in theCalendarclass (used for date/time handling) and other utility classes.static import java.lang.System.out: A shortcut that lets us writeout.println()instead of the longerSystem.out.println().DAY_IM: This constant stores the number of milliseconds in one day. The math checks out: 1000ms/sec × 60sec/min × 60min/hr ×24hr/day = 86,400,000 ms per day.
2. Initializing the Starting Date
public static void main(String [] args) { Calendar c = Calendar.getInstance(); c.set(2004,0,7,15,40); long day1 = c.getTimeInMillis();
Calendar.getInstance(): Creates aCalendarobject set to your computer's current time—we'll overwrite this immediately with our target start date.c.set(2004,0,7,15,40): Sets the calendar to January 7, 2004, 3:40 PM. Critical note:Calendaruses 0-based months, so0= January,1= February, and so on.day1 = c.getTimeInMillis(): Converts the calendar's date into alongvalue representing milliseconds since January 1, 1970 (the "epoch" time). Turning dates into numbers makes math with dates way easier!
3. The Loop (The Core of the Program)
for (int x = 0; x < 60; x++) { day1 += (DAY_IM * 29.52); c.setTimeInMillis(day1); out.println(String.format("full moon on %tc", c)); }
Let's unpack this loop line by line:
- Loop Rule:
for (int x = 0; x < 60; x++)→ We run this block 60 times total (once for each full moon we want to calculate).xstarts at 0 and increments by 1 each time until it hits 60, then the loop stops. - Calculate Next Full Moon:
day1 += (DAY_IM * 29.52)→ We take the current full moon's timestamp (day1) and add the number of milliseconds in 29.52 days. 29.52 is the average number of days between full moons (the lunar synodic period). SinceDAY_IMis an integer and 29.52 is a float, Java automatically converts the result to along(the type ofday1) to store the new timestamp. - Update the Calendar:
c.setTimeInMillis(day1)→ We tell theCalendarobject to refresh its date/time to match our newly calculated timestamp. - Print the Result:
out.println(String.format("full moon on %tc", c))→String.formatuses the%tcformat specifier to turn theCalendarobject into a human-readable full date/time string (likeWed Feb 04 15:40:00 EST 2004).
Quick Tips for New Java Devs
- Back when Head First Java was written,
Calendarwas the standard for date/time work—nowadays Java has the more intuitivejava.timeAPI (likeLocalDateTime), but this code is still a great example of basic date math. - Using milliseconds for date calculations is common because it turns date/time into a simple number we can add or subtract without fuss.
- The tiny floating-point error from using 29.52 won't affect this example much—it's close enough for showing approximate full moon dates!
内容的提问来源于stack exchange,提问作者rachit3dev
相关产品推荐
相关产品推荐

