(1)Java.util.Date类型转换成long类型
不要唠叨了,还是先上代码
- 1
- 2
- 1
- 2
解析:其中getTime()返回为long类型,长度为13,表示毫秒;如果想获得秒数,只需要除以1000即可。
- 1
- 1
(2)long类型转换成java.util.Date类型
方式一:
- 1
- 2
- 3
- 4
- 1
- 2
- 3
- 4
前面的mseconds是表示的是秒数,所以要先乘1000得到毫秒数,再转为java.util.Date类型,这样就完成了long到Date的转化;为了格式化输出Date,可以调用SimpleDateFormat的format方法格式化输出Date
方式二:
SimpleDateFormat format =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Long timeWeek=new Date().getTime()-(long)(7L*24L*60L*60L*1000L);
Long timeMonth=new Date().getTime()-(long)(30L*24L*60L*60L*1000L);
Long timeQuarter=new Date().getTime()-(long)(90L*24L*60L*60L*1000L);
String dateWeek = format.format(timeWeek);
String dateMonth = format.format(timeMonth);
String dateQuarter = format.format(timeQuarter);
Date dateWk=null;
Date dateMh=null;
Date dateQe=null;
try {
dateWk=format.parse(dateWeek);
dateMh=format.parse(dateMonth);
dateQe=format.parse(dateQuarter);
} catch (ParseException e) {
e.printStackTrace();
}
(3)形如“2015-08-31 21:08:06”等格式化string类型转换成java.util.Date类型
- 1
- 2
- 3
- 4
- 1
- 2
- 3
- 4
其输出结果为
- 1
- 1
有时候需要对CST时间进行转化,如:Wed Sep 16 11:26:23 CST 2009,此时同样可以用SimpleDateFormat的parse解决
- 1
- 2
- 3
- 1
- 2
- 3
备注(util类):
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 转自:http://blog.csdn.net/ouyang111222/article/details/48139803