Use JavaBean to create your online calendar (2)

Source: Internet
Author: User

JavaBean Program Analysis

I want to introduce some of the main methods in HtmlCalendarNotePad. I believe they will be helpful to you.

Public void setYear (int year) // sets the year. The default value is the current year.
Public int getYear () // obtain the year. The default value is the current year.
// This is written in a standard Java program. Because a Java program is a class, this setXXX/getXXX is often written.
Public void setMonth (int month) // sets the month (1-12). The default value is the current month.
Public int getMonth () // obtain the month (1-12). The default value is the current month.
Public void setStyle (int style)
Public int getStyle ()
// Set/get the style of the calendar)
// It is the first day of a week on Sunday (HtmlCalendarNotePad. SUNDAY_FIRST) or the first day of a week on Monday (HtmlCalendarNotePad. MONDAY_FIRST)

// The default value is HtmlCalendarNotePad. SUNDAY_FIRST.
Public void setLocale (Locale loc) // sets the region code (Locale). The default value is Locale. PRC.
Public Locale getLocale () // obtain the region code (Locale). The default value is Locale. PRC.

Public void setAction (int day, String actionUrl, String target_frame)
// Set a hyperlink to process the request. If any error occurs, this method returns an empty string.
// You can set a common URL, such as "http://www.yesky.com/action.jsp"
// Or a javascript function name, such as "myFunction"
// The date will be passed to the URL as a parameter or a string value to javascript. The format is yyyymmdd.
// Target_frame can be a null string.
For example:
If setAction (21, "http://www.yesky.com/", "_ blank") is called ")
Hyperlink: <a href = "http://www.yesky.com /? Date = 20000621 "target = _ blank> 21 </a> 〉

Public void setActions (String actionUrl, String target_frame)
// You can also set a hyperlink for each day of a month

Now let me share with you the attractive Bean: (HtmlCalendarNotePad. java ).
Import java. text. DateFormat;
Import java. text. SimpleDateFormat;
Import java. util .*;

