date.getTime()
Posted jtlgb
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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();
- %>
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
- %>
3、按本地時區輸出當前日期
- <%@ 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");
- %>
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));
- %>
輸出的結果為:
It is 星期五 2003.05.30 at 11:30:46 上午 CST
(更為詳盡的格式符號請參看SimpleDateFormat類)
6、將字符串轉換為日期
- <%@ page import="java.util.Date"%>
- <%@ page import="java.text.DateFormat"%>
- <%
- String input = "1222-11-11";
- SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
- Date t = null;
- try{
- t = formatter.parse(input);
- out.println(t);
- }catch(ParseException e){
- out.println("unparseable using" + formatter);
- }
- %>
輸出結果為:Fri Nov 11 00:00:00 CST 1222
7、計算日期之間的間隔
- <%@ page import="java.util.Date"%>
- <%@ page import="java.text.DateFormat"%>
- <%
- String input = "2003-05-01";
- SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
- Date d1 = null;
- trycatch(ParseException e)
- Date d2 = new Date();
- long diff = d2.getTime() - d1.getTime();
- out.println("Difference is " + (diff/(1000*60*60*24)) + " days.");
- %>
輸出結果為:
Difference is 29 days.
8、日期的加減運算
方法:用Calendar類的add()方法
- <%@ page import="java.util.*"%>
- <%@ page import="java.text.*"%>
- <%
- Calendar now = Calendar.getInstance();
- SimpleDateFormat formatter = new SimpleDateFormat("E yyyy.MM.dd ‘at‘ hh:mm:ss a zzz");
- out.println("It is now " + formatter.format(now.getTime()));
- now.add(Calendar.DAY_OF_YEAR,-(365*2));
- out.println("<br>");
- out.println("Two years ago was " + formatter.format(now.getTime()));
- %>
輸出結果為:
It is now 星期五 2003.05.30 at 01:45:32 下午 CST
Two years ago was 星期三 2001.05.30 at 01:45:32 下午 CST
9、比較日期
方法:用equals()、before()、after()方法
- <%@ page import="java.util.*"%>
- <%@ page import="java.text.*"%>
- <%
- DateFormat df = new SimpleDateFormat("yyy-MM-dd");
- Date d1 = df.parse("2000-01-01");
- Date d2 = df.parse("1999-12-31");
- String relation = null;
- if(d1.equals(d2))
- relation = "the same date as";
- else if(d1.before(d2))
- relation = "before";
- else
- relation = "after";
- out.println(d1 +" is " + relation + ‘ ‘ + d2);
- %>
輸出結果為:
Sat Jan 01 00:00:00 CST 2000 is after Fri Dec 31 00:00:00 CST 1999
10、記錄一件事所花費的時間
方法:調用兩次System.getTimeMillis()方法,求差值
- <%@ page import="java.text.*"%>
- <%
- long t0,t1;
- t0 = System.currentTimeMillis();
- out.println("Cyc starts at " + t0);
- int k = 0;
- for(int i =0;i<100000;i++)
- t1 = System.currentTimeMillis();
- out.println("<br>");
- out.println("Cyc ends at " + t1);
- out.println("<br>");
- out.println("This run took " + (t1-t0) + "ms.");
- %>
輸出結果為:
Cyc starts at 1054275312432
Cyc ends at 1054275312442
This run took 10ms.
11、其它:如何格式化小數
- <%@ page import="java.text.*"%>
- <%
- DecimalFormat df = new DecimalFormat(",###.00");
- double aNumber = 33665448856.6568975;
- String result = df.format(aNumber);
- out.println(result);
- %>
輸出結果為:
33,665,448,856.66
以上是关于date.getTime()的主要内容,如果未能解决你的问题,请参考以下文章
TypeError:未定义不是一个对象(评估'details.date.getTime')
如何将 Timestamp(date.getTime()) 转换为 ms 访问日期? [复制]