導航:首頁 > 編程語言 > java動態時間顯示時間

java動態時間顯示時間

發布時間:2022-11-03 05:45:09

java中如何設置時間和顯示時間

JAVA中獲取當前系統時間2011-07-06 20:45 並格式化輸出:
import java.util.Date;
import java.text.SimpleDateFormat;

public class NowString {
public static void main(String[] args) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//設置日期格式
System.out.println(df.format(new Date()));// new Date()為獲取當前系統時間
}
}

設置時間,推薦 使用java.util.Calendar類來進行操作,
import java.util.Date;
import java.util.Calendar;

import java.text.SimpleDateFormat;

public class TestDate{
public static void main(String[] args){
Date now = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");//可以方便地修改日期格式

String hehe = dateFormat.format( now );
System.out.println(hehe);

Calendar c = Calendar.getInstance();//可以對每個時間域單獨修改

int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int date = c.get(Calendar.DATE);
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
int second = c.get(Calendar.SECOND);
System.out.println(year + "/" + month + "/" + date + " " +hour + ":" +minute + ":" + second);
}
}

❷ 這個java窗體怎麼顯示動態時間

你是想實時更新狀態欄上的時間?

可以用定時器自動去更新
import javax.swing.*;
import java.awt.*;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

public class Clock extends JFrame {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new Clock().setVisible(true);
}
});

}

private JLabel timeLabel;

public Clock() {
timeLabel = new JLabel(String.valueOf(new Date()), JLabel.CENTER);
this.getContentPane().add(timeLabel, BorderLayout.CENTER);
this.setBounds(0, 0, 300, 100);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
new Timer("clock").schele(new TimerTask() {
@Override
public void run() {
timeLabel.setText(String.valueOf(new Date()));
}
}, 0, 1000);
}
}

❸ java動態時間代碼

你應該是想把時間顯示在jl01的控制項上。但是你這樣只能顯示初始化時的時間。

每隔1s更新時間,簡單一點可以用循環,然後每次循環設置1000ms的睡眠時間,這個同時需要用多線程實現。也可以用timer做定時任務。推薦後者

//importjava.util.Timer
//importjava.util.TimerTask
Timertimer=newTimer();
TimerTasktask=newTimerTask(){
@Override
publicvoidrun(){
Datedate=newDate();
Stringstring=newSimpleDateFormat("yyyy-MM-ddHH:mm:ss").format(date);
jl01.setText(string);
}
};
//立即開始任務,任務間隔1000ms。schele和scheleAtFixedRate的區別自行搜索
timer.scheleAtFixedRate(task,0,1000);

❹ java獲取動態時間

import java.awt.Dimension;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
/**
* This class is a simple JFrame implementation to explain how to
* display time dynamically on the JSwing-based interface.
* @author Edison
*
*/
public class TimeFrame extends JFrame
{
/*
* Variables
*/
private JPanel timePanel;
private JLabel timeLabel;
private JLabel displayArea;
private String DEFAULT_TIME_FORMAT = "HH:mm:ss";
private String time;
private int ONE_SECOND = 1000;

public TimeFrame()
{
timePanel = new JPanel();
timeLabel = new JLabel("CurrentTime: ");
displayArea = new JLabel();

configTimeArea();

timePanel.add(timeLabel);
timePanel.add(displayArea);
this.add(timePanel);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setSize(new Dimension(200,70));
this.setLocationRelativeTo(null);
}

/**
* This method creates a timer task to update the time per second
*/
private void configTimeArea() {
Timer tmr = new Timer();
tmr.scheleAtFixedRate(new JLabelTimerTask(),new Date(), ONE_SECOND);
}

/**
* Timer task to update the time display area
*
*/
protected class JLabelTimerTask extends TimerTask{
SimpleDateFormat dateFormatter = new SimpleDateFormat(DEFAULT_TIME_FORMAT);
@Override
public void run() {
time = dateFormatter.format(Calendar.getInstance().getTime());
displayArea.setText(time);
}
}

public static void main(String arg[])
{
TimeFrame timeFrame=new TimeFrame();
timeFrame.setVisible(true);
}
}

❺ Java中如何實現顯示動態的時間

利用死循環和線程,讓線程在循環中每sleep1秒,重新獲取下系統時間不就是動態顯示時間了嗎
while(true){
Date date=new Date(System.currentTimeMillis());
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(date);
//每一秒刷新下時間
try {
Thread.sleep(1000);//sleep是以ms為單位
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
你可以試下代碼,看看是不是你要的效果

❻ JAVA中JLabel動態顯示時間的問題。

初始化你的JLabel
,實例化new
就可以了。
同時你的程序有個比較嚴重的問題,那就是線程是否安全,swing不是線程安全的,你在一個新開的線程中更新GUI可能會報錯,雖然這樣的可能性很低,但是如果你要嚴謹的話,在你的線程中添加如下代碼
SwingUtilities.invokeAndWait(new
Runnable()
{
public
void
run()
{
displayArea.setText(dateFormatter.format(Calendar.getInstance().getTime()));
}
});

❼ 如何用JAVA編程「在網頁中顯示動態時間」

index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<script type="text/javascript">

var xmlHttp;

function creatRequest()
{
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{

// Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{

try
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
alert("您的瀏覽器不支持AJAX!");
return false;
}
}
}
}

function startRequest()
{
creatRequest();
xmlHttp.open("GET","time.jsp",true);
xmlHttp.onreadystatechange = handleRequest;
xmlHttp.send(null);
}

