0

Here is my defaultuser service class I want the difference to be stored in hh:mm:ss format in database (I'm using spring mvc+hibernate)

@Override
@Transactional
public LoginLog punchLogoutTime(User user) {
    LoginLog log = getOpenUserLoginLog(user);
    log.setLogoutTime(new Date());
    long diff=log.getLogoutTime().getTime()-log.getLoginTime().getTime();
    log.setTotalTime(diff);
    log = loginLogDao.update(log);
    return log;
}

Here is my pojo class.I take totaltime as long.

@Column(name = "total_time")
private Long totalTime;

public Long getTotalTime() {
    return totalTime;
}

public void setTotalTime(Long difference) {
    this.totalTime = difference;
}

Please tell me appropriate answer .Thanks:)

Alex Salauyou
  • 14,185
  • 5
  • 45
  • 67
Vivek
  • 25
  • 9
  • 2
    This is not a good approach, because even if you generate such date and format it as `hh:mm:ss`, then durations exceeding 24 hours will be stored incorrectly. – Alex Salauyou Mar 29 '16 at 17:24
  • have you got time in milliseconds ? then it is very easy just by dividing `milli-seconds` by ` 24*60*60*1000` – Vikrant Kashyap Mar 29 '16 at 17:25
  • 1
    You should store a non formatted value (imagine someone ask you for a different format later) –  Mar 29 '16 at 17:25
  • Storing duration as `Long` as you do now is much better. If you need to format it to some text representation, use [`Duration`](https://docs.oracle.com/javase/8/docs/api/java/time/Duration.html) or write your own method (either on server or client side). – Alex Salauyou Mar 29 '16 at 17:32

2 Answers2

0


//assume diff is in millisec

DateFormat df = new SimpleDateFormat("dd:MM:yy:HH:mm:ss"); System.out.println(df.format(diff));

//remove dd:mm:yy if you want

Gaurav
  • 126
  • 7
  • I did it!makes no effect! :( – Vivek Mar 29 '16 at 17:55
  • show me your code what you did? – Gaurav Mar 29 '16 at 17:58
  • i dont know where to put it.but i put it as:LoginLog log = getOpenUserLoginLog(user); log.setLogoutTime(new Date()); long diff=log.getLogoutTime().getTime()-log.getLoginTime().getTime(); DateFormat df = new SimpleDateFormat("dd:MM:yy:HH:mm:ss"); System.out.println(df.format(diff)); log.setTotalTime(diff); log = loginLogDao.update(log); return log; – Vivek Mar 29 '16 at 18:00
  • change your setTotalTime(diff) to setTotalTime(df.format(diff)) and then appropriately change your pojo to take String as input instead of long. – Gaurav Mar 29 '16 at 18:12
  • Thanks Gaurav ,I think You are close to it.! when i debug it everything goes fine but at the end it gives me exception(java.sql.SQLException: Data truncated for column 'total_time' at row 1).... – Vivek Mar 29 '16 at 18:32
  • that exception is because data type in your DB there it is expecting Date or long (i dont know) but the method is sending String. – Gaurav Mar 29 '16 at 18:34
  • maybe!how can I resolve it?do you have any idea?:( – Vivek Mar 29 '16 at 18:48
0

Modify your entity class such as below to specify the format of the Date.

//Create Table SQL in SQL Server
  CREATE TABLE "DBO"."Test"(
     Id INT PRIMARY KEY NOT NULL,
     difftime TIME not null
  )

//Entity Code
@Entity(name = "Test")
@Table(name = "Test")
public class Test {

    @Id
    @Column(name = "Id")
    private int id;

    @Column(name = "difftime")
    @DateTimeFormat(pattern = "hh:mm:ss")
    private Date time;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public Date getTime() {
        return time;
    }

    public void setTime(Date time) {
        this.time = time;
    }

}

//Controller Code for saving

 @Override
        @Transactional
        public LoginLog punchLogoutTime(User user) {
            LoginLog log = getOpenUserLoginLog(user);
            log.setLogoutTime(new Date());
            long diff=log.getLogoutTime().getTime()-log.getLoginTime().getTime();
            Date totalTimeIn=new Date();
            totalTimeIn.setTime(diff);          
            log.setTotalTime(totalTimeIn);
            log = loginLogDao.update(log);
            return log;
        }

enter image description here

Shaan
  • 588
  • 1
  • 4
  • 15
  • do I need to change anything in defaultuser class?? – Vivek Mar 29 '16 at 17:53
  • Which database are you using? I know SQLServer has TIME datatype. – Shaan Mar 29 '16 at 18:04
  • m using mysql .but when i change total time as Date then i too need to change in defaultuser class and it gives me error (change type of diff to long.... – Vivek Mar 29 '16 at 18:10
  • I just worked on an example for you and edited and posted the same. Can you update me with your exception – Shaan Mar 29 '16 at 18:23
  • Thnaks for your help shaan.but I didnt get the concept of making testSave method .I need to edit in same method. – Vivek Mar 29 '16 at 18:30
  • That is for my example to show case you how I did. Just change in your entity class and assign a Date object from the long (time difference) '@Column(name = "difftime") @DateTimeFormat(pattern = "hh:mm:ss") private Date time;' – Shaan Mar 29 '16 at 18:33
  • Make sure that your table-column created as TIME datatype. – Shaan Mar 29 '16 at 18:35
  • did it!but gives me exception (org.apache.jasper.JasperException: PWC6033: Unable to compile class for JSP||PWC6197)..... – Vivek Mar 29 '16 at 18:45
  • I just updated your code how it should be in my above example. Make sure **@Column(name = "total_time") @DateTimeFormat(pattern = "hh:mm:ss") private Date totalTime;** – Shaan Mar 29 '16 at 18:48
  • Make sure below point **1) You entity class compiled fine 2) You pojo class compiled fine. 3) Table created with Time Datetype for total_time column 4) Did you assign Date object instead of long in pojo.** – Shaan Mar 29 '16 at 18:55
  • Did you figure out the issue.. vivek? – Shaan Mar 29 '16 at 19:15
  • no shaan ..i did exactly the same but total_time goes null with same exception..! :/ – Vivek Mar 30 '16 at 04:26
  • Did you get any difference in long in Java code? If you print the value or break point, can help with this. Can you attach the complete log. – Shaan Mar 30 '16 at 11:29