Android convert date and time to milliseconds
try this...
Calendar calendar = Calendar.getInstance();
calendar.set(datePicker.getYear(), datePicker.getMonth(), datePicker.getDayOfMonth(),
timePicker.getCurrentHour(), timePicker.getCurrentMinute(), 0);
long startTime = calendar.getTimeInMillis();
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.
You can use this code
long miliSecsDate = milliseconds ("2015-06-04");
Log.d("miliSecsDate", " = "+miliSecsDate);
public long milliseconds(String date)
{
//String date_ = date;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try
{
Date mDate = sdf.parse(date);
long timeInMilliseconds = mDate.getTime();
System.out.println("Date in milli :: " + timeInMilliseconds);
return timeInMilliseconds;
}
catch (ParseException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return 0;
}