Page 364 - Introduction to Programming with Java: A Problem Solving Approach
P. 364
330 Chapter 8 Software Engineering
/********************************************************************
*
CalendarDemo.java
Dean & Dean
This program demonstrates how to use the Calendar class.
********************************************************************/
*
*
*
import java.util.*;
// for Scanner and Calendar
public class CalendarDemo
{
public static void main(String[] args)
{
Scanner stdIn = new Scanner(System.in);
Calendar time = Calendar.getInstance();
int day;
int hour;
System.out.println(time.getTime());
day = time.get(time.DAY_OF_YEAR); ⎫⎬
hour = time.get(time.HOUR_OF_DAY);⎭ System.out.println("day of year= " + day); System.out.println("hour of day= " + hour);
// initially now
// day of year
// hour of day
}
// end class CalendarDemo
}
Apago PDF Enhancer
System.out.print("Enter number of days to add: ");
day += stdIn.nextInt();
System.out.print("Enter number of hours to add: ");
hour += stdIn.nextInt();
time.set(time.DAY_OF_YEAR, day);
time.set(time.HOUR_OF_DAY, hour);
System.out.println(time.getTime());
// end main
Sample session:
Mon Sep 24 16:42:27 CDT 2007
day of year= 267
hour of day= 16
Enter number of days to add: 8
Enter number of hours to add: 13
Wed Oct 03 05:42:27 CDT 2007
Parameters are int codes that specify the kind of information desired.
Figure 8.12 Demonstration program for the Calendar class.