Android: convert date to milliseconds

I used

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

to get the time (03/12/2012, 17:31), now I want to convert this time to milliseconds, because I have a file with the date and text of the pair, and I want to convert the dates in milliseconds so that I can not add text to the incoming 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 have to put time in milliseconds, I have to convert time, does anyone know how?

+4
source share
5 answers

The easiest way is to convert the 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(); 
+23
source

Use your date object and call date.getTime()

+2
source

If you want the current millisecond time to just use System.currentTimeMillis()

0
source

Use .getMillis ();

eg:

 DateTime dtDate = new DateTime(); dtDate.getMillis() 
0
source
 String Date = "Tue Apr 25 18:06:45 GMT+05:30 2017"; SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy"); try { Date mDate = sdf.parse(Date); long timeInMilliseconds = mDate.getTime(); } catch (ParseException e) { e.printStackTrace(); } 
0
source

Source: https://habr.com/ru/post/1401029/


All Articles