Public class HtmlCalendarNotePad
{
Public static final int MONDAY_FIRST = 1; // you can specify Monday as the first day of each week.
Public static final int SUNDAY_FIRST = 2; // you can specify Sunday as the first day of each week.
Private Hashtable config;
Private static final String Mnth [] = {

"August ",
"July", "August", "September", "October", "November", "December"
};

Private int year;
Private int month;
Private int style;
Private String sFont;
Private Locale loc;
Private static String NEWLINE = "\ n ";



Public HtmlCalendarNotePad () // Constructor

{

SFont = null;

GregorianCalendar gCalendar = new GregorianCalendar (); // obtain the current calendar (GregorianCalendar)

Config = new Hashtable (); // create a new hash table to store configuration information

NEWLINE = System. getProperty ("line. separator ");

Style = 2; // set every week to Sunday as the first day
Month = gCalendar. get (2); // get the month

Year = gCalendar. get (1); // year

Loc = Locale. PRC; // set the country name. The default value is the People's Republic of China.

}



Private String formatObject (String s, Object obj)

{

String s1 = "";

If (obj! = Null)

S1 = String. valueOf (String. valueOf (obj ));

If (s = null)

Return s1;

Else

Return s + s1 + "</font> 〉";

}



Private int getDay (Calendar calendar) // gets the position of a specified day in the Calendar.

{

If (style = 2)

Return calendar. get (7)-1;

Else

Return (calendar. get (7) + 5) % 7;

}



Public String getHtml ()

{

GregorianCalendar gCalendar = new GregorianCalendar (year, month-1, 1 );

GregorianCalendar gCalendar1 = new GregorianCalendar (2001, 3, 24 );

SimpleDateFormat simpledateformat = new SimpleDateFormat ("EEE", loc );

// Set the abbreviation format. EEE is the abbreviation of a week, for example, Sun. If EEEE is Sunday.

Int I = month-1;

Int j = 0;

StringBuffer stringbuffer = new StringBuffer (""); // create a new string buffer.

Stringbuffer. append ("<table> <tr> \ n"); // Add <table> <tr> On stringbuffer to prepare for table creation.

If (style = 2) // see the preceding description.

{

Stringbuffer. append ("<th align = right>" + formatObject (sFont, simpledateformat. format (gCalendar1.getTime () + "</th> \ n ");

GCalendar1.add (5, 1 );

For (int k = 1; k <7; k ++)

{

Stringbuffer. append ("<th align = right>" + formatObject (sFont, simpledateformat. format (gCalendar1.getTime () + "</th> \ n ");

GCalendar1.add (5, 1 );

}



}

Else

{

GCalendar1.add (5, 1 );

Stringbuffer. append ("<th align = right>" + formatObject (sFont, simpledateformat. format (gCalendar1.getTime () + "</th> \ n ");

For (int l = 2; l <8; l ++)

{

GCalendar1.add (5, 1 );

Stringbuffer. append ("<th align = right>" + formatObject (sFont, simpledateformat. format (gCalendar1.getTime () + "</th> \ n ");

}



}

Stringbuffer. append ("</tr> \ n ");

Int i1 = 0;

J = 0;

If (getDay (gCalendar)> 0)

{

Stringbuffer. append ("<tr> 〉");

For (; i1 <getDay (gCalendar); i1 ++)

{

Stringbuffer. append ("<td align = right> 〉");

If (sFont! = Null)

Stringbuffer. append (sFont + "</font> 〉");

Else

Stringbuffer. append ("");

Stringbuffer. append ("</td> \ n ");

J ++;

}



}

For (; gCalendar. get (2) = I; gCalendar. add (5, 1 ))

{

Int j1 = gCalendar. get (5 );

Int k1 = (i1 + j1) % 7;

If (k1 = 1)

{

Stringbuffer. append ("<tr>" + NEWLINE );

J = 0;

}

Stringbuffer. append ("<td align = right> 〉");

J ++;

If (sFont! = Null)

Stringbuffer. append (sFont );

String s;

If (s = (String) config. get (String. valueOf (j1 )))! = Null)

{

Stringbuffer. append ("<a href = \"");

If (s. toUpperCase (). startsWith ("HTT") | s. indexOf (".")> 0)

{

Stringbuffer. append (s );

If (s. indexOf ("? ") <0)

Stringbuffer. append ("? Date = "+ stringDate (gCalendar ));

Else

Stringbuffer. append ("& date =" + stringDate (gCalendar ));

}

Else

{

Stringbuffer. append ("javascript:" + s + "('" + stringDate (gCalendar) + "');");

}

Stringbuffer. append ("\"");

If (s = (String) config. get (j1 + "target "))! = Null)

Stringbuffer. append ("target = \" "+ s + "\"");

Stringbuffer. append ("> ("〉");

Stringbuffer. append (gCalendar. get (5 ));

Stringbuffer. append ("</a> \ n ");

}

Else

{

Stringbuffer. append (String. valueOf (j1 ));

}

If (sFont! = Null)

Stringbuffer. append ("</font> 〉");

Stringbuffer. append ("</td> \ n ");

If (k1 = 0)

Stringbuffer. append ("</tr> \ n ");

}



If (j <7)

{

For (; j <7; j ++)

{

Stringbuffer. append ("<td align = right> 〉");

If (sFont! = Null)

Stringbuffer. append (sFont );

Stringbuffer. append ("");

If (sFont! = Null)

Stringbuffer. append ("</font> 〉");

Stringbuffer. append ("</td> \ n ");

}



Stringbuffer. append ("</tr> \ n ");

}

Stringbuffer. append ("</table> \ n ");

Return stringbuffer. toString ();

}



Public Locale getLocale () // obtain the region name

{

Return loc;

}

Public int getYear () // obtain the year

{

Return htmlCalendarYear;

}

Public int getMonth () // obtain the monthly score

{

Return htmlCalendarMonth;

}



Public int getStyle () // get the calendar Style

{

Return htmlCalendarStyle;

}

// Set the URI of the action. The target_frame value can be _ blank, _ parent, _ top, and _ self.

Public void setAction (int day, String actionUri, String target_frame)

{

If (actionUri! = Null)

{

Config. put (String. valueOf (day), actionUri );

If (target_frame! = Null & target_frame.length ()> 0)

Config. put (day + "target", target_frame );

}

}

// Set hyperlinks for all days of a month

Public void setActions (String actionUri, String target_frame)

{

For (int day = 1; day <=31; day ++)

SetAction (day, actionUri, target_frame );



}

// Set the region

Public void setLocale (Locale locale)

{

Loc = locale;

}

// Set the year

Public void setYear (int htmlCalendarYear)

{

If (htmlCalendarYear> 0)

{

Year = htmlCalendarYear;

Config. clear ();

}

}

// Set the month

Public void setMonth (int htmlCalendarMonth)

{

If (htmlCalendarMonth >=1 & htmlCalendarMonth <= 12)

{

Month = htmlCalendarMonth;

Config. clear ();

}

}

// Set the calendar Style

Public void setStyle (int htmlCalendarStyle)

{

Style = htmlCalendarStyle;

}

Private String stringDate (Calendar calendar)

{

String strDay = String. valueOf (calendar. get (1 ));

Return strDay + twoDigits (calendar. get (2) + 1) + twoDigits (calendar ar. get (5 ));

}

Private String twoDigits (int day) // Add 0 before 1-9 to align the numbers in the calendar

{

String stringDay = String. valueOf (day); // gets the value of day.

If (stringDay. length () = 1) // if the string length is 1

Return "0" + stringDay; // Add zero before the string

Else

Return stringDay;

}

}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.