JAVA-近日遇到的java的日期时间类型的几个问题总结

每次一想到要用java的日期就巨头大。:(
 
特意总结一下:(以后再慢慢补充了,我就不信了,一个日期时间我搞不定你了,小样儿的。)
 
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,注意大小写的含义通常是不同的:
字母    含义                            示例
y    Year                             1996;96             哪一年
M   Month in year  J              uly;Jul;07           一年中的哪一月
m   Minute in hour                 30                    一个小时中的第几分钟
w   Week in year                   27                   一年中的第几个星期
W   Week in month                 2                    一个月中的第几个星期
D    Day in year                     189                 一年中的第几天
d    Day in month                   10                  一个月中的第几天
H    Hour in day (0-23)            0                   一天中的第几个小时(24小时制)
h    Hour in am/pm (1-12)        12                  一天中上午、下午的第几个小时(12小时制)
S    Millisecond                      978                 毫秒数
s    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).
 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值