how to get a formatted date as milliseconds?

23,651

Solution 1

You can convert the string into a Date object using this code:-

Date d = DateFormat.parse(String s)

And then convert it into milliseconds by using the inbuilt method

long millisec = d.getTime();

Solution 2

Use date.getTime()

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd, HH:mm:ss");
formatter.setLenient(false);


String oldTime = "2012-07-11 10:55:21";
Date oldDate = formatter.parse(oldTime);
long oldMillis = oldDate.getTime();
Share:
23,651
Reyjohn
Author by

Reyjohn

I develop android application from the very beginning of my career.

Updated on July 15, 2022

Comments

  • Reyjohn
    Reyjohn almost 2 years

    I have a formatted date from sqllite database, to use this in a graph view I need to format it in a long number.

    The format is:

    2012-07-11 10:55:21

    how can I convert it to milliseconds?