how to get date from milliseconds in android

2019-02-21 15:30发布

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

how can i do this???

5条回答
叛逆
2楼-- · 2019-02-21 16:02

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);
查看更多
再贱就再见
3楼-- · 2019-02-21 16:05

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);
查看更多
Luminary・发光体
4楼-- · 2019-02-21 16:12

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)));
查看更多
等我变得足够好
5楼-- · 2019-02-21 16:14

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;

}
查看更多
仙女界的扛把子
6楼-- · 2019-02-21 16:17

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

查看更多
登录 后发表回答