How to receive user date through JTextField, using JodaTime?

1

I'm not able to do user input. I want the program to make the difference in days between today's date and the date entered by the user. The program has more implementations but it's just that part that is not working.

private JTextField dataVencimento;
dataVencimento = new JTextField(20)
container.add(dataVencimento);

...

DateTime hoje = new DateTime();
DateTime dataVencimento = new DateTime();

Days daysBetween = Days.daysBetween(dataVencimento, hoje);

JOptionPane.showMessageDialog(null, "Dias de diferença: " + daysBetween, 
                     "Atraso", JOptionPane.INFORMATION_MESSAGE);
    
asked by anonymous 31.07.2016 / 07:36

2 answers

1

Try to make this account using LocalDate , parsing the string with DateTimeFormatter :

  String strDate = "30/08/2016";
  DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
  LocalDate dataVencimento = LocalDate.parse(strDate, formatter);

  LocalDate dataHoje = LocalDate.now();

  long daysBetween = ChronoUnit.DAYS.between(dataHoje, dataVencimento);

  System.out.println(daysBetween);

The code above, if executed today (07/31/2016), will display 30. If negative, it is because the second date is less than the first date passed in DAYS.between .

See working at ideone .

References :

Java SE 8 Date and Time (oracle)

p>

Calculate days between two dates in Java 8

    
31.07.2016 / 15:17
0

Thanks in advance, your response was helpful, although I have managed to solve it differently, it was just a matter of logic itself.

private JTextField dia;
private JTextField mes;
private JTextField ano;

container.add(new JLabel("Dia"));
container.add(dia);
container.add(new JLabel("Mês"));
container.add(mes);
container.add(new JLabel("Ano"));
container.add(ano);

...

int day = Integer.parseInt(dia.getText());
int month = Integer.parseInt(mes.getText());
int year = Integer.parseInt(ano.getText());

DateTime hoje = new DateTime();
DateTime dataVencimento = new DateTime(year, month, day, 0, 0);

Days diferenca = Days.daysBetween(dataVencimento, hoje);

What I did was create a variable for each part of the date, day, month, year, and convert it to an integer and put the variable within the DateTime dateVenu.

    
01.08.2016 / 03:26