JAVA自定义日期选择器

下载jar地址,

https://toedter.com/jcalendar/ jar包下载地址

依赖包如下图所示:

整个项目代码已经上传到CSDN

https://download.csdn.net/download/qq_30273575/89241601?ydreferer=aHR0cHM6Ly9tcC5jc2RuLm5ldC9tcF9kb3dubG9hZC9tYW5hZ2UvZG93bmxvYWQvVXBEZXRhaWxlZA%3D%3D

package utils;

import javax.swing.*;
import com.toedter.calendar.JDateChooser;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

public class GoSaveMain extends JFrame implements ActionListener{
    
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    //https://www.cnblogs.com/lihello/p/12795948.html 下载使用显示日期的jar包
    // https://toedter.com/jcalendar/ jar包下载地址
    
    //其他参考 https://blog.csdn.net/u012426327/article/details/77712882
    
    
    private JLabel date1Lable;
    private JLabel date2Lable;
    private JLabel amountLable;
    private JLabel interestRateLable;
    private JLabel reslutLable;

    
    private JTextField amountField;
    private JTextField interestRateField;
    private JTextField resultField;
    private JDateChooser dateChooser;
    private JDateChooser dateChooser2;
    
    private JButton runButton;
    private JCheckBox simulateBox;
    
    public GoSaveMain() {
        // 创建JDateChooser组件
        dateChooser = new JDateChooser();
        dateChooser2 = new JDateChooser();
        // 设置JDateChooser的宽度
        dateChooser.setPreferredSize(new Dimension(100, dateChooser.getPreferredSize().height));
        dateChooser2.setPreferredSize(new Dimension(100, dateChooser.getPreferredSize().height));
        
        JPanel date_panel = new JPanel();
        date_panel.setLayout(new GridLayout(6,2,1,1)) ;
        
        date1Lable = new JLabel("StartDate");
        date2Lable = new JLabel("EndDate");
        amountLable = new JLabel("Amount");
        reslutLable = new JLabel("Result");
        amountField=new JTextField("") ;
        interestRateLable = new JLabel("Rate");
        interestRateField=new JTextField("") ;
        resultField=new JTextField("") ;
        runButton = new JButton("Calculate") ;
        simulateBox = new JCheckBox("模拟时间8点前");
        date_panel.add(date1Lable);
        date_panel.add(dateChooser);
        date_panel.add(date2Lable);
        date_panel.add(dateChooser2);
        date_panel.add(amountLable);
        date_panel.add(amountField);
        date_panel.add(interestRateLable);
        date_panel.add(interestRateField);
        date_panel.add(simulateBox);
        date_panel.add(runButton);
        date_panel.add(reslutLable);
        date_panel.add(resultField);
        runButton.addActionListener(this);
        
        // 创建一个FlowLayout布局
        this.setLayout(new FlowLayout());
 
        // 将JDateChooser添加到JFrame
        this.add(date_panel);
 
        // 设置JFrame的其他属性
        this.setTitle("计算Gosave利息");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(300, 300);
        this.setLocationRelativeTo(null);
        this.setVisible(true);
        
        this.addWindowListener(new WindowAdapter() {

            @Override
            public void windowClosing(WindowEvent e) {
                int flag = JOptionPane.showConfirmDialog(null, "要退出该程序吗?","友情提示",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);
                if(flag==JOptionPane.YES_OPTION) {
                    System.exit(0);
                }else {
                    return;
                }
            }
        });
    }
 
    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            new GoSaveMain();
        });
    }


    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        if(e.getSource()==runButton){
            System.out.println("\nStart to Calculate interest!!!");
            String  amount = this.amountField.getText();
            String interestRate = this.interestRateField.getText();
            Date  dateChooser= this.dateChooser.getDate();
            Date  dateChooser2= this.dateChooser2.getDate();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String startDate = sdf.format(dateChooser);
            String endDate = sdf.format(dateChooser2);
            System.out.println(amount);
            System.out.println(interestRate);
            System.out.println(startDate);
            System.out.println(endDate);
            
            String checkBoxValue = "";
            if(simulateBox.isSelected()) {
                checkBoxValue = "true";
            }else {
                checkBoxValue = "false";
            }
            
            HashMap<String, String> map = new HashMap<String, String>();
            map.put("startDate", startDate);
            map.put("endDate", Common.getRealEndDate(endDate, getMinusDays(checkBoxValue)));
            map.put("amount", amount);
            map.put("interestRate", interestRate);
            map.put("checkBoxValue", checkBoxValue);
            
            
            CalculateUtils calculateUtils = new CalculateUtils(map);
            double value = calculateUtils.getResult();
            this.resultField.setText(""+value);
        }
    }
    
    public int getMinusDays(String checkBoxValue) {
        int minus_days =0;
        if(Common.getHoursThisTime()<8) {
            minus_days =1;
        }
        if(checkBoxValue.contains("true")){
            minus_days =1;
        }
        System.out.println("minus_days: "+minus_days);
        return minus_days;
    }
    

}
 

  • 9
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是一个简单的自定义日期选择器的示例代码: 首先,创建一个名为`DatePickerDialogFragment`的新类,继承自`DialogFragment`。这个类将用于显示日期选择器对话框。 ```java import android.app.DatePickerDialog; import android.app.Dialog; import android.os.Bundle; import android.widget.DatePicker; import androidx.annotation.NonNull; import androidx.fragment.app.DialogFragment; import java.util.Calendar; public class DatePickerDialogFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener { @NonNull @Override public Dialog onCreateDialog(Bundle savedInstanceState) { // 获取当前日期 final Calendar calendar = Calendar.getInstance(); int year = calendar.get(Calendar.YEAR); int month = calendar.get(Calendar.MONTH); int day = calendar.get(Calendar.DAY_OF_MONTH); // 创建并返回一个 DatePickerDialog 实例 return new DatePickerDialog(getActivity(), this, year, month, day); } @Override public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) { // 在此处处理选择的日期,你可以将其传递给其他组件或执行任何其他操作 // 例如,你可以在一个 TextView 中显示选择的日期 String selectedDate = String.format("%d-%02d-%02d", year, month + 1, dayOfMonth); // textView.setText(selectedDate); } } ``` 然后,在你的活动或片段中调用`DatePickerDialogFragment`来显示日期选择器对话框。 ```java public class MainActivity extends AppCompatActivity { // ... private void showDatePickerDialog() { DialogFragment newFragment = new DatePickerDialogFragment(); newFragment.show(getSupportFragmentManager(), "datePicker"); } // ... } ``` 当用户选择日期后,`onDateSet`方法将被调用,并传递选择的年、月和日。你可以在此方法中进行任何你想要的后续操作,例如更新文本视图中的日期。 请注意,以上代码只是一个简单的示例,你可以根据自己的需求进行修改和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值