Java, Python, Database, Flutter, Matlap, Micorcontroller, Tutorials, Swing Framework

Hancie e-Learning Studio

Learn Java, Learn HTML, CSS, PHP, Javascript, Python Tutorials || Download program source codes || Java Project and Source code available here || All types error troubleshooting tips available here

How to calculate the difference between two JDateChooser value in Java Swing?

 


GUI
package Trial;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import com.toedter.calendar.JDateChooser;

class Demo {

	JButton jb1;
	JDateChooser c1,c2;
	JTextField noofdaystxt;
	JButton btn;

	Demo() {
		JFrame frame = new JFrame();
		frame.setTitle("Hancie e-Learning Studio");
		frame.setLayout(null);
		frame.setSize(600, 500);
		frame.setLocationRelativeTo(null);

		c1 = new JDateChooser();
		c1.setDateFormatString("yyyy-MM-dd");
		c1.setBounds(160, 10, 210, 35);
		c1.setBorder(BorderFactory.createLineBorder(Color.white, 1));
		c1.setFont(new Font("Verdana", Font.PLAIN, 18));
		frame.add(c1);
		
				
		c2 = new JDateChooser();
		c2.setDateFormatString("yyyy-MM-dd");
		c2.setBounds(160, 60, 210, 35);
		c2.setBorder(BorderFactory.createLineBorder(Color.white, 1));
		c2.setFont(new Font("Verdana", Font.PLAIN, 18));
		frame.add(c2);
		
		noofdaystxt = new JTextField();
		noofdaystxt.setBounds(160, 140, 210, 35);
		noofdaystxt.setFont(new Font("Verdana", Font.BOLD, 18));
		frame.add(noofdaystxt);
		
		btn = new JButton("Calculate");
		btn.setBounds(160, 200, 210, 35);
		btn.setBorder(BorderFactory.createLineBorder(Color.white, 1));
		btn.setFont(new Font("Verdana", Font.BOLD, 18));
		frame.add(btn);
		btn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				
				try {
					
					String fromdate = ((JTextField) c1.getDateEditor().getUiComponent()).getText();
					String todate = ((JTextField) c2.getDateEditor().getUiComponent()).getText();

					LocalDate fday = LocalDate.parse(fromdate);
					LocalDate tday = LocalDate.parse(todate);
					
					Long day_gap = ChronoUnit.DAYS.between(fday, tday);
					noofdaystxt.setText(String.valueOf(day_gap));
					
				}
				
				catch(Exception ex) {
					System.out.println("Error"+ex.getMessage());
				}
			}
			
			
			
		});
		
		
		
		
		frame.setVisible(true);

	}

	public static void main(String args[]) {
		new Demo();
	}
}
Output

Welcome all to Hancie e-learning studio
Friends,
I have brought an e-learning platform for you from where you can teach internet, website, programming language, error troubleshooting, etc. Blogger, WordPress templates, themes are available for free on this website, which are not charged for downloading, so that you can earn money by blogging using such templates and this is not a difficult task and this You can do it now. You keep uploading new posts by us and keep taking advantage of this website. The aim of which is to spread the knowledge related to internet, career, web designing and technology to the people and contribute to the development of the country.