转换日期为毫秒 [英] Convert date to miliseconds

查看:507
本文介绍了转换日期为毫秒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用

SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy, HH:mm");
String time = formatter.format(new Date());

要获取时间(12.03.2012,17:31),现在我想将其转换时间毫秒,因为我有一对夫妇的日期和文本文件,我想将日期转换以毫秒为单位,这样我不能用在收件箱中添加文本

to get the time (12.03.2012, 17:31), now i want to convert this time to miliseconds, because i have a file with a couple dates and text, and i want to convert the dates in miliseconds so that i cant add the text in inbox using

ContentValues values = new ContentValues();
values.put("address", "123");
values.put("body", "tekst");
values.put("read", 1);
values.put("date", HERE I MUST PUT A DATE IN MILISECONDS);      
context.getContentResolver().insert(Uri.parse("content://sms/inbox"), values);

由于我必须把以毫秒为单位我必须转换的时间的时间,没有人知道如何?

Because i must put a time in miliseconds i must convert the time, does anyone know how?

推荐答案

最简单的方法是将日期键入毫秒:

The simplest way is to convert Date type to milliseconds:

SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy, HH:mm");
formatter.setLenient(false);

Date curDate = new Date();
long curMillis = curDate.getTime();
String curTime = formatter.format(curDate);

String oldTime = "05.01.2011, 12:45";
Date oldDate = formatter.parse(oldTime);
long oldMillis = oldDate.getTime();

这篇关于转换日期为毫秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