3
I need to assemble a condition to subtract a day from the date, for example I created a new column in the database and I need to fill it, the condition to fill it is the following, I’ll take the column inicioPesagem
which saves me date and time, and I will check, if the schedule of that column inicioPesagem
is less than 06:00 to.m, or before 06:00 I need to set my new column a day before, pick up the date from inicioPesagem
, subtract a day and set, and if it is greater than 06:00 or after 06:00 set on the same day.
But I set up some test conditions and he’s not comparing, always falls into my else
ignoring the other conditions, I believe it is for the difference of format, who comes from the bank comes in the format 2017-09-26 11:55:44.0
(I don’t know where I get this .0
at the end) and the one that is comparing comes in this format Mon Oct 09 06:00:00 BRT 2017
, I think that’s the problem, but when I use the format
and after the parse
to convert to date again, my format is lost.
Follows the class created to insert the data in the column:
package metrix.model.database.util;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import metrix.model.entity.Pesagem;
import metrix.model.service.PesagemService;
import org.joda.time.DateTime;
/**
*
* @author ricardo.campos
*/
public class InsereDataMed {
public static void main(String[] args) throws ParseException, Exception {
List<Pesagem> pesagens = new ArrayList();
HibernateUtil.openSession();
pesagens = PesagemService.getAllPesos();
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
SimpleDateFormat sdfh = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
Date dataMed = new Date();
Date dataFixa = sdfh.parse(sdf.format(new Date()) + " 06:00:00");
HibernateUtil.getCurrentSession().beginTransaction();
for (Pesagem p : pesagens) {
int test = dataMed.compareTo(sdfh.parse(sdfh.format(dataFixa)));
if (test == 0) {
System.out.println("Igual");
} else if (test > 0) {
System.out.println("Depois");
} else {
System.out.println("Antes");
}
dataMed = p.getInicioPesagem();
if (dataMed.before(dataFixa)) {
DateTime dateTime = new DateTime(dataMed);
dateTime = dateTime.plusDays(-1);
p.setDataMed(dateTime.toDate());
} else {
p.setDataMed(p.getInicioPesagem());
}
PesagemService.salvar(p, HibernateUtil.getCurrentSession());
System.out.println("Inicio Pesagem: " + p.getInicioPesagem()+"\n"+"Data Fixa: " + dataFixa+"\n"+"Data Med: "+dataMed+"\n");
}
HibernateUtil.getCurrentSession().getTransaction().commit();
HibernateUtil.closeCurrentSession();
}
}
Gustavo thanks for wanting to help, I managed to solve the problem, in vdd it was a logic error in comparison even, it was not p format the problem,
– Ricardo Campos