I have my MySQL database inserting timestamp when I upload a record, so what's entered is something like 2013-02-02 16:59:29. Is there a Java way to convert that into something like 10 Days Ago?
问问题
7099 次
3 回答
10
假设您已将数据库中的日期时间值读入 java:
Date date; // read from database
int days = TimeUnit.MILLISECONDS.toDays(
System.currentTimeMillis() - date.getTime());
然后你可以随意格式化它。
于 2013-02-13T02:15:26.983 回答
2
借助结果集从 mysql 获取时间并将时间数据传递给以下方法
public static void main(String[] args) {
long timStampFromMysql = rs.getTimestamp("time");// Fetch time from mysql
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(convertTime(timStampFromMysql , 15));
System.out.println(cal);
}
public static long convertTime(long timeInMillies, int days)
{
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(timeInMillies);
calendar.add(Calendar.DAY_OF_YEAR, days);
return calendar.getTimeInMillis();
}
于 2013-02-13T03:45:31.740 回答
0
Joda TimeDays.daysBetween
库中的将为您计算。
DateTime start = new DateTime(time_ms);
DateTime end = new DateTime(now);
Days days = Days.daysBetween(start, end);
int d = days.getDays();
于 2013-11-30T07:33:00.260 回答