I want to convert the date and time received from the system into a integer in Android Studio with Java
For example
System Date/Time: 2021-06-18 19:51:43 --> Convert to :1624029703
please guide me
I want to convert the date and time received from the system into a integer in Android Studio with Java
For example
System Date/Time: 2021-06-18 19:51:43 --> Convert to :1624029703
please guide me
You can create a method dateConverter()
that accept a String
public static long dateConverter(String date) {
DateTimeFormatter dateFormatter
= DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
return LocalDate.parse(date, dateFormatter)
.atStartOfDay(ZoneOffset.UTC)
.toInstant()
.toEpochMilli();
}
DateTimeFormatter is a replacement for the old SimpleDateFormat.
You can then call this method from the main
.
public class Main {
public static void main(String[] args) {
long longDate = dateConverter("2021-06-18 19:51:43");
System.out.println("Long Date is: " + longDate);
}