「java整点报时」java获取整点时间

博主:adminadmin 2023-03-20 17:14:05 481

本篇文章给大家谈谈java整点报时,以及java获取整点时间对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

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 遇到几个难题麻烦各位大虾帮小弟解答。。。

第一题:T.java

public class T {

static boolean stop,wait;

static int c = 0;

static long time;

public static void main(String[] args) {

final Thread a = new Thread(){

public void run(){

while(!stop){

try {

sleep(1000);

} catch (InterruptedException e) {}

if(!wait)

System.out.println(new java.util.Date());

}

}

};

Thread b = new Thread(){

public void run(){

while(!stop){

if(System.currentTimeMillis()/1000%3600/60==0

System.currentTimeMillis()-time60*60*1000){

wait=true;

System.out.println("整点报时: "+new java.util.Date());

++c;

if(c==2)

time=System.currentTimeMillis()-2000;

}

else if(wait){

wait=false;

c=0;

}

try {

sleep(1000);

} catch (InterruptedException e) {}

}

}

};

b.start();

a.start();

}

}

第二题:App.java

import java.applet.Applet;

import java.awt.Color;

import java.awt.Font;

import java.awt.Label;

public class App extends Applet implements Runnable{

Label l = new Label("欢迎光临");

public void init(){

this.setSize(400,80);

this.setLayout(null);

l.setFont(new Font("kaiti_gb2312",Font.BOLD,48));

l.setForeground(Color.red);

l.setSize(200,80);

add(l);

new Thread(this).start();

}

public void run() {

boolean toR=true;

while(true){

if(toR){

l.setLocation(l.getX()+1,l.getY());

}

else{

l.setLocation(l.getX()-1,l.getY());

}

if(l.getX()+l.getWidth()==getWidth())

toR=false;

else if(l.getX()==0)

toR=true;

try {

Thread.sleep(5);

} catch (InterruptedException e) {}

Thread.yield();

}

}

}

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获取整点时间、java整点报时的信息别忘了在本站进行查找喔。