13

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

how can i do this???

Saqib Abbasi
  • 607
  • 2
  • 6
  • 17

8 Answers8

26

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);
madlymad
  • 6,367
  • 6
  • 37
  • 68
Mohsin Naeem
  • 12,542
  • 3
  • 39
  • 53
  • It is showing wrong month. We’re in April, and I get 3. Also, while using `Calendar` was a good idea in 2012, it isn’t anymore. We’ve got a lot better in [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Apr 11 '20 at 17:00
  • I am late but yea for anyone wondering...if I am not wrong, in the Calendar class, 0 = January and not 1, so it is not showing the wrong month actually. But understandably, it can be confusing. – sammy Jan 12 '22 at 21:35
20

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;

}
Kiran Kumar
  • 1,192
  • 8
  • 10
4

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)));
Semih Yagcioglu
  • 4,011
  • 1
  • 26
  • 43
Summved Jain
  • 872
  • 2
  • 18
  • 21
3

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);
thaussma
  • 9,756
  • 5
  • 44
  • 46
2

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

mango
  • 5,577
  • 4
  • 29
  • 41
  • i didn't even mean `Date()` specifically but no it isn't, not yet, and not for these purposes. don't confuse everyone else. – mango Dec 02 '12 at 10:50
  • just `setTime` is not deprecated. other methods (most of them) are deprecated. So why involve `Date` to just set the time? Why not use `Calnedar class` – Mohsin Naeem Dec 02 '12 at 10:57
  • i have no agenda with any specific class. but if some methods are still working fine for what i need and are clearly not deprecated, i don't throw the baby out with the bathwater. – mango Dec 02 '12 at 11:02
  • yes but after `setTime`. When he is going to retrieve the date..then again he has no option^^ other then using the `Calender` class. Why not adopt a new born baby! – Mohsin Naeem Dec 02 '12 at 11:05
0

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;
}
Khalid Lakhani
  • 179
  • 2
  • 10
0

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

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

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)));
Amintabar
  • 2,198
  • 1
  • 29
  • 29
waseem
  • 116
  • 1
  • 13