频道直达 - 学院 - 下载 - 交易 - 截图 - 特效 - 字库 - 手册 - 排名-工具 - 繁體
设为首页
加入收藏
联系我们
建站搜索: 虚拟主机  域名注册   常用广告代码      用户注册 | 用户登陆
您当前的位置:中国建站之家 -> 网站开发 -> PHP -> 文章内容
精彩推荐
热门文章
· 注册码大全二
· 注册码大全四
· 注册码大全一
· 通过google 赶快来赚..
· [图文] 头像-qq头像(..
· 要10G免费网络硬盘的..
· 注册码大全三
· 注册码大全十
· [图文] 梦幻背景图片..
· [图文] 卡通动物图片..
相关文章
· 给出一篇链表的
· Flash MX 认证考试(样题..
· ASP常用数据库连接及操作..
· Photoshop打造美女残缺美..
· 动态按钮-2
· Windows 2000 下 RDS 配..
· Flash 9预览版AS 3.0编程..
· 用C#写vs插件中的一些Ti..
· 青岛新闻网1G免费邮箱
· FLASH游戏编程第(1)课..
一个简单编程思想在php与java中的实现比较:日期类!
作者:未知  来源:转载  发布时间:2005-7-21 8:50:15  发布人:acx

减小字体 增大字体

以前用PHP时写了一个简单的class,功能主要是解决,大量页面上需要显示下拉列表框选择年/月/日/周之类的。希望对大家学习PHP和java能有帮助。

php的实现如下:
getCurrentDate.class.php
<?php
/*
* 功能:生成下拉列表(年/月/日/周为当前值)
* 程序员:xiangli
* 日期:2003-01-19
*/

#---------------------------------------------------#
# 修改:2003-03-18                                  #
# 修改原因:添加了周的生成                            #
#-------------------------------------------------#

class getCurrentDate{
  var    $Years = 2002;
  var    $Months = 12;
  var    $Days = 31;
  var    $Weeks = 52;
  
    /*获得年的下拉列表*/
    function getCurrentYear()
    {
        for ($i = Date(''Y''); $i >= $this->Years; $i--)
        {
            echo "<option value=''$i''>{$i}年</option>\n";
        }
    }

    /*获得月的下拉列表*/
    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}月</option>\n";
            else
                echo "<option value=''$m''>{$m}月</option>\n";
        }
    }

    /*获得日的下拉列表*/
    function getCurrentDay()
    {
        for ($i = 1; $i <= $this->Days; $i++){
            if($i == date(''d''))
                echo "<option value=''$i'' selected>{$i}日</option>\n";
            else
                echo "<option value=''$i''>{$i}日</option>\n";
        }
    }
    
    /*获得周的下拉列表*/
    function getCurrentWeek()
    {
        for ($i = 1; $i <= $this->Weeks; $i++){
            if($i == date(''W''))
                echo "<option value=''$i'' selected>{$i}周</option>\n";
            else
                echo "<option value=''$i''>{$i}周</option>\n";
        }
    }    
}
?>

调用如下:
includ("../public/getCurrentDate.class.php");
$getCurrentDate = net getCurrentDate();
<select name ="xxxxx">
<?=$getCurrentDate->getCurrentYear()?>
</select>
//////////////////////////////////////////////////////////


java的实现方法:
getCurrentDate.java
/*
* 功能:生成下拉列表(年/月/日/周为当前值)
* 程序员:xiangli
* 日期:2003-01-19
*/

// #---------------------------------------------------#
// # 修改:2003-03-18                                 #
// # 修改原因:添加了周的生成                         #
// #-------------------------------------------------#

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");
  
    /*获得年的下拉列表*/
    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 + "年</option>\n";
             
        }
        return Content;
    }

    /*获得月的下拉列表*/
    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 + "月</option>\n";
            else
                Content += "<option value=''" + m + "''>" + m + "月</option>\n";
        }
        return Content;
    }

    /*获得日的下拉列表*/
    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 + "日</option>\n";
            else
                Content += "<option value=''" + m + "''>" + m + "日</option>\n";
        }
        return Content;
    }
    
    /*获得周的下拉列表*/
    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 + "周</option>\n";
            else
                Content += "<option value=''" + m + "''>" + m + "周</option>\n";
        }
        return Content;
    }    
}


调用方法:
<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> 
[打 印]
[] [返回上一页] [收 藏]
下一篇文章:写不下了
∷相关文章评论∷    (评论内容只代表网友观点,与本站立场无关!) [更多评论...]
关于本站 - 网站帮助 - 广告合作 - 下载声明 - 友情连接 - 网站地图 - 人才招聘
网站合作、内容监督、商务咨询:QQ: 9576619
Copyright ? 2005--2008 中国建站之家版权所有
未经授权禁止转载、摘编、复制或建立镜像.如有违反,追究法律责任.
免责申明:中国建站之家(www.jz123.cn)上的所有提供下载的软件和资源
均来源于网络,为软件或程序作者提供和网友推荐收集整理而来,仅供学习
和研究使用。如有侵犯你的版权,请立即联系我们,本站将在3个工作日内删除。
粤ICP备05092265号