I used
SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy, HH:mm");
String time = formatter.format(new Date());
to get t
I recommend you use java.time, the modern Java date and time API, for your date and time work.
ZoneId zone = ZoneId.systemDefault();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd.MM.uuuu, H:mm");
String dateTimeString = "12.03.2012, 17:31";
LocalDateTime dateTime = LocalDateTime.parse(dateTimeString, formatter);
long milliseconds = dateTime.atZone(zone).toInstant().toEpochMilli();
System.out.println(milliseconds);
When I run this in Europe/Zurich time zone, the output is:
1331569860000
java.time works nicely on both older and newer Android devices. It just requires at least Java 6.
org.threeten.bp
with subpackages.java.time
was first described.java.time
to Java 6 and 7 (ThreeTen for JSR-310).Using this method you can Convert your Date Into miliseconds that can be used to add events to the Calender
public Long GettingMiliSeconds(String Date)
{
long timeInMilliseconds = 0;
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
try {
Date mDate = sdf.parse(Date);
timeInMilliseconds = mDate.getTime();
} catch (ParseException e) {
e.printStackTrace();
}
return timeInMilliseconds;
}
If you want the current time in millis just use System.currentTimeMillis()
Use .getMillis();
e.g:
DateTime dtDate = new DateTime();
dtDate.getMillis()
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();
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();
}