如何从Android中获得毫秒日期(how to get date from millisecond

2019-07-04 09:09发布

我有时间milliseconds ,现在我要分开 timedate从这些milliseconds

我怎样才能做到这一点???

Answer 1:

你可以使用这样

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


Answer 2:

此功能将给予您毫秒的字符串日期

public static String getFormattedDateFromTimestamp(long timestampInMilliSeconds)
{
    Date date = new Date(); 
    date.setTime(timestampInMilliSeconds);
    String formattedDate=new SimpleDateFormat("MMM d, yyyy").format(date);
    return formattedDate;

}


Answer 3:

你可以在毫秒转换为日期对象,然后在时间字符串的格式,只是日期的另一个字符串中提取日期



Answer 4:

使用日历来获得不同的时间字段的值:

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


Answer 5:

转换millisecondsDate实例,并将其传递到所选择的格式:

SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");

String myDate = dateFormat.format(new Date(dateInMillis)));


文章来源: how to get date from milliseconds in android