开发者

calculate the time passed after an entry of date with the current time of the day

     PreparedStatement pStmt = con.prepareStatement("insert into mydate values(?,?)");

         java.util.Date now = new java.util.Date();

         pStmt.setDate( 1, new java.sql.Date( now.getTime()) );


         pStmt.setTimestamp( 2, new java.sql.Times开发者_如何学运维tamp( now.getTime() ) );
          pStmt.executeUpdate();

QUERY1: I am creating post a comment module .I want to display the time when user posted their comments.also i want to show the time passed after they have posted a comment. like 2 days ago ,5 min before etc.

QUERY2: i also want to change the format of the date to dd/mm/yyyy.


For displaying the date formatted you can use java.text.SimpleDateFormat or org.apache.commons.lang.time.FastDateFormat

For the time intervals you can use org.apache.commons.lang.time.DurationFormatUtils

Here is a sample:

//this will get the date in format dd/mm/yyyy
FastDateFormat fmt = FastDateFormat.getInstance("dd/MM/yyyy");
String txt = fmt.format(date);

//this will get the interval
long ival = System.currentTimeMilis()-date.getTime();
String interval = DurationFormatUtils.formatDurationWords(ival, true, true);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