我有一个可以正常工作的DatePicker,但是,当我将日期转换为时间戳时,时间戳是完全错误的。 请在下面查看我的代码:
final Calendar c = Calendar.getInstance();
int mYear = c.get(Calendar.YEAR); // current year
int mMonth = c.get(Calendar.MONTH); // current month
int mDay = c.get(Calendar.DAY_OF_MONTH); // current day
timestamp = c.getTimeInMillis();
c.getTimeInMillis
部分似乎没有获得正确的日期。有人知道如何解决这个问题吗?
答案 0 :(得分:0)
我怀疑您正在将timestamp
(以毫秒为单位)馈给需要几秒钟的例程。那些“ 3个额外的数字”是时间戳的毫秒部分。如果您觉得这很对,请将timestamp
除以1000,然后再继续进行。
答案 1 :(得分:0)
我认为您想获取当前的毫秒数,因此该指令将为:c.get(Calendar.MILLISECOND)
public static void main(String args[]){
final Calendar c = Calendar.getInstance();
int mYear = c.get(Calendar.YEAR); // current year
int mMonth = c.get(Calendar.MONTH); // current month
int mDay = c.get(Calendar.DAY_OF_MONTH); // current day
int mHour = c.get(Calendar.HOUR); // current hour
int mMinute = c.get(Calendar.MINUTE); // current min
int mMilisecond = c.get(Calendar.MILLISECOND); // current milisecond
System.out.println("Year: "+mYear+", Month: "+mMonth+", Day:"+mDay+", Hour: "+mHour+", mMinute: "+mMinute+", mMilisecond: "+mMilisecond);
}
现在方法getTimeInMillis
就像Java文档所说:
/**
* Returns this Calendar's time value in milliseconds.
*
* @return the current time as UTC milliseconds from the epoch.
* @see #getTime()
* @see #setTimeInMillis(long)
*/
一个时代是一个绝对的时间参考