我根据调用事务的日期/时间计算事务ID。因此我使用.getTime()将Date对象转换为long num,代码如下
public long calcTransactionID (Date myDate)
{
long transactionID;
transactionID = myDate.getTime();
return long;
}
完美无缺。现在我该怎么做反过来,给定事务ID我想得到Date对象(或者至少是字符串中的日期/时间)。
public getDateFromTrans (long transactionID)
{
???
return myDate;
}
是否可以使用Date.getTime()?
构造日期对象答案 0 :(得分:4)
尝试:
public Date getDateFromTrans (long transactionID)
{
return new Date(transactionID);
}
答案 1 :(得分:0)
您也可以使用java.util.Calendar
,因为:
public void writeDateAndTime(long transactionId) {
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(transactionId);
// this is how to get each part
int year = calendar.get(Calendar.YEAR);
int month = Calendar.get(Calendar.MONTH);
int day = Calendar.get(Calendar.DAY_OF_MONTH);
int hour = Calendar.get(Calendar.HOUR_OF_DAY);
int minute = Calendar.get(Calendar.MINUTE);
int second = Calendar.get(Calendar.SECOND);
// and this is how to get a java.util.Date object
Date date = Calendar.getTime();
}