當前位置:首頁 » 編程語言 » 顯示時間java

顯示時間java

發布時間: 2022-12-25 17:16:07

java窗口顯示當前時間

用 java的 swing做個圖形界面 然後顯示當前的時間:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.JTextField;
import javax.swing.Timer;
import javax.swing.JFrame;

public class NowTime extends JFrame
{
//添加 顯示時間的JTextField
private void addComponent(){
JTextField time = new JTextField();
this.getContentPane().add(time);
this.setTimer(time);
}
//顯示窗口
private void showTime(){
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//this.pack();//自動適應窗口大小
this.setSize(160, 80);
this.setVisible(true);
}

//設置Timer 1000ms實現一次動作 實際是一個線程
private void setTimer(JTextField time){
final JTextField varTime = time;
Timer timeAction = new Timer(1000, new ActionListener() {

public void actionPerformed(ActionEvent e) {
long timemillis = System.currentTimeMillis();
//轉換日期顯示格式
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
varTime.setText(df.format(new Date(timemillis)));
}
});
timeAction.start();
}

//運行方法
public static void main(String[] args) {
NowTime timeFrame = new NowTime();
timeFrame.addComponent();
timeFrame.showTime();
}
}

❷ 在java中,要如何在一個文本框動態顯示時間

<html>
<head>
<title>時鍾特效</title>
<script type="text/javascript">
function disptime(){
var today = new Date(); //獲得當前時間
var hh = today.getHours(); //獲得小時、分鍾、秒
var mm = today.getMinutes();
var ss = today.getSeconds();
/*設置div的內容為當前時間*/
document.getElementById("myclock").innerHTML="<h1>現在是:"+hh+":"+mm+":"+ss+"<h1>";
document.getElementById("myClock1").value=hh+":"+mm+":"+ss;
/*
使用setTimeout在函數disptime()體內再次調用setTimeout
設置定時器每隔1秒(1000毫秒),調用函數disptime()執行,刷新時鍾顯示
*/
var myTime=setTimeout("disptime()",1000);
}

</script>
</head>

<body onload="disptime()">
<div id="myclock"></div>
<input type="text" id="myClock1" value=""></input>
</body>
</html>

❸ java程序如何實現時間的顯示

你可以利用這個
timer.scheleAtFixedRate(new sysTimeRefresher(), 0, 1000);<---每秒讀一次(可以自己改)

class SysTimeRefresher extends TimerTask {
@Override
synchronized public void run() {
Calendar.getInstance().getTime()<----這個是抓取現在系統時間隔式請自己看要哪一種

這里可以實做顯示時間

timer.cancel();<----這是實作16:30時間到中只運行

}}

❹ java如何在一個文本框動態顯示時間

packagecn.test;

importjava.text.SimpleDateFormat;
importjava.util.Date;
importjava.util.Timer;

importjavax.swing.JFrame;
importjavax.swing.JTextField;

publicclassTimeextendsJFrame{
privateJTextFieldTimeBox=newJTextField();

privateTimertimer=newTimer();
privateSimpleDateFormatsdf=newSimpleDateFormat("HH:mm:ss");
publicTime(){
TimeBox.setBounds(0,0,100,50);

add(TimeBox);

//方法1定時器方法
//TimerTasktask=newTimerTask(){
//@Override
//publicvoidrun(){
//TimeBox.setText(sdf.format(newDate()));
//}
//};
//timer.schele(task,1000,1000);


//方法2線程休眠方法
Displaytimedisplay=newDisplay();
timedisplay.start();
}{
SimpleDateFormatsdf=newSimpleDateFormat("HH:mm:ss");
publicDisplay(){
System.out.println("display");
}

@Override
publicvoidrun(){
while(true){
System.out.println("Threadisrunning...");
TimeBox.setText(sdf.format(newDate()));
try{
Thread.sleep(1000);
}catch(InterruptedExceptione){
System.out.println("sleeperror!!");
e.printStackTrace();
}
}
}
}

publicstaticvoidmain(String[]args){
Timet=newTime();
t.setSize(300,200);
t.setResizable(false);
t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
t.setVisible(true);

}
}

❺ java 動態顯示時間

javax.swing.Timer ,很容易的,一個事件,在事件裡面顯示你的時間就可以了。

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.Timer;

