Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Convert date to milliseconds

Tags:

date

android

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?

Let's say I have a time 05.01.2011, 12:45 and want to convert it, how? I want to convert an old time that I have (not to get miliseconds from current time).

like image 465
gabskoro Avatar asked Mar 12 '12 16:03

gabskoro


People also ask

How do you find milliSeconds from a Date?

Javascript date getMilliseconds() method returns the milliseconds in the specified date according to local time. The value returned by getMilliseconds() is a number between 0 and 999.

How to create Date with milliSeconds in java?

Create another one with the same time in millis + 1 (with Date secondDate = new Date(firstDate. getTime() + 1) ).


1 Answers

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();
like image 119
Aleks G Avatar answered Sep 18 '22 06:09

Aleks G