SWT using DATETIME problem in saving compatible SQL format

Asked

Viewed 203 times

5

The problem is this: I have a personal finance program that will register some important data, among them the registration date and the due date. Program interfaces are made in Eclipse at SWT, and I’m using the widget DateTime, that uses a native windows calendar instance and shows on the program screen.

It turns out that I can not store in the database this format, because it is not compatible with any type SQL, as Date or TimeStamp. I already tried to convert this value to String and to Date, but he won’t take it. Someone has an idea?

I hope I made clear the problem without the need to post code as it is a format compatibility problem SWT with SQL. If anyone uses SWT or has an idea of how to show a summary calendar (dd/MM/yyyy) on screen, pro user track the date of registration and choose the due date to save later in the bank, I appreciate the help.

  • For his comment on anthony’s answer, the solution he gave worked - I suggest you accepted his response by clicking on the right, in the upper left corner of the answer. This is how we repay the help the community gives us.

1 answer

2


I’d convert to a middleman like Calendar (Java <= 7) or LocalDate (in Java 8):

Calendar:

Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_MONTH, dateTime.getDay());
calendar.set(Calendar.MONTH, dateTime.getMonth());
calendar.set(Calendar.YEAR, dateTime.getYear());
// Em alguns bancos como o Oracle tipo `DATE` pode incluir tempo 
// Em bancos sem resolução de tempo não é necessário zerar os campos
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0)
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);

Localdate

LocalDate instance = LocalDate.of(dateTime.getYear(),
        dateTime.getMonth() + 1, // SWT Retorna mes no padrão 0 - 11
        dateTime.getDay());

From then on it’s a matter of converting to a guy sqlDate as you would normally.

java.sql.Date sqlDateFromCalendar = new java.sql.Date(calendar.getTimeInMillis()); 
java.sql.Date sqlDateFromLocalDate = new java.sql.Date(Date.from(localDate
        .atStartOfDay(ZoneId.systemDefault()).toInstant()).getMillis());
  • Thanks for the help, I used Local Date and it worked perfectly.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.