posts - 48,  comments - 5,  trackbacks - 0
  日期时间显示的多种格式

1、日期时间显示的java类: 文件名:Datetime.java

2、在JSP文件中显示不同的日期时间格式:JSP文件名:datetime.jsp

3、日期时间脚本显示:文件名:time.htm

--------------日期时间显示的java类: 文件名:Datetime.java -------------

//日期时间显示的多种格式类:文件名:Datetime.java

import java.text.DateFormat;

import java.text.SimpleDateFormat;

import java.util.Calendar;

import java.util.Date;

import java.util.Locale;

/**

 *日期时间显示的多种格式类

 *以不同方法实现日期时间的不同显示格式

 *@author逍湘

 *@version1.0

 *@time2007年7月26日上午10时23分51秒

 */

publicclass Datetime {

   

    /**

     *以字符串格式显示日期时间(ThuJul2610:23:51CST2007)

     *@returndatetime

     */

    public String getDatetime_String1(){

        String datetime=new Date().toString();

        return datetime;

    }

   

    /**

     *以字符串格式显示日期时间(26Jul200702:23:51GMT)

     *@returndatetime

     */

    public String getDatetime_String2(){

        String datetime=new Date().toGMTString();

        return datetime;

    }  

   

    /**

     *以系统格式显示日期时间(yy-MM-dd上午HH:mm)

     *@returndatetime

     */

    public String getDatetime_System(){

        DateFormat dt=DateFormat.getInstance();

        String datetime=dt.format(new Date()).toString();

        return datetime;

    }

   

    /**

     *以中国格式显示日期时间(xxxx年xx月xx日下午xx时xx分xx秒)

     *@returndatetime

     */

    public String getDatetime_China(){

         DateFormat datetime1=DateFormat.getDateInstance(DateFormat.LONG,Locale.CHINA);

         DateFormat datetime2=DateFormat.getTimeInstance(DateFormat.LONG,Locale.CHINA);

         String datetime=datetime1.format(new Date())+" "+datetime2.format(new Date());

        return datetime;

    }

   

    /**

     *以常用格式显示日期时间(yyyy-MM-ddHH:mm:ss+MILLISECOND)

     *@returndatetime

     */

    public String getDatetime_Standard(){

         Calendar now=Calendar.getInstance();

         String datetime =

                       now.get(Calendar.YEAR)+"-" +

                       (now.get(Calendar.MONTH)+1)+"-" +

                       now.get(Calendar.DAY_OF_MONTH)+" "+

                       now.get(Calendar.HOUR)+":"+

                       now.get(Calendar.MINUTE)+":"+

                       now.get(Calendar.SECOND)+

                       now.get(Calendar.MILLISECOND);                   

         return datetime;

    }

   

    /**

     *以常用格式显示日期时间(yyyy-MM-ddHH:mm:ss)

     *@returndatetime

     */

    public String getDatetime(){      

         String datetime =new Date().toLocaleString();                  

         return datetime;

    }  

    /**

     *以简单系统格式显示日期时间(yyyy-MM-dd)

     *@return

     */

    public String getDatetime_SimpleDateFormat (){

        try {

            SimpleDateFormat f = new java.text.SimpleDateFormat("yyyy-MM-dd");

            String sDate = f.format(new java.util.Date());

            java.util.Date dt = f.parse(sDate);

            java.sql.Date sqlDate = new java.sql.Date(dt.getTime());

            String datetime=sqlDate.toString();

            return datetime;

        } catch (Exception ee) {

            ee.printStackTrace();

            returnnull;

        }

    }

   

   

    /**

     *测试显示结果

     *@paramargs

     */

    publicstaticvoid main(String[] args) {

        // TODO自动生成方法存根

        System.out.println("以字符串格式显示日期时间: "+new Datetime().getDatetime_String1());

        System.out.println("以字符串格式显示日期时间: "+new Datetime().getDatetime_String2());

        System.out.println("以系统格式显示日期时间: "+new Datetime().getDatetime_System());

        System.out.println("以中国格式显示日期时间: "+new Datetime().getDatetime_China());

        System.out.println("以常用格式显示日期时间: "+new Datetime().getDatetime_Standard());

        System.out.println("以常用格式显示日期时间: "+new Datetime().getDatetime());

        System.out.println("以简单系统格式显示日期: "+new Datetime().getDatetime_SimpleDateFormat());

    }

}

