8

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?

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
Reyjohn
  • 2,654
  • 9
  • 37
  • 63
  • 1
    Does this help? http://stackoverflow.com/questions/7735705/how-to-convert-date-format-to-milliseconds – Ivan Jul 11 '12 at 16:15
  • I need to format it from string, but the link shows how to convert from date type value – Reyjohn Jul 11 '12 at 16:18

4 Answers4

7

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();

gkris
  • 1,209
  • 3
  • 17
  • 44
6

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();
K_Anas
  • 31,226
  • 9
  • 68
  • 81
  • 2
    This gives a parse exception, instead use this format: SimpleDateFormat sdf = new SimpleDateFormat("dd-M-yyyy hh:mm:ss"); – jai May 26 '16 at 13:02
0

try this:

import java.util.*;


public class ConvertDateIntoMilliSecondsExample{

public static void main(String args[]){

//create a Date object  
Date date = new Date();
System.out.println("Date is : " + date);

//use getTime() method to retrieve milliseconds
System.out.println("Milliseconds since January 1, 1970, 00:00:00 GMT : "
                  + date.getTime());

    }

}
0

java.time

Solution using java.time, the modern date-time API*:

import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        // Test
        System.out.println(toMillis("2012-07-11 10:55:21"));
    }

    public static long toMillis(String strDateTime) {
        // Replace the parameter, ZoneId.systemDefault() which returns JVM's default
        // timezone, as applicable e.g. to ZoneId.of("America/New_York")
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("u-M-d H:m:s", Locale.ENGLISH)
                                    .withZone(ZoneId.systemDefault());

        ZonedDateTime zdt = ZonedDateTime.parse(strDateTime, dtf);
        
        Instant instant = zdt.toInstant();
        
        return instant.toEpochMilli();
    }
}

Output:

1342000521000

Learn more about the the modern date-time API* from Trail: Date Time.


*For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110