You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

《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 the Calendar class (used for date/time handling) and other utility classes.
  • static import java.lang.System.out: A shortcut that lets us write out.println() instead of the longer System.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 a Calendar object 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: Calendar uses 0-based months, so 0 = January, 1 = February, and so on.
  • day1 = c.getTimeInMillis(): Converts the calendar's date into a long value 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). x starts 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). Since DAY_IM is an integer and 29.52 is a float, Java automatically converts the result to a long (the type of day1) to store the new timestamp.
  • Update the Calendar: c.setTimeInMillis(day1) → We tell the Calendar object to refresh its date/time to match our newly calculated timestamp.
  • Print the Result: out.println(String.format("full moon on %tc", c)) → String.format uses the %tc format specifier to turn the Calendar object into a human-readable full date/time string (like Wed Feb 04 15:40:00 EST 2004).
Quick Tips for New Java Devs
  • Back when Head First Java was written, Calendar was the standard for date/time work—nowadays Java has the more intuitive java.time API (like LocalDateTime), 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 03:51:40