how to get a formatted date as milliseconds?
Asked Answered
S

4

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?

Sprig answered 11/7, 2012 at 16:13 Comment(2)
Does this help? #7736205Marelya
I need to format it from string, but the link shows how to convert from date type valueSprig
P
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();

Pincers answered 11/7, 2012 at 16:23 Comment(0)
D
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();
Diahann answered 11/7, 2012 at 16:22 Comment(1)
This gives a parse exception, instead use this format: SimpleDateFormat sdf = new SimpleDateFormat("dd-M-yyyy hh:mm:ss");Clifford
M
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());

    }

}
Malayan answered 11/7, 2012 at 16:21 Comment(0)
A
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.

Aenea answered 10/5, 2021 at 10:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.