Aug 31, 2017

How to write a program for calendar in Java

calendar for a given month of a given year, or of the current month and year.

import java.util.*;
import java.text.*;
/** Print a month page.
* Only works for the Western calendar.
*/
public class CalendarPage {
/** The names of the months */
String[] months = {
"January", "February", "March", "April",
"May", "June", "July", "August",
"September", "October", "November", "December"
};
/** The days in each month. */
public final static int dom[] = {
31, 28, 31, 30, /* jan feb mar apr */
31, 30, 31, 31, /* may jun jul aug */
30, 31, 30, 31 /* sep oct nov dec */
};
/** Compute which days to put where, in the Cal panel */
public void print(int mm, int yy) {
/** The number of days to leave blank at the start of this
month */
int leadGap = 0;
System.out.print(months[mm]); // print month and year

System.out.print(" ");
System.out.print(yy);
System.out.println( );
if (mm < 0 || mm > 11)
throw new IllegalArgumentException("Month " + mm + " bad,
must be 0-11");
GregorianCalendar calendar = new GregorianCalendar(yy, mm, 1);
System.out.println("Su Mo Tu We Th Fr Sa");
// Compute how much to leave before the first.
// getDay( ) returns 0 for Sunday, which is just right.
leadGap = calendar.get(Calendar.DAY_OF_WEEK)-1;
int daysInMonth = dom[mm];
if (calendar.isLeapYear(calendar.get(Calendar.YEAR)) && mm ==
1)
++daysInMonth;
// Blank out the labels before 1st day of month
for (int i = 0; i < leadGap; i++) {
System.out.print(" ");
}
// Fill in numbers for the day of month.
for (int i = 1; i <= daysInMonth; i++) {
// This "if" statement is simpler than fiddling with
NumberFormat
if (i<=9)
System.out.print(' ');
System.out.print(i);
if ((leadGap + i) % 7 == 0) // wrap if end of line.
System.out.println( );
else
System.out.print(' ');
}
System.out.println( );
}
/** For testing, a main program */
public static void main(String[] av) {
int month, year;
CalendarPage cp = new CalendarPage( );
// print the current month.
if (av.length == 2) {
cp.print(Integer.parseInt(av[0])-1,
Integer.parseInt(av[1]));
} else {
Calendar c = Calendar.getInstance( );
cp.print(c.get(Calendar.MONTH), c.get(Calendar.YEAR));
}
}
}

Creating A Simple Text Formatter Program in Java

import java.io.*;
import java.util.*;
/**
* Fmt -format text (like Berkeley Unix fmt).
*/
public class Fmt {
/** The maximum column width */
public static final int COLWIDTH=72;
/** The file that we read and format */
BufferedReader in;
/** If files present, format each, else format the standard input.
*/
public static void main(String[] av) throws IOException {
if (av.length == 0)
new Fmt(System.in).format( );
else for (int i=0; i<av.length; i++)
new Fmt(av[i]).format( );
}
/** Construct a Formatter given a filename */
public Fmt(String fname) throws IOException {
in = new BufferedReader(new FileReader(fname));
}
/** Construct a Formatter given an open Stream */
public Fmt(InputStream file) throws IOException {
in = new BufferedReader(new InputStreamReader(file));
}
/** Format the File contained in a constructed Fmt object */
public void format( ) throws IOException {
String w,f;
int col = 0;
while ((w = in.readLine( )) != null) {
if (w.length( ) == 0) { // null line
System.out.print("\n"); // end current line
if (col>0) {
System.out.print("\n"); // output blank line
col = 0;
}
continue;
}
// otherwise it's text, so format it.
StringTokenizer st = new StringTokenizer(w);
while (st.hasMoreTokens( )) {
f = st.nextToken( );
if (col + f.length( ) > COLWIDTH)

{

System.out.print("\n");
col = 0;
}
System.out.print(f + " ");
col += f.length( ) + 1;
}
}
if (col>0) System.out.print("\n");
in.close( );
}
}