Saqib Abbasi
Saqib Abbasi

Reputation: 617

how to get date from milliseconds in android

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

how can i do this???

Upvotes: 14

Views: 28052

Answers (8)

waseem
waseem

Reputation: 166

You can use the Date format and set your millisecond value as a parameter to this constructor, Follow this code:

SimpleDateFormat SDF= new SimpleDateFormat("dd/MM/yyyy"); 
String date = SDF.format(new Date(millies)));

Upvotes: 0

Anonymous
Anonymous

Reputation: 86369

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

Upvotes: 0

Khalid Lakhani
Khalid Lakhani

Reputation: 188

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;
}

Upvotes: 0

Summved Jain
Summved Jain

Reputation: 890

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

Upvotes: 4

Mohsin Naeem
Mohsin Naeem

Reputation: 12642

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

Upvotes: 27

thaussma
thaussma

Reputation: 9886

Use a Calendar to get the values of different time fields:

Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(timeInMillis);
int dayOfMonth = cal.get(Calendar.DAY_OF_MONTH);
int monthOfYear = cal.get(Calendar.MONTH);

Upvotes: 3

Kiran Kumar
Kiran Kumar

Reputation: 1212

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;

}

Upvotes: 21

mango
mango

Reputation: 5636

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

Upvotes: 2

Related Questions