A simple programming idea in PHP and Java implementation comparison: Date Class!

Source: Internet
Author: User
Tags date format comparison integer learn php php and return tostring
Compare | programming before using PHP to write a simple class, the main function is to solve, a large number of pages need to display a Drop-down list box to select the year/month/day/week. I hope you can learn PHP and Java to help.

The implementation of PHP is as follows:
getCurrentDate.class.php
<?php
/*
* Function: Generate Drop-down list (year/month/day/weekly basis current value)
* Programmer: Xiangli
* Date: 2003-01-19
*/

#---------------------------------------------------#
# Modified: 2003-03-18 #
# modification Reason: Added week's Build #
#-------------------------------------------------#

Class getcurrentdate{
var $Years = 2002;
var $Months = 12;
var $Days = 31;
var $Weeks = 52;

/* Get the Year drop-down list * *
function Getcurrentyear ()
{
for ($i = Date (' Y '); $i >= $this->years; $i--)
{
echo "<option value= ' $i ' >{$i} years </option>\n";
}
}

/* Get the month drop-down list * *
function Getcurrentmonth ()
{
for ($i = 1; $i <= $this->months; $i + +)
{
($i <10)? ($m = "0". $i):($m = $i);
if ($i = = Date (' m '))
echo "<option value= ' $m ' selected>{$m} month </option>\n";
Else
echo "<option value= ' $m ' >{$m} month </option>\n";
}
}

/* Get the day drop-down list * *
function Getcurrentday ()
{
for ($i = 1; $i <= $this->days; $i + +) {
if ($i = = Date (' d '))
echo "<option value= ' $i ' selected>{$i} day </option>\n";
Else
echo "<option value= ' $i ' >{$i} day </option>\n";
}
}

/* Get week's dropdown list * *
function Getcurrentweek ()
{
for ($i = 1; $i <= $this->weeks; $i + +) {
if ($i = = Date (' W ')
echo "<option value= ' $i ' selected>{$i} week </option>\n";
Else
echo "<option value= ' $i ' >{$i} week </option>\n";
}
}
}
?>

The call is as follows:
Includ (".. /public/getcurrentdate.class.php ");
$getCurrentDate = Net getcurrentdate ();
<select name = "XXXXX" >
<?= $getCurrentDate->getcurrentyear ()?>
</select>
//////////////////////////////////////////////////////////


How to implement Java:
Getcurrentdate.java
/*
* Function: Generate Drop-down list (year/month/day/weekly basis current value)
* Programmer: Xiangli
* Date: 2003-01-19
*/

// #---------------------------------------------------#
# Modified: 2003-03-18 #
# modification Reason: Added week's Build #
// #-------------------------------------------------#

Import java.io.*;
Import java.util.*;
Import java.text.*;

public class Getcurrentdate {
public int Years = 2002;
public int Months = 12;
public int days = 31;
public int Weeks = 52;
Date mydate = new Date ();
SimpleDateFormat formatter = new SimpleDateFormat ("Yyyy-mm-dd w");

/* Get the Year drop-down list * *
Public String getcurrentyear ()
{
String Content = "";
for (int i = Integer.parseint (Formatter.format (mydate). toString (). substring (0, 4)); I >= Years; i--)
{
Content + + "<option value= '" + i + "' >" + i + "year </option>\n";

}
return Content;
}

/* Get the month drop-down list * *
Public String Getcurrentmonth ()
{
String m;
String Content = "";

for (int i = 1; I <= Months; i++)
{
M=i<10? ("0" + i): integer.tostring (i);
if (i = = Integer.parseint (Formatter.format (mydate). toString (). substring (5, 7))
Content + = "<option value= '" + M + "' selected>" + M + "month </option>\n";
Else
Content + = "<option value= '" + M + "' >" + M + "month </option>\n";
}
return Content;
}

/* Get the day drop-down list * *
Public String Getcurrentday ()
{
String Content = "";
String m;

for (int i = 1; I <= days; i++) {
M=i<10? ("0" + i): integer.tostring (i);
if (i = = Integer.parseint (Formatter.format (mydate). toString (). SUBSTRING (8, 10))
Content + = "<option value= '" + M + "' selected>" + M + "Day </option>\n";
Else
Content + + "<option value= '" + M + "' >" + M + "Day </option>\n";
}
return Content;
}

/* Get week's dropdown list * *
Public String Getcurrentweek ()
{
String Content = "";
String m;

for (int i = 1; I <= Weeks; i++) {
M=i<10? ("0" + i): integer.tostring (i);
if (i = = Integer.parseint (Formatter.format (mydate). toString (). SUBSTRING (11)))
Content + = "<option value= '" + M + "' selected>" + M + "Week </option>\n";
Else
Content + = "<option value= '" + M + "' >" + M + "Zhou </option>\n";
}
return Content;
}
}


Call Method:
<jsp:usebean id= "Getcurrentdate" scope= "session" class= "Getcurrentdate"/>
<select name= "Years" >
<%=getcurrentdate.getcurrentyear ()%>
</select>
<select name= "Months" >
<%=getcurrentdate.getcurrentmonth ()%>
</select>
<select name= "Days" >
<%=getcurrentdate.getcurrentday ()%>
</select>
<select name= "Weeks" >
<%=getcurrentdate.getcurrentweek ()%>
</select>

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.