Lost date format

Asked

Viewed 65 times

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();

    }

}

1 answer

1

From what I’ve seen you’re wearing dating + 6 hours just to make the comparison. This is not required in the new Java 8 Date API, see:

String dataMed = "2017-09-26 11:55:44.0"; // p.getInicioPesagem();
//String dataFixa = "Mon Oct 09 06:00:20 BRT 2017";

DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.0");
//DateTimeFormatter format2 = DateTimeFormatter.ofPattern("E MMM dd HH:mm:ss z yyyy");

LocalDateTime dataMed2 = LocalDateTime.parse(dataMed, format);
//LocalDateTime dataFixa2 = LocalDateTime.parse(dataFixa, format2);

// dataMed2.getHour() = 11    
if(dataMed2.getHour() < 6){
    System.out.println("Antes das 6 horas");
    dataMed = dataMed2.minusDays(1);
}
// Pra que o else se após as 6 não faz nada? Além disso você pega
// p.getInicioPesagem() de novo, entrando no else dataMed não foi
// alterado, logo a informação está redundante.

p.setDataMed(dataMed2.toLocalDate());

I left it commented if you want to use the comparison of two dates, or the date comes in the other format. Just convert p.getInitialize() to String.

See working on ideone.

  • 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,

Browser other questions tagged

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