1、由java.util.Date类型得到long型表示时间的秒数
java.util.Date dt = new Date();
System.out.println(dt.toString());   //java.util.Date的含义
long lSysTime1 = dt.getTime() / 1000;   //得到秒数,Date类型的getTime()返回毫秒数
2、由long型表示的时间秒数得到某种格式表示的字符串的日期时间
import java.text.*;
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
//前面的lSysTime是秒数,先乘1000得到毫秒数,再转为java.util.Date类型
java.util.Date dt = new Date(lSysTime1 * 1000);  
String sDateTime = formatter.format(dt);  //得到精确到秒的表示:08/31/2006 21:08:00
formatter = new SimpleDateFormat("MM/dd/yyyy HH:mm");   //精确到分钟
sDateTime = formatter(dt);
System.out.println(sDateTime);
formatter = new SimpleDateFormat("MM/dd/yyyy HH");  //精确到小时
sDateTime = formatter(dt);
System.out.println(sDateTime);
 
3、由"08/31/2006 21:08:00"格式的String得到java.util.Date型
String sDt = "08/31/2006 21:08:00";
formatter = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
java.util.Date dt2 = formatter.parse(sDt);
//继续转换得到秒数的long型
long lTime = dt2.getTime() / 1000;
 
 
//注意HH与hh含认不同,HH表示以24小时制取,hh表示以12小时制取
常用的格式的含义,摘自Jdk,注意大小写的含义通常是不同的:
字母    含义                            示例
   Year                             1996;96             哪一年
  Month in year               uly;Jul;07           一年中的哪一月
  Minute in hour                 30                    一个小时中的第几分钟
  Week in year                   27                   一年中的第几个星期
  Week in month                                   一个月中的第几个星期
   Day in year                     189                 一年中的第几天
   Day in month                   10                  一个月中的第几天
   Hour in day (0-23)                             一天中的第几个小时(24小时制)
   Hour in am/pm (1-12)        12                  一天中上午、下午的第几个小时(12小时制)
   Millisecond                      978                 毫秒数
   Second in minute              55                  一分钟的第几秒
 
4、系统当前时间
long lSysTime2 = System.currentTimeMillis();   //得到毫秒表示的系统当前时间
//jdk1.5 API的解释
1)、java.util.Date.getTime()
public long getTime()
Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Date object.
Returns:
the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this date.

2)、System.currentTimeMillis()
public static long currentTimeMillis()
Returns the current time in milliseconds. Note that while the unit of time of the return value is a millisecond, the granularity of the value depends on the underlying operating system and may be larger. For example, many operating systems measure time in units of tens of milliseconds.
See the description of the class Date for a discussion of slight discrepancies that may arise between "computer time" and coordinated universal time (UTC).
posted on 2009-04-29 09:55  翌晨  阅读(2236)  评论(0编辑  收藏  举报