天天看点

JSP如何显示年月日星期时分秒

瑕??虫?剧ず?哄??绉????风???ユ???煎???

??? 棣???锛??ㄦ?版??搴?mysql涓?涓??芥???ユ??瀛?娈靛??涔???datetime绫诲??锛?瑕?淇?瀛?涓?archar绫诲??锛?????涓?绠′?瀛??ヤ?涔??煎?锛????烘?ラ?芥??01-09-09???峰???

??? ?舵?★??ㄥ???版??搴?瀛??版????java涓?锛???浠ュ?╃?ㄦ?浜?瀛??ュ??绉??煎???simpledatetime绫诲???版????

??? ????锛??ㄥ???ユ????jsp??浠朵腑锛????哄????璁板?锛?浠?etstring?瑰????烘?ュ氨??浠ヤ?锛?灏辨??浣??宠????煎???

????????etDate???虹?跺????impleDateFormat?绘?煎?????浣?甯??????煎?锛?

娉?涓?锛??╃??avascript

<script language=JavaScript>

today=new Date();

function initArray(){

this.length=initArray.arguments.length

for(var i=0;i<this.length;i++)

this[i+1]=initArray.arguments[i] }

var d=new initArray(

"??????,

"????涓?",

"????浜?",

"????涓?",

"??????",

"????浜?",

"??????");

document.write(

"<font color=##000000 style='font-size:9pt;font-family: 骞煎??'> ",

today.getYear(),"骞?,

today.getMonth()+1,"??",

today.getDate(),"??",

d[today.getDay()+1],

"</font>" );

</script>

娉?浜?锛?JSP浠g??

<%@ page contentType="text/html;charset=GB2312" %>

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

<%! public String getDayWeek(int n)

??? { String week[]={"??????,"????涓?","????浜?","????涓?","??????","????浜?","??????"};

????? return week[n];

??? }

%>

<HTML>

<BODY bgcolor=cyan><Font size=1>

<% Calendar calendar=Calendar.getInstance(); //??寤轰?涓??ュ??瀵硅薄??

??????????? calendar.setTime(new Date());//?ㄥ????堕?村??濮????ュ???堕?淬??

?? String 骞?String.valueOf(calendar.get(Calendar.YEAR)),

????????? ??=String.valueOf(calendar.get(Calendar.MONTH)+1),

????????? ??String.valueOf(calendar.get(Calendar.DAY_OF_MONTH)),

????????? ????=getDayWeek(calendar.get(Calendar.DAY_OF_WEEK)-1);

int hour=calendar.get(Calendar.HOUR_OF_DAY),

????? minute=calendar.get(Calendar.MINUTE),

????? second=calendar.get(Calendar.SECOND);

%>

<P>?板?ㄧ???堕?存??<BR>

<%=骞?>骞? <%=??%>??

<%=??>?ャ??

<%=????%>

<BR>

<%=hour%>?? <%=minute%>??

<%=second%>绉?

<% if(??equals("26"))

???? {out.print("<BR><H2>浠?澶╂????姣?瀹规????浣????ュ??锛?</H2>");

???? }

??? if(hour>=22)

???? { out.print("<BR><H2>?板?ㄦ?堕?村???浜?娉ㄦ??浼???</H2>");

???? }

%>??

</FONT>

</BODY>

</HTML>

娉?涓?锛?SimpleDateFormat?ㄦ?

?public class SimpleDateFormat extends DateFormat

SimpleDateFormat ??涓?涓?浠ョ?瑰?????????瑰??煎??????????版?????蜂?绫汇?? 瀹???璁告?煎??? (date -> text)??璇?娉????? (text -> date)??????????

SimpleDateFormat ??璁镐互涓烘?ユ??-?堕?存?煎??????╀换浣??ㄦ?锋??瀹????瑰????ㄣ?? 浣???锛?甯?????DateFormat 涓??? getTimeInstance?? getDateInstance ?? getDateTimeInstance ??寤轰?涓??ユ??-?堕?存?煎???绋?搴??? 姣?涓?绫绘?规?杩???涓?涓?浠ョ己???煎????瑰???濮??????ユ??锛??堕?存?煎???绋?搴?????浠ユ?规????瑕???applyPattern ?规?淇??规?煎????瑰???

SimpleDateFormat?芥?扮??缁ф?垮?崇郴锛?

java.lang.Object

??? |

??? +----java.text.Format

??????????? |

??????????? +----java.text.DateFormat

??????????????????? |

??????????????????? +----java.text.SimpleDateFormat

涓??㈡??涓?灏?渚?瀛?锛?

import java.text.*;

import java.util.Date;

public class FormatDateTime {

???? public static void main(String[] args) {

???????? SimpleDateFormat myFmt=new SimpleDateFormat("yyyy骞?M??dd??HH??m??ss绉?");

???????? SimpleDateFormat myFmt1=new SimpleDateFormat("yy/MM/dd HH:mm");

???????? SimpleDateFormat myFmt2=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//绛?浠蜂?now.toLocaleString()

???????? SimpleDateFormat myFmt3=new SimpleDateFormat("yyyy骞?M??dd??HH??m??ss绉? E ");

???????? SimpleDateFormat myFmt4=new SimpleDateFormat(

???????????????? "涓?骞翠腑??绗? D 澶╀?骞翠腑绗?w涓?????涓???涓?绗?W涓??????ㄤ?澶╀腑k??z?跺??);

???????? Date now=new Date();

???????? System.out.println(myFmt.format(now));

???????? System.out.println(myFmt1.format(now));

???????? System.out.println(myFmt2.format(now));

???????? System.out.println(myFmt3.format(now));

???????? System.out.println(myFmt4.format(now));

???????? System.out.println(now.toGMTString());

???????? System.out.println(now.toLocaleString());

???????? System.out.println(now.toString());

???? }???

???

}

????锛?

2004骞?2??16??17??4??27绉?

04/12/16 17:24

2004-12-16 17:24:27

2004 骞?2??16??17??4??27绉? ??????

涓?骞翠腑??绗? 351 澶?涓?骞翠腑绗?51涓????? 涓???涓?绗?3涓????? ?ㄤ?澶╀腑17??CST?跺?? 16 Dec 2004 09:24:27 GMT

2004-12-16 17:24:27

Thu Dec 16 17:24:27 CST 2004

----------------------

??ava涓???寰?褰?????绯荤??堕?村苟涓?杞???????宸辨?宠????煎?

???? ??瑕?寮?杩? java.util.Calendar ?? java.text.SimpleDateFormat 杩?涓や釜绫?

??????? Calendar rightNow = Calendar.getInstance();

???????? SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMddhhmmss");

???????? String sysDatetime = fmt.format(rightNow.getTime());??

??? ??浠ュ??new SimpleDateFormat("yyyyMMddhhmmss") 涓?寮??烽???㈢???煎?杩?琛?缂?杈?,杞??㈡????宸辩?歌????煎?,姣?濡?杩???浠ヨ浆

??? ?㈡????? new??? SimpleDateFormat("yyyy/MM/dd??? hh:mm:ss??? ") ???煎?

?

java ?ユ??澶???锛?杞?锛?

?

1.ResultSet.getDate() 浠??版??搴?涓????烘?ユ??涓?ava.sql.Date锛?瀹????剁被灏辨??java.util.Date

绀轰?锛?

??? SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy骞?M??dd??);

??? System.out.println(simpleDateFormat.format(new Date()));

杈??虹???涓猴?2010骞?4??01?? ??? SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy骞?M??d??);