----在JSP文件中显示不同的日期时间格式:JSP文件名:datetime.jsp------

<%@ page language="java" pageEncoding="GBK"%>

<%@ page import="java.util.*" %>

<%@ page import="java.text.*;" %>

<html>

 <head>   

    <title>日期时间显示</title>

 </head>

 <body bgcolor="#76B9C7">

    日期时间显示 <br>

    <hr>

       

<%

//************ 以字符串格式显示日期时间 **********************************************

 Date time=new Date();

 out.print("字符串格式:"+ time );

 %>

 <br>字符串格式:<%=new Date().toGMTString() %><p>

 <hr>

 <%

//************ 以系统格式显示日期时间 **********************************************

 DateFormat df1=DateFormat.getInstance();

    out.print( "系统格式:"+ df1.format(new Date()));

 %>

 <br>系统格式:<%= df1.format(new Date())%><p>

 <hr>

<%

//********** 以中国格式显示日期时间 **********************************************

 DateFormat df2=DateFormat.getDateInstance(DateFormat.LONG,Locale.CHINA);

 DateFormat df3=DateFormat.getTimeInstance(DateFormat.LONG,Locale.CHINA);

 out.print("中国格式:"+df2.format(new Date())+" ");

 out.print(df3.format(new Date()));

 %>

<br>中国格式:<%= df2.format(new Date())+" "+ df3.format(new Date())%><p>

<hr>

<%

//********** 以常用格式显示日期时间 **********************************************

 Calendar now=Calendar.getInstance();

 String nowtime =

               now.get(Calendar.YEAR)+"-" +

               (now.get(Calendar.MONTH)+1)+"-" +

               now.get(Calendar.DAY_OF_MONTH)+" "+

               now.get(Calendar.HOUR)+":"+

               now.get(Calendar.MINUTE)+":"+

               now.get(Calendar.SECOND)+

               now.get(Calendar.MILLISECOND);

 out.print("常用格式:"+nowtime );

 %>

<br>常用格式:<%=new Date().toLocaleString() %><p>

<hr>

<%

//********** 以简单系统格式显示日期时间 **********************************************

        String datetime="";

        try {

            SimpleDateFormat f = new java.text.SimpleDateFormat("yyyy-MM-dd");

            String sDate = f.format(new java.util.Date());

            java.util.Date dt = f.parse(sDate);

            java.sql.Date sqlDate = new java.sql.Date(dt.getTime());

            datetime=sqlDate.toString();           

        } catch (Exception ee) {

            ee.printStackTrace();          

        }

 out.print("简单系统格式:"+datetime );

 %>

<br>简单系统格式:<%= datetime %><p>

<hr>

 </body>

</html>

---------------- 日期时间脚本显示 -------------------------------

<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=gb2312">

<title>日期时间脚本显示</title>

</head>

<body bgcolor="#AED5E8"><center>

日期时间脚本显示<p><hr>

<!-- 第一种形式 -->

<div align="center">

<SCRIPT language=JavaScript>

<!--

var noon;

var time=new Date();

var hour=time.getHours();

var minute=time.getMinutes();

var today=new Date;

var week=new Array(7);

week[0]="天";

week[1]="一";

week[2]="二";

week[3]="三";

week[4]="四";

week[5]="五";

week[6]="六";

if (hour<5) noon="凌晨";

if (hour>4 & hour<8) noon="早晨";

if (hour>7 & hour<12) noon="上午";

if (hour==12) noon="中午";

if (hour>12 & hour<19) noon="下午";

if (hour>18 & hour<23) noon="晚上";

if (hour>22) noon="深夜";

if (hour>12) hour=hour-12;

if (hour<10) hour="0"+hour;

if (minute<10) minute="0"+minute;

document.write("<span class='p9'>");

document.write(today.getYear()+"年"+(today.getMonth()+1)+"月"+today.getDate()+"日 星期"+week[today.getDay()]+" "+"北京时间:"+noon+" "+hour+":"+minute+" ");

document.write("</span>");

//-->

</SCRIPT>

</div>

<p><hr>

<!-- 第二种形式 -->

