用 java的 swing做個圖形界面 然后顯示當前的時間:
10年積累的成都網(wǎng)站建設(shè)、網(wǎng)站設(shè)計經(jīng)驗,可以快速應(yīng)對客戶對網(wǎng)站的新想法和需求。提供各種問題對應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認識你,你也不認識我。但先網(wǎng)站設(shè)計制作后付款的網(wǎng)站建設(shè)流程,更有永興免費網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。
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();//自動適應(yīng)窗口大小
this.setSize(160, 80);
this.setVisible(true);
}
//設(shè)置Timer 1000ms實現(xiàn)一次動作 實際是一個線程
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();
//轉(zhuǎn)換日期顯示格式
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中獲取當前系統(tǒng)時間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");//設(shè)置日期格式
System.out.println(df.format(new Date()));// new Date()為獲取當前系統(tǒng)時間
}
}
設(shè)置時間,推薦 使用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);
}
}
通過new Date獲取當前的日期與時間
示例:
public?static?void?main(String[]?args){?
Date?now?=?new?Date();?//獲取當前時間
SimpleDateFormat?dateFormat?=?new?SimpleDateFormat("yyyy/MM/dd?HH:mm:ss");//格式化當前日期時間,顯示如2015/06/27?14:22:22
}
private?Shape?rect;????????????//背景矩形
private?Font?font;????????????//設(shè)置字體
private?Date?date;????????????//現(xiàn)在的時間
private?Thread?time;????????//時間線程
private?CanvasPanel?canvas;
public?static?void?main(String[]?args)?{
new?TimerTest20140930();
}
public?TimerTest20140930(){
super("繪制文本");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400,300);
rect?=?new?Rectangle2D.Double(10,10,200,100);
font?=?new?Font("宋體",Font.BOLD,16);
canvas=new?CanvasPanel();
add(canvas);
time?=?new?Thread(new?Runnable(){
public?void?run(){
while(true){
canvas.repaint();
try{
Thread.sleep(1000);
}catch(Exception?ex){
}
}
}
});
time.start();
setLocationRelativeTo(null);
setVisible(true);
}??
class?CanvasPanel?extends?Canvas?{
public?void?paint(Graphics?g){
super.paint(g);
Graphics2D?g2?=?(Graphics2D)?g;
g2.setColor(Color.BLACK);
g2.fill(rect);
g2.setColor(Color.BLUE);
g2.setFont(font);
g2.drawString("現(xiàn)在的時間是",?20,?30);
date?=?new?Date();
g2.drawString(String.format("%tr",?date),?50,?60);???????
}
}