function handleRequest()
{
if(xmlHttp.readyState==4)
{
if(xmlHttp.status==200)
{
document.getElementById("res").innerHTML = xmlHttp.responseText;
}
}
}

window.setInterval("startRequest()",1000);

</script>
</head>
<body>
<div id="res"></div>
</body>
</html>

time.jsp

<%@ page contentType="text/html; charset=utf-8" %>
<%@ page import="java.util.Date"%>
<%@ page import="java.text.SimpleDateFormat"%>

<%
response.setContentType("text/xml; charset=UTF-8");
response.setHeader("Cache-Control","no-cache");
String str = request.getSession().getServletContext().getRealPath(request.getRequestURI());
Date d = new Date();
String sf = "北京時間:H:mm:ss";
SimpleDateFormat sdf = new SimpleDateFormat(sf);
out.println(sdf.format(d.getTime()));
//out.println(d.getSeconds());
out.close();
%>

你試試吧,應該沒問題的

❽ JAVA怎麼在JAVA Swing 界面上顯示動態的當前時間~~~~

package com.exam.student.reserve;

import java.awt.*;
import javax.swing.*;
import java.util.*;
public class Time extends JFrame
{
JPanel pnlmain;
static JLabel lblmove;
JButton bntcontrol;
currenttime ct;
public Time()
{

pnlmain=new JPanel();
this.setContentPane(pnlmain);
lblmove=new JLabel("你好");
lblmove.setFont(new Font("宋體",Font.BOLD,22));
lblmove.setForeground(Color.RED);
pnlmain.add(lblmove);
ct=new currenttime();
ct.start();
setTitle("線程");
setSize(250,150);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setResizable(false);
}

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

}
class currenttime extends Thread
{
Date datedisplay;
GregorianCalendar gccalendar;
String strtime;
public currenttime(){}
public void run()
{
while(true)
{
displaytime();
try
{
this.sleep(1000);
}
catch(InterruptedException e)
{
JOptionPane.showMessageDialog(null,"線程中斷");
}
}
}
public void displaytime()
{
datedisplay=new Date();
gccalendar=new GregorianCalendar();
gccalendar.setTime(datedisplay);
strtime="當前時間:"+gccalendar.get(Calendar.DATE)+":"+gccalendar.get(Calendar.HOUR)+":"+gccalendar.get(Calendar.MINUTE)+":"+gccalendar.get(Calendar.SECOND);
Time.lblmove.setText(strtime);
}
}

❾ 怎麼使用Java將時間動態的顯示在網頁中

一段js實現吧:

JScriptcode
很簡單的一個功能函數,用Date()對象獲取到當前時間,然後用setTimeout每隔1秒獲取最新的時間.
寫的過程中碰到過一個小小的問題:我最初的想法是用setInterval()每隔1秒獲取最新時間,可是可以,但setInterval如果放在主函數內部,但導致內存泄漏(至於原因,暫時還沒想明白),後來在Rocky的提醒下用setTimeout()才解決內存泄漏問題
functionnowTime(ev,type){
/*
*ev:顯示時間的元素
*type:時間顯示模式.若傳入12則為12小時制,不傳入則為24小時制
*/
//年月日時分秒
varY,M,D,W,H,I,S;
//月日時分秒為單位時前面補零
functionfillZero(v){
if(v<10){v='0'+v;}
returnv;
}
(function(){
vard=newDate();
varWeek=['星期天','星期一','星期二','星期三','星期四','星期五','星期六'];
Y=d.getFullYear();
M=fillZero(d.getMonth()+1);
D=fillZero(d.getDate());
W=Week[d.getDay()];
H=fillZero(d.getHours());
I=fillZero(d.getMinutes());
S=fillZero(d.getSeconds());
//12小時制顯示模式
if(type&&type==12){
//若要顯示更多時間類型諸如中午凌晨可在下面添加判斷
if(H<=12){
H='上午'+H;
}elseif(H>12&&H<24){
H-=12;
H='下午'+fillZero(H);
}elseif(H==24){
H='下午00';
}
}
ev.innerHTML=Y+'年'+M+'月'+D+'日'+''+W+''+H+':'+I+':'+S;
//每秒更新時間
setTimeout(arguments.callee,1000);
})();

❿ 在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動態時間顯示時間相關的資料

熱點內容
五菱宏光空調壓縮機 瀏覽:64
為什麼app佔用幾百兆 瀏覽:676
自動解壓失敗叫我聯系客服 瀏覽:482
易語言新手源碼 瀏覽:456
oa伺服器必須有固定ip地址 瀏覽:42
傳奇源碼分析是什麼 瀏覽:267
解放壓縮機支架 瀏覽:255
程序員禿頂搞笑相遇 瀏覽:6
IBM手機app商店叫什麼名字 瀏覽:834
jpeg壓縮質量 瀏覽:774
雲伺服器評測對比 瀏覽:145
java日期轉string 瀏覽:221
openfire源碼編譯 瀏覽:897
在線小工具箱引流網站源碼 瀏覽:337
非科班程序員自學 瀏覽:801
壓縮泡沫鞋底底材 瀏覽:219
程序員職場第一課2正確的溝通 瀏覽:679
遇到不合法app應該怎麼辦 瀏覽:91
匯編程序編譯後的文件 瀏覽:81
大智慧均線源碼 瀏覽:374