<FONT color=#f8941f>        

<SCRIPT language=JavaScript>

<!--                                                                            

// Begin                                                                             

   document.write("今天是:")

   var enable=0; today=new Date();                                                                            

   var day; var date;                                                                             

   var time_start = new Date();                                                                            

   var clock_start = time_start.getTime();                                                                             

   if(today.getDay()==0) day="星期日"                                                                            

   if(today.getDay()==1) day="星期一"                                                                            

   if(today.getDay()==2) day="星期二"                                                                            

   if(today.getDay()==3) day="星期三"                                                                            

   if(today.getDay()==4) day="星期四"                                                                             

   if(today.getDay()==5) day="星期五"                                                                            

   if(today.getDay()==6) day="星期六"                                                                             

   date=(today.getYear())+"年"+(today.getMonth()+1)+"月"+today.getDate()+"日 ";                                                                            

   document.write(date+day);                                                                             

// -->

</SCRIPT><br>

<SCRIPT language=JavaScript>

<!--

var mess1="你正在登陆的是:逍湘个人网!";

var mess2= "你正在登陆的是:逍湘个人网!"

day = new Date( )

hr = day.getHours( )

if (( hr >=0 ) && (hr <= 4 ))

mess1= "夜深了,逍湘提醒您要注意身体哦..."

if (( hr >= 4 ) && (hr < 7))

mess1=" 清晨好,您可真早呀!吃早餐了吗?"

if (( hr >= 7 ) && (hr < 12))

mess1=" 上午好,您开始工作了? "

if (( hr >= 12) && (hr <= 19))

mess1= "下午好,您工作辛苦了!"

if ((hr >= 19) && (hr <= 23))

mess1= "晚上好,您真是分秒必争呀! "

document.write("<blink>")

document.write(mess1)

document.write("</blink>")

document.write(mess2)

// -->

</SCRIPT>

</FONT>

<p><hr>

<!-- 第三种形式 -->

<div id="clock" ></div>

<SCRIPT language=Javascript1.2>

<!--

function show() {

 if(!document.layers && !document.all) return;

 var d = new Date();

 var iYear = d.getYear()

 var iMonth = d.getMonth()

 var iDay = d.getDate()

 var sWeek = d.getDay()

 var sHour = d.getHours()

 var sMinute = d.getMinutes()

 var sSecond = d.getSeconds()

 if(sWeek == 0) sWeek = "<font color=#ffffff>星期天</font>";

 if(sWeek == 1) sWeek = "星期一";

 if(sWeek == 2) sWeek = "星期二";

 if(sWeek == 3) sWeek = "星期三";

 if(sWeek == 4) sWeek = "星期四";

 if(sWeek == 5) sWeek = "星期五";

 if(sWeek == 6) sWeek = "<font color=#ffffff>星期六</font>";

 if(sHour <= 9) sHour = "0" + sHour;

 if(sMinute <= 9) sMinute = "0" + sMinute;

 if(sSecond <= 9) sSecond = "0" + sSecond;

 sClock = "&nbsp;&nbsp;" + iYear + "年" + (iMonth + 1) + "月" + iDay + "日 " + sWeek

    + " " + sHour + ":" + sMinute + ":" + sSecond;

 if(document.layers) {

    document.layers.clock.document.write(sClock);

    document.layers.clock.document.close();

 }

 else if(document.all)

    clock.innerHTML = sClock;

 setTimeout("show()", 1000);

}

show();

//-->

</SCRIPT>

<p><hr>

<!-- 第四种形式 -->

<FONT color=#ff0033>

      <DIV id=jnkc></DIV>

<SCRIPT>

setInterval("jnkc.innerHTML=new Date().toLocaleString()+' 星期'+'日一二三四五六'.charAt (new Date().getDay());",1000);

</SCRIPT>

</FONT>

<p><hr>

</center>

</body>

</html>


人生在世,一生何求。浪迹天涯,独自逍遥。
E-mail:tylz7758@163.com
QQ: 297187963
posted on 2010-08-08 23:59 逍湘 阅读(1097) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航:
 

留言簿(2)

随笔档案(49)

文章档案(17)

最新随笔

积分与排名

  • 积分 - 25767
  • 排名 - 1500

最新评论

阅读排行榜