Gabrijel Gabrijel - 1 month ago 20
Android Question

Convert date to miliseconds

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 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?

Answer

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