Android:将日期转换为毫秒 [英] Android: Convert date to milliseconds

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

问题描述

我使用

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

获取时间(12.03.2012,17:31),现在我想转换这个时间到毫秒,因为我有一个几个日期和文本的文件,我想以毫秒为单位转换日期,以便我不能使用



<$ p $添加收件箱中的文本p> ContentValues values = new ContentValues();
values.put(address,123);
values.put(body,tekst);
values.put(read,1);
values.put(date,这里我必须在MILLISECONDS中输入日期);
context.getContentResolver()。insert(Uri.parse(content:// sms / inbox),values);

因为我必须在毫秒级的时间内转换时间,有没有人知道如何? p>

解决方案

最简单的方法是将 Date 类型转换为毫秒:

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

日期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();


I used

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

to get the time (12.03.2012, 17:31), now i want to convert this time to milliseconds, because i have a file with a couple dates and text, and i want to convert the dates in milliseconds 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 MILLISECONDS);     
context.getContentResolver().insert(Uri.parse("content://sms/inbox"), values);

Because i must put a time in milliseconds 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();

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

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