Use JavaBean to create your online calendar book

Source: Internet
Author: User
Tags add date format config empty final string stringbuffer
Create | Calendars Some friends once said that if there is an online diary, or an online calendar can remind themselves to do things. Actually, you can do one of these calendars yourself. I don't believe you. See the following example:

〈html〉
〈head〉
〈title〉 million Calendar Notepad 〈/title〉
〈/head〉
〈body bgcolor = "white"
Set page scripting language is Java, import Htmlcalendarnotepad class, Htmlcalendarnotepad in the following will be mentioned
〈%@ page language= "java" import= "Htmlcalendarnotepad"%〉
Define a JavaBean whose ID is htmlcal
〈jsp:usebean id= "Htmlcal" scope= "session" class= "Htmlcalendarnotepad"/〉
〈%
Set the parameters, take the required month for March, because the year is not set, so the default for this year.
Htmlcal.setmonth (3);
Set action, March 24, go to Tenkine, open in a new window (or you can open it in a different way)
Htmlcal.setaction ("http://www.yesky.com/", "_blank");
%〉
〈table width=300〉
〈TR〉〈TD nowrap〉
〈%=htmlcal.gethtml ()%〉//prints a one month calendar in tabular form
〈/td〉〈/tr〉
〈/table〉
〈/body〉
〈/html〉

You will see the following output results

Mon Tue Wed Thu Fri Sat Sun
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31

Isn't that the same as the calendar, but it's better than the calendar, with a preset link to remind you to go to a site, or to execute a JavaScript function, so you don't have a notepad anymore? Of course, you can also add some features to make it more powerful, such as adding a form, allowing users to fill in the diary sent to your system's database and so on. To accomplish this, let's take a look at how the JavaBean program is written.
JavaBean Program Analysis

I would like to introduce some of the main methods of Htmlcalendarnotepad, I believe that we will help

public void setyear (int years)//set year. The default value is the current year
public int getyear ()//Get year. The default value is the current year
This is standard Java program writing, because Java program is a class so often write this setxxx/getxxx
public void setmonth (int month)//Set month (1-12), default value is current month
public int getmonth ()//Get Month (1-12), default value is current month
public void SetStyle (int style)
public int GetStyle ()
Set/Get the style of the calendar (style)
Is the first day of the week in Sunday Htmlcalendarnotepad.sunday_first or the first day of the week in Monday Htmlcalendarnotepad.monday_first

The default value is Htmlcalendarnotepad.sunday_first
public void SetLocale (Locale loc)//Set region code (LOCALE), default value is LOCALE.PRC
Public Locale GetLocale ()//Get Region code (LOCALE), default value is LOCALE.PRC

public void setaction (int day, string actionurl, String target_frame)
Set up hyperlinks to process requests. If any errors are made, the method returns an empty string.
You can set an ordinary URL, such as "http://www.yesky.com/action.jsp"
or a JavaScript function name, such as "MyFunction"
The date will be passed as a parameter to the URL, or passed as a string value to JavaScript. The format is YYYYMMDD.
Target_frame can be an empty string.
Such as:
If you call Setaction ("http://www.yesky.com/", "_blank")
Hyperlink is: 〈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 the one month

Now let me share with you the tempting bean (bean): (Htmlcalendarnotepad.java).
Import Java.text.DateFormat;
Import Java.text.SimpleDateFormat;
Import java.util.*;

public class Htmlcalendarnotepad
{
public static final int monday_first = 1; Set each week to be Monday for the first day
public static final int sunday_first = 2; Set each week to be Sunday for the first day
Private Hashtable config;
private static final String mnth[] = {

"January", "February", "March", "April", "May", "June",
"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 ()//constructors

{

Sfont = null;

GregorianCalendar Gcalendar = new GregorianCalendar (); Get the current calendar (Grigory)

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

NewLine = System.getproperty ("Line.separator");

style = 2; Set each week to be Sunday for the first day
month = Gcalendar.get (2); Get month

Year = Gcalendar.get (1); The year taken

loc = LOCALE.PRC; Set country name, default to 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)//Get the location of a 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 for the week, such as 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〉 to the StringBuffer to prepare for the creation of the table

if (style = = 2)//See explanation

{

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 ()//Get region name

{

Return LOC;

}

public int getyear ()//year of acquisition

{

return htmlcalendaryear;

}

public int getmonth ()//Get monthly Score

{

return htmlcalendarmonth;

}



public int GetStyle ()//Get the style of the calendar

{

return htmlcalendarstyle;

}

The Uri,target_frame value of the action can be set to _blank, _parent, _top, _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 up hyperlinks for all days of one months

public void Setactions (string Actionuri, String target_frame)

{

for (int day = 1; day〈= day++)

Setaction (Day, Actionuri, target_frame);



}

Set up a region

public void SetLocale (Locale Locale)

{

loc = locale;

}

Set year

public void setyear (int htmlcalendaryear)

{

if (htmlcalendaryear〉0)

{

Year = Htmlcalendaryear;

Config.clear ();

}

}

Set month

public void setmonth (int htmlcalendarmonth)

{

if (htmlcalendarmonth〉= 1 && htmlcalendarmonth〈= 12)

{

month = Htmlcalendarmonth;

Config.clear ();

}

}

Set the style of a calendar

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.get (5));

}

Private String twodigits (int day) + 0 before 1-9 for the number in the calendar to align

{

String stringday = string.valueof (day); Get the value of day

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

Return "0" + stringday; Then add 0 before the string

Else

return stringday;

}

}


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.