public class TimerShow extends JFrame {

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
new TimerShow();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

private JLabel lblNewLabel;

public TimerShow() {
super("time");
setBounds(100, 100, 450, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setLayout(null);
lblNewLabel = new JLabel(" 簡單時鍾 ", JLabel.CENTER);
lblNewLabel.setBounds(0, (300-35-35)/2, 440, 35);
getContentPane().add(lblNewLabel);
setVisible(true);

new Timer(delay, taskPerformer).start();
}

int delay = 1000; //milliseconds
ActionListener taskPerformer = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
String s=String.format("%tY-%<tm-%<td %<tH:%<tM:%<tS",new Date());
lblNewLabel.setText(s);
}
};
}

❻ Java顯示當前日期時間

介紹2個對象你用用吧。

public static void main(String[] args) {
//日期格式轉換
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String datetime = sdf.format(new Date());
System.out.println(datetime);

//Calendar 有N多關於日期的操作。你可以看看幫助文檔。
Calendar c = Calendar.getInstance();
System.out.println(c.getTime());
}

❼ Java 如何顯示當前系統日期與時間

通過new Date獲取當前的日期與時間

示例:

publicstaticvoidmain(String[]args){
Datenow=newDate();//獲取當前時間
SimpleDateFormatdateFormat=newSimpleDateFormat("yyyy/MM/ddHH:mm:ss");//格式化當前日期時間,顯示如2015/06/2714:22:22
}

❽ Java時間顯示

請參考

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
import java.text.SimpleDateFormat;
class MyJDialog extends JDialog{
SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public MyJDialog(){
Date date=new Date();
String now=fmt.format(date);
setTitle("當前時間");
Container c=getContentPane();
JLabel jl=new JLabel("點前的系統時間是:"+now);
jl.setHorizontalAlignment(SwingConstants.CENTER);
c.add(jl);
setBounds(140,140,400,200);
}
}
public class JDialogDemo extends JFrame{
public JDialogDemo(){
setTitle("系統時間顯示軟體");
Container c=getContentPane();
setLayout(null);
JButton jb=new JButton("系統時間");
jb.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
new MyJDialog().setVisible(true);
}
});
jb.setBounds(50,50,180,50);
c.add(jb);
setBounds(100,100,300,200);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}

public static void main(String[] args) {
new JDialogDemo();
}

}

❾ 一段顯示當下時間的JAVA代碼

privateShaperect;//背景矩形
privateFontfont;//設置字體
privateDatedate;//現在的時間
privateThreadtime;//時間線程
privateCanvasPanelcanvas;

publicstaticvoidmain(String[]args){
newTimerTest20140930();
}
publicTimerTest20140930(){
super("繪制文本");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400,300);
rect=newRectangle2D.Double(10,10,200,100);
font=newFont("宋體",Font.BOLD,16);
canvas=newCanvasPanel();
add(canvas);
time=newThread(newRunnable(){
publicvoidrun(){
while(true){
canvas.repaint();
try{
Thread.sleep(1000);
}catch(Exceptionex){
}
}
}
});
time.start();
setLocationRelativeTo(null);
setVisible(true);
}
classCanvasPanelextendsCanvas{
publicvoidpaint(Graphicsg){
super.paint(g);
Graphics2Dg2=(Graphics2D)g;
g2.setColor(Color.BLACK);
g2.fill(rect);
g2.setColor(Color.BLUE);
g2.setFont(font);
g2.drawString("現在的時間是",20,30);
date=newDate();
g2.drawString(String.format("%tr",date),50,60);
}
}

❿ java 編程中顯示日期和時間的代碼

可以直接通過jdk基本方法,獲取到當前的時間
Date date= new Date();//創建一個時間對象,獲取到當前的時間
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//設置時間顯示格式
String str = sdf.format(date);//將當前時間格式化為需要的類型
System.out.println(str);//輸出結果
結果為:2015-11-06 13:53:54(實時)。

熱點內容
編程包游戲 發布:2024-05-05 20:25:00 瀏覽:607
系統鎖屏忘記密碼如何設置 發布:2024-05-05 20:18:07 瀏覽:759
xp怎樣訪問win7 發布:2024-05-05 20:17:07 瀏覽:870
c語言訪問http 發布:2024-05-05 20:04:14 瀏覽:874
什麼可以配置波爾多葉 發布:2024-05-05 20:00:32 瀏覽:964
cgxrar解壓密碼 發布:2024-05-05 19:47:24 瀏覽:634
ubuntu編譯linux內核 發布:2024-05-05 19:46:05 瀏覽:8
php靜態方法調用對象 發布:2024-05-05 19:24:30 瀏覽:367
電腦LNS伺服器地址 發布:2024-05-05 19:22:15 瀏覽:377
不屬於編譯程序組成的部分是什麼 發布:2024-05-05 19:05:34 瀏覽:614