Reputation: 352
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?
Upvotes: 1
Views: 8960
Reputation: 3033
The Days.daysBetween
from the Joda Time library will compute that for you.
DateTime start = new DateTime(time_ms);
DateTime end = new DateTime(now);
Days days = Days.daysBetween(start, end);
int d = days.getDays();
Upvotes: 0
Reputation: 7016
Fetch time from mysql with the help of resultset and pass time data to below method
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();
}
Upvotes: 2
Reputation: 425083
Assuming you've read the datetime value from the database into java:
Date date; // read from database
int days = TimeUnit.MILLISECONDS.toDays(
System.currentTimeMillis() - date.getTime());
then you can format it as you like.
Upvotes: 10