2

我根据调用交易的日期/时间计算交易 ID。因此,我使用 .getTime() 将 Date 对象转换为 long num,代码如下

public long calcTransactionID (Date myDate)
{
    long transactionID;
    transactionID = myDate.getTime();
    return long;
}

完美运行。现在我该如何做相反的事情,给定我想要获取 Date 对象(或至少是字符串中的日期/时间)的交易 ID。

public getDateFromTrans (long transactionID)
{
    ???
    return myDate;
}

是否可以使用 Date.getTime() 构造一个日期对象?

4

2 回答 2

4

尝试:

public Date getDateFromTrans (long transactionID)
{
    return new Date(transactionID);
}
于 2013-11-02T03:57:05.327 回答
0

您也可以使用 a 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();
}
于 2013-11-02T04:17:09.487 回答