Search code examples
javamysqlstringunix-timestampjava.util.date

String (Date and Time) to Unix Timestamp in Java


I am pulling data from an API, and one of the pieces of data I get is a date and time. The date and time is returned in a string format and I need to get that in to a Unix Timestamp for my mySQL database.

The String looks like: "2018-10-23 18:00:00"

Does anyone know how I would go about getting that from a String to a Unix Timestamp? Should I try using java.util.Date?


Solution

  • Similar to this

    Convert a date format in epoch

    but change

    SimpleDateFormat df = new SimpleDateFormat("MMM dd yyyy HH:mm:ss.SSS zzz");
    

    to

    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");