jsp中擷取年月日時分秒

來源:互聯網
上載者:User

import java.util.*;

int y,m,d,h,mm;
Calendar c = Calendar.getInstance();
y = c.get(Calendar.YEAR); //年
m = c.get(Calendar.MONTH) + 1; //月
d = c.get(Calendar.DAY_OF_MONTH); //日
h = c.get(Calendar.HOUR); //時(HOUR——十二小時制;HOUR_OF_DAY——二十四小時制)
mm = c.get(Calendar.MINUTE); //分

2:
java.util.Date d = new java.util.Date();
java.text.SimpleDateFormat dformat = new java.text.SimpleDateFormat("yyyy-MM-dd");
String datetime = dformat.format(d);
System.out.println(datetime);

3:
import java.util.*;
import java.text.*;

Date now = new Date();
DateFormat defaultFormat = DateFormat.getDateInstance();
DateFormat shortFormat = DateFormat.getDateInstance(DateFormat.SHORT);
DateFormat mediumFormat = DateFormat.getDateInstance(DateFormat.MEDIUM);
DateFormat longFormat = DateFormat.getDateInstance(DateFormat.LONG);
DateFormat fullFormat = DateFormat.getDateInstance(DateFormat.FULL);
String defaultDate = defaultFormat.format(now);
String shortDate = shortFormat.format(now);
String mediumDate = mediumFormat.format(now);
String longDate = longFormat.format(now);
String fullDate = fullFormat.format(now);

System.out.println("(Default) Today :" + defaultDate);
System.out.println("(SHORT) Today : " + shortDate);
System.out.println("(MEDIUM) Today :" + mediumDate);
System.out.println("(LONG) Today : " + longDate);
System.out.println("(FULL) Today : " + fullDate);
4.
<%
java.util.Date now=new java.util.Date();
out.println(now);
%>
如果只要年月日,可以用subString()截取.
或者用這個 String time=new SimpleDateFormat("yyyy-MM-dd").format(Calendar.getInstance().getTime);

5.
Date currentTime = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String dateString = formatter.format(currentTime);
dataString 的格式就是2007-03-08這類型

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.