I have one date and time format as below:
Tue Apr 23 16:08:28 GMT+05:30 2013
I want to convert into milliseconds, but I actually dont know which format it is. Can anybody please help me.
This is the number of seconds since the 1970 epoch. To convert seconds to milliseconds, you need to multiply the number of seconds by 1000. To convert a Date to milliseconds, you could just call timeIntervalSince1970 and multiply it by 1000 every time.
String myDate = "2014/10/29 18:10:45"; SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); Date date = sdf. parse(myDate); long millis = date. getTime();
In case of LocalDate , you can use the toEpochDay() method. It returns the number of days since 01/01/1970. That number then can be easily converted to milliseconds: long dateInMillis = TimeUnit.
Update for DateTimeFormatter introduced in API 26.
Code can be written as below for API 26 and above
// Below Imports are required for this code snippet // import java.util.Locale; // import java.time.LocalDateTime; // import java.time.ZoneOffset; // import java.time.format.DateTimeFormatter; String date = "Tue Apr 23 16:08:28 GMT+05:30 2013"; DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss z yyyy", Locale.ENGLISH); LocalDateTime localDate = LocalDateTime.parse(date, formatter); long timeInMilliseconds = localDate.atOffset(ZoneOffset.UTC).toInstant().toEpochMilli(); Log.d(TAG, "Date in milli :: FOR API >= 26 >>> " + timeInMilliseconds); // Output is -> Date in milli :: FOR API >= 26 >>> 1366733308000
But as of now only 6% of devices are running on 26 or above. So you will require backward compatibility for above classes. JakeWharton has been written ThreeTenABP which is based on ThreeTenBP, but specially developed to work on Android. Read more about How and Why ThreeTenABP should be used instead-of java.time, ThreeTen-Backport, or even Joda-Time
So using ThreeTenABP, above code can be written as (and verified on API 16 to API 29)
// Below Imports are required for this code snippet // import java.util.Locale; // import org.threeten.bp.OffsetDateTime; // import org.threeten.bp.format.DateTimeFormatter; DateTimeFormatter formatter = DateTimeFormatter.ofPattern( "EEE MMM dd HH:mm:ss OOOO yyyy", Locale.ROOT); String givenDateString = "Tue Apr 23 16:08:28 GMT+05:30 2013"; long timeInMilliseconds = OffsetDateTime.parse(givenDateString, formatter) .toInstant() .toEpochMilli(); System.out.println("Date in milli :: USING ThreeTenABP >>> " + timeInMilliseconds); // Output is -> Date in milli :: USING ThreeTenABP >>> 1366713508000
Ole covered summarised information (for Java too) in his answer, you should look into.
Below is old approach (and previous version of this answer) which should not be used now
Use below method
String givenDateString = "Tue Apr 23 16:08:28 GMT+05:30 2013"; SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy"); try { Date mDate = sdf.parse(givenDateString); long timeInMilliseconds = mDate.getTime(); System.out.println("Date in milli :: " + timeInMilliseconds); } catch (ParseException e) { e.printStackTrace(); }
Read more about date and time pattern strings.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With