how to get date from milliseconds in android

一笑奈何 提交于 2019-12-01 14:26:34

问题


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

how can i do this???


回答1:


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



回答2:


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;

}



回答3:


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



回答4:


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




回答5:


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


来源:https://stackoverflow.com/questions/13667966/how-to-get-date-from-milliseconds-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!