❶ 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>