且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在Android中将字符串日期转换为时间戳?

更新时间:2023-02-03 08:46:56

如果你使用 Date 对象的 getTime() 你会得到毫秒的时间.无需使用 Timestamp 即可获得结果.

If you use getTime() of Date object you will get time in millisecond. No need to use Timestamp to get your result.

String str_date="13-09-2011";
DateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
Date date = (Date)formatter.parse(str_date); 
System.out.println("Today is " +date.getTime());

上面的代码会打印出你需要的类似 1312828200000 的东西,这是长值.

The above code will print something like 1312828200000 you need and this is long value.