关于java整点活动的信息
本篇文章给大家谈谈java整点活动,以及对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
在JAVA中如何判断当前时间是不是整点
System.currentTimeMillis()产生一个当前的毫秒,这个毫秒其实就是自1970年1月1日0时起的毫秒数,用来计算当前毫秒与1970年1月1日之间的毫秒差。
那么我们就可以long time=System.currentTimeMillis();
然后换算一下小时,发现1小时等于3600s等于3600000ms。
那么我们把这个long类型的time进行一次取余运算。
int min=time%3600000;
那么只要min的值小于一分钟,即60000ms即可。
if(min60000)System.out.println("当前时间为整点.");
else println("非整点.");
整体简化代码如下:
if(System.currentTimeMillis()%360000060000)System.out.println("当前时间为整点.");
else println("非整点.");
JAVA 求一个简单的整点报时(声音)程序!!! 懂得说一下思路和相关包以及类!!!
1. 把歌曲名放到一个数组里面,在用一个标志位,用来查看声音是否结束。true-播放中,false-结束。如果声音结束播放下一曲
if(isplay == true){//什么都不用做
}
else if (isplay==false){
gequ[i+1].play();
}
大概思路是这样子的。。
2. 用循环线程,中间歇半秒
Thread timeThread = new Thread(){
@Override
public void run() {
while(true){
System.out.println(new Date());
try {
Thread.sleep(500); //休息半秒,不然cpu会100%
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
timeThread.run();
希望对你有帮助。。^^
java程序 时钟 整点报时代码
import java.awt.*;
import java.util.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.Timer;
public class Test extends JFrame implements ActionListener{
Timer t=new Timer(1000,this);
GregorianCalendar calendar=new GregorianCalendar();
JLabel label1=new JLabel(new Date().toString()),
label2=new JLabel("
");
public Test(){
super("TIME");
Container c=this.getContentPane();
c.setLayout(new FlowLayout());
c.add(label1);c.add(label2);
t.start();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setBounds(200,200,200,100);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e){
Date date=new Date();
calendar.setTime(date);
if(e.getSource()==t){
label1.setText(date.toString());
if(calendar.get(Calendar.MINUTE)==0)//当分钟为0报时
if(calendar.get(Calendar.SECOND)=5){//持续5秒报时
if(calendar.get(Calendar.SECOND)%2==0)
label2.setText("现在是北京时间"+calendar.get(Calendar.HOUR_OF_DAY)+"点整。");
if(calendar.get(Calendar.SECOND)%2==1)
label2.setText("
");
}
}
}
public static void main (String[] args) {
new Test();
}
}
java整点活动的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于、java整点活动的信息别忘了在本站进行查找喔。