您的位置:首页 > 其它

时间戳转换

2017-08-10 11:50 28 查看

数据库转换

时间“yyyy-MM-dd hh:mm:ss”转“yyyy-MM-dd”

select convert(char(10),getdate(),120)
select convert(char(10),getdate(),110)
select convert(char(10),getdate(),100)
结果:
2010-09-25
09-25-2010
09-25-2010


Java转换

Created with Raphaël 2.1.0SimpleDateFormat format = newSimpleDateFormat("yyyy-MM-dd HH:mm:ss");时间戳(Long)时间戳(Long)StringStringDateDateformat()parse()getTime()

1、时间戳转化为Date(or String)

//时间戳转化为Sting或Date
SimpleDateFormat format =  newSimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Long time=newLong(445555555);
String d = format.format(time);
Date date=format.parse(d);
System.out.println("Format To String(Date):"+d);
System.out.println("Format To Date:"+date);


运行结果:

Format To String(Date):1970-01-06 11:45:55

Format To Date:Tue Jan 06 11:45:55 CST 1970

2、Date(or String)转化为时间戳

//Date或者String转化为时间戳
SimpleDateFormat format =  newSimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time="1970-01-06 11:45:55";
Date date = format.parse(time);
System.out.print("Format To times:"+date.getTime());


运行结果:

Format To times:445555000

3、注意

定义SimpleDateFormat时newSimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);里面字符串头尾不能有空格,有空格那是用转换时对应的时间空格也要有空格(两者是对应的),比如:

SimpleDateFormat format =  newSimpleDateFormat(" yyyy-MM-dd HH:mm:ss ");
String time=" 1970-01-06 11:45:55 ";//注:改正后这里前后也加了空格
Date date = format.parse(time);
System.out.print("Format To times:"+date.getTime());


4、java中Date类中的getTime()是获取时间戳的,java中生成的时间戳精确到毫秒级别,而unix中精确到秒级别,所以通过java生成的时间戳需要除以1000。

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Baidu {

/**
* @param args
*/
public static void main(String[] args) {
try {
String time = "2011/07/29 14:50:11";
Date date = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").parse(time);
long unixTimestamp = date.getTime()/1000;
System.out.println(unixTimestamp);
} catch (ParseException e) {
e.printStackTrace();
}
}
}


js转换

Date转时间戳

// 获取当前时间戳(以s为单位)
var timestamp = Date.parse(new Date());
timestamp = timestamp / 1000;
//当前时间戳为:1403149534
console.log("当前时间戳为:" + timestamp);


String转时间戳

// 获取某个时间格式的时间戳
var stringTime = "2014-07-10 10:21:12";
var timestamp2 = Date.parse(new Date(stringTime));
timestamp2 = timestamp2 / 1000;
//2014-07-10 10:21:12的时间戳为:1404958872
console.log(stringTime + "的时间戳为:" + timestamp2);


时间(戳)转String

// 将当前时间换成时间格式字符串
var timestamp3 = 1403058804;
var newDate = new Date();
newDate.setTime(timestamp3 * 1000);
// Wed Jun 18 2014
console.log(newDate.toDateString());
// Wed, 18 Jun 2014 02:33:24 GMT
console.log(newDate.toGMTString());
// 2014-06-18T02:33:24.000Z
console.log(newDate.toISOString());
// 2014-06-18T02:33:24.000Z
console.log(newDate.toJSON());
// 2014年6月18日
console.log(newDate.toLocaleDateString());
// 2014年6月18日 上午10:33:24
console.log(newDate.toLocaleString());
// 上午10:33:24
console.log(newDate.toLocaleTimeString());
// Wed Jun 18 2014 10:33:24 GMT+0800 (中国标准时间)
console.log(newDate.toString(
8f36
));
// 10:33:24 GMT+0800 (中国标准时间)
console.log(newDate.toTimeString());
// Wed, 18 Jun 2014 02:33:24 GMT
console.log(newDate.toUTCString());


设置格式转换

newDate.format('yyyy-MM-dd h:m:s')
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: