how to get date from milliseconds in android

后端 未结 7 1527
情话喂你
情话喂你 2021-01-17 16:32

I have time in milliseconds, now I want to separate time and date from these milliseconds.

how

相关标签:
7条回答
  • 2021-01-17 16:55

    Further to Kiran Kumar Answer

     public static String getFormattedDateFromTimestamp(long timestampInMilliSeconds, String dateStyle){
            Date date = new Date(); 
            date.setTime(timestampInMilliSeconds);
            String formattedDate=new SimpleDateFormat(dateStyle).format(date);
            return formattedDate;
    }
    
    0 讨论(0)
  • 2021-01-17 16:58

    Convert the milliseconds to Date instance and pass it to the chosen formatter:

    SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
    
    String myDate = dateFormat.format(new Date(dateInMillis)));
    
    0 讨论(0)
  • 2021-01-17 17:03

    you can use like this

    Calendar cl = Calendar.getInstance();
    cl.setTimeInMillis(milliseconds);  //here your time in miliseconds
    String date = "" + cl.get(Calendar.DAY_OF_MONTH) + ":" + cl.get(Calendar.MONTH) + ":" + cl.get(Calendar.YEAR);
    String time = "" + cl.get(Calendar.HOUR_OF_DAY) + ":" + cl.get(Calendar.MINUTE) + ":" + cl.get(Calendar.SECOND);
    
    0 讨论(0)
  • 2021-01-17 17:04

    java.time and ThreeTenABP

    I suggest java.time, the modern Java date and time API, for your date and time work:

        long millisecondsSinceEpoch = 1_567_890_123_456L;
    
        ZonedDateTime dateTime = Instant.ofEpochMilli(millisecondsSinceEpoch)
                .atZone(ZoneId.systemDefault());
        LocalDate date = dateTime.toLocalDate();
        LocalTime time = dateTime.toLocalTime();
    
        System.out.println("Date: " + date);
        System.out.println("Time: " + time);
    

    Output in my time zone (Europe/Copenhagen):

    Date: 2019-09-07
    Time: 23:02:03.456
    

    The date and time classes used in the other answers — Calendar, Date and SimpleDateFormat — are poorly designed and long outdated. This is why I don’t recommend using any of them but prefer java.time.

    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)
  • 2021-01-17 17:06

    You could convert the milliseconds to a date object and then extract date in the format of a time string and another string of just the date

    0 讨论(0)
  • 2021-01-17 17:09

    This function will give you a String date from milliseconds

    public static String getFormattedDateFromTimestamp(long timestampInMilliSeconds)
    {
        Date date = new Date(); 
        date.setTime(timestampInMilliSeconds);
        String formattedDate=new SimpleDateFormat("MMM d, yyyy").format(date);
        return formattedDate;
    
    }
    
    0 讨论(0)
提交回复
热议问题