java定时任务每天多个时间点怎么办

java定时任务每天多个时间点怎么办,第1张

1、创建ScheduledExecutorService对象,用于执行定时任务

2、使用scheduleAtFixedRate()方法来设置定时任务,该方法可以指定任务的初始举斗延迟时间、任务运行周期等参数。

3、在定时任务的run()方法中编写任务代码,实现需要执行的业务逻辑。

4、在程序启动时,调用锋或ScheduledExecutorService对象的shutdown()方法来关闭定时任务。正基磨

public static void main(String[] args) {

long begain = System.currentTimeMillis()//开始系统时间

try {

Thread.sleep(100)

} catch (Exception e) {

e.printStackTrace()

}

long CheckTime = System.currentTimeMillis()//判断时间野誉

while(true){

System.out.println(CheckTime-begain)

try {

Thread.sleep(1000)

} catch (Exception e) {

e.printStackTrace()

}

CheckTime = System.currentTimeMillis()

if((CheckTime-begain)>=(30*1000)){//判断时候到30秒

System.out.println("30秒后结束")

break

}

}

}

给你的答案希望你迅盯能颂昌段满意

以下是给你修改过的 ,还加了尺慧一个取消功能,看看。

import java.awt.*

import java.awt.event.*

import java.awt.geom.*

import java.util.*

import javax.swing.*

public class ClockFrame extends JFrame {

private JComboBox hourBox, minuteBox, secondBox

private int hour, minute, second, totalSeconds, currentSeconds

private long argue

private GregorianCalendar calendar

private boolean change = true

private static final int WIDTH = 200

private static final int HEIGHT = 150

public ClockFrame() {

setTitle("关机定时陵正答")

setSize(200, 150)

Container contentPanel = getContentPane()

JPanel timePanel = new JPanel()

timePanel.setLayout(new GridLayout(4, 2))

JLabel minuteLable = new JLabel("设置分钟")

timePanel.add(minuteLable)

minuteBox = new JComboBox()

timePanel.add(minuteBox)

for (int i = 0i <60i++) {

minuteBox.addItem(i)

}

minuteBox.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent evt) {

minute = ((Integer) minuteBox.getSelectedItem()).intValue()

}

})

JLabel secondLable = new JLabel("设清粗置秒钟")

timePanel.add(secondLable)

secondBox = new JComboBox()

timePanel.add(secondBox)

for (int i = 0i <60i++) {

secondBox.addItem(i)

}

secondBox.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent evt) {

second = ((Integer) secondBox.getSelectedItem()).intValue()

}

})

contentPanel.add(timePanel, BorderLayout.CENTER)

JButton check = new JButton("确定")

contentPanel.add(check, BorderLayout.SOUTH)

check.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent evt) {

JButton check=(JButton) evt.getSource()

if (check.getText().equals("确定")) {

calendar = new GregorianCalendar()

int currentSeconds = calendar.get(Calendar.HOUR_OF_DAY)

* 3600 + calendar.get(Calendar.MINUTE) * 60

+ calendar.get(Calendar.SECOND)

totalSeconds = hour * 3600 + minute * 60 + second

if (totalSeconds - currentSeconds >= 0) {

argue = (totalSeconds - currentSeconds) * 1000

JOptionPane.showMessageDialog(ClockFrame.this,

"您设置的时间为 " + hour + ":" + minute + ":" + second

+ "\n程序将在后台运行,并在此时自动关闭计算机!", "设置成功",

JOptionPane.INFORMATION_MESSAGE)

hideFrame()

}

try {

// Thread.sleep(argue)//这句没用

Runtime.getRuntime().exec(

"shutdown.exe -s -c \"我要关机了噢!不好意思!\" -t "

+ totalSeconds)

check.setText("取消")

} catch (Exception e) {

e.printStackTrace()

}

}else{

try {

Runtime.getRuntime().exec("shutdown.exe -a")

check.setText("确定")

} catch (Exception e) {

e.printStackTrace()

}

}

}

})

}

private void hideFrame() {

this.setVisible(false)

}

public static void main(String[] args) {

JFrame frame = new ClockFrame()

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)

frame.setLocationByPlatform(true)

frame.show()

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/12396188.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-25
下一篇 2023-05-25

发表评论

登录后才能评论

评论列表(0条)

保存