java.util.Date
-----------
date.getTime()返回的是什么?
问题:
-------------
Date date = new Date();
System.out.println(date.getTime());
输出结果是1210745780625
编译时间当时时间大概是2008年5.14好14.16分
谁能给我解释下这数字分别是什么意思?
答案:
-------------
你想得到时间格式为2008-05-14这种吧?
date.getTime()所返回的是一个long型的毫秒数
获取特定格式的时间需要格式化的。
例子:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
sdf.format(new Date());
得到的日期格式为:2008-05-14
------------------------
扩展:date()的方法
1、獲取服務器端當前日期:
2、獲取當前年、月、日、星期:
3、按本地時區輸出當前日期
輸出結果為: 2003-5-30
4、獲取數據庫中字段名為"publish_time"、類型為Datetime的值
5、按照指定格式打印日期
-----------
date.getTime()返回的是什么?
问题:
-------------
Date date = new Date();
System.out.println(date.getTime());
输出结果是1210745780625
编译时间当时时间大概是2008年5.14好14.16分
谁能给我解释下这数字分别是什么意思?
答案:
-------------
你想得到时间格式为2008-05-14这种吧?
date.getTime()所返回的是一个long型的毫秒数
获取特定格式的时间需要格式化的。
例子:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
sdf.format(new Date());
得到的日期格式为:2008-05-14
------------------------
扩展:date()的方法
1、獲取服務器端當前日期:
- <%@ page import="java.util.Date"%>
- <%
- Date myDate = new Date();
- %>
<%@ page import="java.util.Date"%>
<%
Date myDate = new Date();
%>
2、獲取當前年、月、日、星期:
- <%@ page import="java.util.Date"%>
- <%
- Date myDate = new Date();
- int thisYear = myDate.getYear() + 1900;//thisYear = 2003
- int thisMonth = myDate.getMonth() + 1;//thisMonth = 5
- int thisDate = myDate.getDate();//thisDate = 30
- int thisDay = myDate.getDay();//thisDay = 1
- %>
<%@ page import="java.util.Date"%>
<%
Date myDate = new Date();
int thisYear = myDate.getYear() + 1900;//thisYear = 2003
int thisMonth = myDate.getMonth() + 1;//thisMonth = 5
int thisDate = myDate.getDate();//thisDate = 30
int thisDay = myDate.getDay();//thisDay = 1
%>
3、按本地時區輸出當前日期
- <%@ page import="java.util.Date"%>
- <%
- Date myDate = new Date();
- out.println(myDate.toLocaleString());
- %>
<%@ page import="java.util.Date"%>
<%
Date myDate = new Date();
out.println(myDate.toLocaleString());
%>
輸出結果為: 2003-5-30
4、獲取數據庫中字段名為"publish_time"、類型為Datetime的值
- <%@ page import="java.util.Date"%>
- <%
- ...連接數據庫...
- ResultSet rs = ...
- Date sDate = rs.getDate("publish_time");
- %>
<%@ page import="java.util.Date"%>
<%
...連接數據庫...
ResultSet rs = ...
Date sDate = rs.getDate("publish_time");
%>
5、按照指定格式打印日期
- <%@ page import="java.util.Date"%>
- <%@ page import="java.text.DateFormat"%>
- <%
- Date dNow = new Date();
- SimpleDateFormat formatter = new SimpleDateFormat("E yyyy.MM.dd 'at' hh:mm:ss a zzz");
- out.println("It is " + formatter.format(dNow));
- %>
本文详细介绍了 Java 中 Date 类的使用方法,包括如何获取当前日期、时间,并解释了 date.getTime() 方法返回的毫秒数含义。此外,还提供了多种日期格式化示例。



1012

被折叠的 条评论
为什么被折叠?



