8

I used

SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy, HH:mm");
String time = formatter.format(new Date());

to get the time (12.03.2012, 17:31), now i want to convert this time to milliseconds, because i have a file with a couple dates and text, and i want to convert the dates in milliseconds so that i cant add the text in inbox using

ContentValues values = new ContentValues();
values.put("address", "123");
values.put("body", "tekst");
values.put("read", 1);
values.put("date", HERE I MUST PUT A DATE IN MILLISECONDS);     
context.getContentResolver().insert(Uri.parse("content://sms/inbox"), values);

Because i must put a time in milliseconds i must convert the time, does anyone know how?

Let's say I have a time 05.01.2011, 12:45 and want to convert it, how? I want to convert an old time that I have (not to get miliseconds from current time).

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
gabskoro
  • 175
  • 1
  • 2
  • 7

7 Answers7

30

The simplest way is to convert Date type to milliseconds:

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

Date curDate = new Date();
long curMillis = curDate.getTime();
String curTime = formatter.format(curDate);

String oldTime = "05.01.2011, 12:45";
Date oldDate = formatter.parse(oldTime);
long oldMillis = oldDate.getTime();
Name is Nilay
  • 2,743
  • 4
  • 35
  • 77
Aleks G
  • 56,435
  • 29
  • 168
  • 265
  • 1
    You dont have understand me, i know this. I want the to convert and old time that i have, not to get miliseconds from current time. Let's say i have a time 05.01.2011, 12:45 and want to convert it, how? – gabskoro Mar 12 '12 at 21:36
  • @Gabrijel Ok, I understand now. I updated my answer to include what you're after – Aleks G Mar 13 '12 at 09:09
  • `long oldMillis = oldDate.getTime()` this will only convert `12:45` to milliseconds not `05.01.2011, 12:45` as a whole! – Muhammad Babar Oct 23 '14 at 07:17
  • @MuhammadBabar I believe you are mistaken. Test it. – Aleks G Oct 23 '14 at 07:41
3

Use your date object and call date.getTime()

Dan S
  • 9,139
  • 3
  • 37
  • 48
1
String Date = "Tue Apr 25 18:06:45 GMT+05:30 2017"; 
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
try {
    Date mDate = sdf.parse(Date);
    long timeInMilliseconds = mDate.getTime();

} catch (ParseException e) {
            e.printStackTrace();
}
Makvin
  • 3,475
  • 27
  • 26
1

If you want the current time in millis just use System.currentTimeMillis()

pandre
  • 6,685
  • 7
  • 42
  • 50
0

Use .getMillis();

e.g:

DateTime dtDate = new DateTime();
dtDate.getMillis()
Nickmccomb
  • 1,467
  • 3
  • 19
  • 34
0

Using this method you can Convert your Date Into miliseconds that can be used to add events to the Calender

public Long GettingMiliSeconds(String Date)
    {
        long timeInMilliseconds = 0;

        SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
        try {

            Date mDate = sdf.parse(Date);
            timeInMilliseconds = mDate.getTime();

        } catch (ParseException  e) {
            e.printStackTrace();
        }

        return  timeInMilliseconds;
    }
  • (1) I recommend you don’t use `SimpleDateFormat` and `Date`. Those classes are poorly designed and long outdated, the former in particular notoriously troublesome. Instead use classes from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). (2) Are you contributing anything that isn’t already in some other answer? – Ole V.V. Sep 24 '20 at 06:18
  • 1) i have not worked on Java date and time API till now i'll use for next time 2) i think if you are getting date in different formats from database there is no accorate answer to change date into mili seconds so i decided to add this on this. – Gulfam Raza Sep 24 '20 at 08:14
  • it is a complete method to convert date and time into miliseconds weather in which format you got the datetime . – Gulfam Raza Sep 24 '20 at 08:15
0

java.time and desugaring or ThreeTenABP

I recommend you use java.time, the modern Java date and time API, for your date and time work.

    ZoneId zone = ZoneId.systemDefault();
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd.MM.uuuu, H:mm");
    
    String dateTimeString = "12.03.2012, 17:31";
    
    LocalDateTime dateTime = LocalDateTime.parse(dateTimeString, formatter);
    long milliseconds = dateTime.atZone(zone).toInstant().toEpochMilli();
    
    System.out.println(milliseconds);

When I run this in Europe/Zurich time zone, the output is:

1331569860000

Question: Doesn’t java.time require Android API level 26?

java.time works nicely on both older and newer Android devices. It just requires at least Java 6.

  • In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.
  • In non-Android Java 6 and 7 get the ThreeTen Backport, the backport of the modern classes (ThreeTen for JSR 310; see the links at the bottom).
  • On older Android either use desugaring or the Android edition of ThreeTen Backport. It’s called ThreeTenABP. In the latter case make sure you import the date and time classes from org.threeten.bp with subpackages.

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161