Home > Software design >  How to get timestamp if I have date in android?
How to get timestamp if I have date in android?

Time:12-07

I want a method that can convert the given date and time to timestamp. Format of the date: "dd-MM-yyyy"

CodePudding user response:

Try to Use SimpleDateFormat(DATE_FORMAT)

try {
    val dateFormat = SimpleDateFormat("yyyy-MM-dd'T'HH:mm")
    val date: Date = dateFormat.parse(YOUR_DATE_IN_STRING)
    val timeStamp = date.time
} catch (e: ParseException) {
    e.printStackTrace()
}

CodePudding user response:

You should write your question clearly.

Here is the code to convert Date time to the timestamp for java.

try {
     SimpleDateFormat simpleDate = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss", Locale.ENGLISH);
     Calendar cal = Calendar.getInstance();
     cal.setTime(simpleDate.parse("4-12-2021 11:30:05"));
     System.out.println("0000000000000 =>"   cal.getTimeInMillis());
} catch (ParseException e) {
     e.printStackTrace();
}
  • Related