Android convert date and time to milliseconds

后端 未结 10 2171
长发绾君心
长发绾君心 2020-11-28 12:05

I have one date and time format as below:

Tue Apr 23 16:08:28 GMT+05:30 2013

I want to convert into milliseconds, but I actually dont know

相关标签:
10条回答
  • 2020-11-28 12:18

    In Kotlin,

    Just use

    timeInMilSeconds = date.time
    

    where timeInMilSeconds is milliseconds(var timeInMilSeconds: Long) and date is Date

    0 讨论(0)
  • 2020-11-28 12:19

    Just to complement the given answers, if you need to convert the time given by the date to other time units you can use the TimeUnit API

    Example:

    TimeUnit.MILLISECONDS.toMinutes(millis)
    
    0 讨论(0)
  • 2020-11-28 12:21

    java.time and ThreeTenABP

    I am providing the modern answer — though not more modern than it works on your Android API level too.

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(
                "EEE MMM dd HH:mm:ss OOOO yyyy", Locale.ROOT);
        String givenDateString = "Tue Apr 23 16:08:28 GMT+05:30 2013";
        long timeInMilliseconds = OffsetDateTime.parse(givenDateString, formatter)
                .toInstant()
                .toEpochMilli();
        System.out.println(timeInMilliseconds);
    

    Output from this snippet is:

    1366713508000

    The SimpleDateFormat, Date and Calendar classes used in most of the other answers are poorly designed and now long outdated. I recommend that instead you use java.time, the modern Java date and time API. Edit: The other answers were fine answers when the question was asked in 2013. Only time moves on, and we should not use SimpleDateFormat, Date and Calendar any more.

    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 use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from org.threeten.bp with subpackages.

    Links

    • Oracle tutorial: Date Time explaining how to use java.time.
    • Java Specification Request (JSR) 310, where java.time was first described.
    • ThreeTen Backport project, the backport of java.time to Java 6 and 7 (ThreeTen for JSR-310).
    • ThreeTenABP, Android edition of ThreeTen Backport
    • Question: How to use ThreeTenABP in Android Project, with a very thorough explanation.
    0 讨论(0)
  • 2020-11-28 12:32

    try this...

    Calendar calendar = Calendar.getInstance();
    calendar.set(datePicker.getYear(), datePicker.getMonth(), datePicker.getDayOfMonth(), 
                 timePicker.getCurrentHour(), timePicker.getCurrentMinute(), 0);
    long startTime = calendar.getTimeInMillis();
    
    0 讨论(0)
提交回复
热议问题