在android中转换毫秒的时间

时间:2014-04-07 07:42:05

标签: java android time

  Log.d("Time", String.valueOf(calendarSelected.get(Calendar.DAY_OF_MONTH) + "/" + (monthNumber+1) + "/" + year+ ", " + hour12 + ":" + min+ " " + AM_PM));

它返回日期,如

7/4/2014, 1:44 PM

我试过这个

Calendar calendar = Calendar.getInstance();

calendar.set(Calendar.DAY_OF_MONTH, calendarSelected.get(Calendar.DAY_OF_MONTH));
calendar.set(Calendar.MONTH, (monthNumber));
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.HOUR, hour24);
calendar.set(Calendar.MINUTE, min);
calendar.set(Calendar.SECOND, 1);

但它在milisec Tue Apr 08 01:44:01 GMT+06:00 2014中给了我1396899841127。这是错误的。

我如何在MiliSec中转换它?

1 个答案:

答案 0 :(得分:1)

System.currentTimeMillis()在1970-01-01T00:00:0之后给出毫秒,直到当前时刻。

定义从1970 -...直到您的开始时间的毫秒数。然后从currentTimeMillis的毫秒中减去该值。

private static final Long ZERO_HOUR;

static {
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.MONTH, (3));
    calendar.set(Calendar.DAY_OF_MONTH, (7));
    calendar.set(Calendar.YEAR, 2014);
    calendar.set(Calendar.HOUR, 1);
    calendar.set(Calendar.MINUTE, 44);
    calendar.set(Calendar.SECOND, 1);
    ZERO_HOUR = calendar.getTimeInMillis();
}

public static void main(final String[] args) {
    long currentTime = System.currentTimeMillis();
    long diff = currentTime - ZERO_HOUR;
    System.out.println(diff);
}