Java设置到期时间


                        Calendar calendar = new GregorianCalendar();

                        Date date = new Date();
                        calendar.setTime(date);
                        if (draworder.getState() == 1) {
                            calendar.add(calendar.DAY_OF_MONTH, 1);
                        } else if (draworder.getState() == 2) {
                            calendar.add(calendar.DAY_OF_MONTH, 3);
                        } else {
                            calendar.add(calendar.YEAR, 1);
                        }
                        date = calendar.getTime();
                        customer.setExpiredate(date);

### 如何在 Java Swing 中实现定时器功能 在 Java Swing 应用程序中,`javax.swing.Timer` 是一种方便的方式来处理基于事件的时间调度任务。此组件允许开发者指定一段代码,在特定间隔内重复执行。 创建并启动一个简单的 `Timer` 需要定义两个主要部分:延迟时间和动作监听器。下面是一个基本的例子来展示如何使用它: ```java import javax.swing.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class SimpleSwingTimerExample { private static void createAndShowGUI() { // 创建窗口框架 JFrame frame = new JFrame("Simple Timer Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); final JLabel label = new JLabel("Time passed: 0 seconds", SwingConstants.CENTER); frame.getContentPane().add(label, BorderLayout.CENTER); int delay = 1000; // 每隔一秒触发一次 ActionListener taskPerformer = new ActionListener() { int counter = 0; public void actionPerformed(ActionEvent evt) { counter++; label.setText("Time passed: " + counter + " seconds"); } }; new Timer(delay, taskPerformer).start(); frame.pack(); frame.setVisible(true); } public static void main(String[] args) { // 调度到事件分派线程以确保安全更新UI组件 SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } } ``` 上述代码展示了怎样通过每秒增加计数器的方式显示经过了多少秒钟[^3]。这里的关键在于实例化了一个新的 `Timer` 对象,并传递给它一个实现了 `ActionListener` 接口的对象作为参数。每当定时器到期时就会调用该对象中的 `actionPerformed()` 方法。 对于更复杂的场景,比如倒计时应用,则可以在初始化的时候设定好总的持续时间,并且每次回调都减少剩余时间直到达到零为止[^4]。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值