??? System.out.println(simpleDateFormat.format(new Date()));

杈??虹???涓猴?2010骞?4??1?? ??? SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM");

??? System.out.println(simpleDateFormat.format(new Date()));

杈??虹???涓猴?04锛?

娉??SimpleDateFormat涓????煎?绫诲????澶?绉?锛??规????渚???绫诲??杩?????瀹?绫诲??????String绫诲??)锛???????API

2.Calendar绫绘??涓?涓??借薄绫伙??峰??瀹?渚??规?锛?Calendar rightNow = Calendar.getInstance();

绀轰?锛?

??? Calendar calendar= Calendar.getInstance();

??? calendar.setTime(new Date());

??? calendar.get(Calendar.MONTH);

娉??杩?绉?杩???杩??????兼??int绫诲????

3.Timestamp?ㄦ?ュ????版??搴??堕?撮??棰?

绀轰?锛?

??? SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss");

??? System.out.println(simpleDateFormat.format(ResultSet.getDate(""));

缁???杈??轰负锛?00:00:00

淇??逛负锛?

??? System.out.println(simpleDateFormat.format(ResultSet.getTimestamp(""));

灏辨???剧ず姝e父???跺??绉?浜?锛?

4.涓??ユ?????堕?寸?稿?崇??绫? java.lang.System/ System.currentTimeMillis();

java.util.Date

java.util.Calendar

java.util.GregorianCalendar

java.util.TimeZone

java.text.DateFormat

?版??搴??稿?崇??涓?涓?绫伙???java.util.Date??瀛?绫? java.sql.Date

java.sql.Time

java.sql.Timestamp

5.??瀛?绗?覆杞??㈡???ユ?????堕?寸被??

1锛?浣跨??imestamp??瀛?绗?覆??yyyy-mm-dd hh:mm:ss.fffffffff ?煎?

绀轰?锛?

??? //浣跨??yyyy-mm-dd hh:mm:ss.fffffffff ?煎??? String 瀵硅薄

??? String s = "2010-04-01 12:12:12.0";

??? Timestamp timestamp = Timestamp.valueOf(s);

2锛?TimeZone??Calendar

绀轰?锛?

??? Calendar hkCalendar = new GregorianCalendar(TimeZone.getTimeZone("Hongkong"));

??? hkCalendar.get(Calendar.DAY_OF_WEEK);

??? System.out.println(hkCalendar.get(Calendar.DAY_OF_WEEK));

??? for (String id : TimeZone.getAvailableIDs()) {

????? System.out.println(id);//杩????虹?扮???奸?藉??浠ュ?虹?板??imeZone.getTimeZone("id")涓???id涓?

??? }