import java.awt.*;

站在用戶的角度思考問題,與客戶深入溝通,找到桓仁網站設計與桓仁網站推廣的解決方案,憑借多年的經驗,讓設計與互聯網技術結合,創造個性化、用戶體驗好的作品,建站類型包括:成都做網站、網站建設、企業官網、英文網站、手機端網站、網站推廣、空間域名、網絡空間、企業郵箱。業務覆蓋桓仁地區。
import java.applet.Applet;
import java.util.Calendar;
import java.text.SimpleDateFormat;
import java.util.Date;
public class ClockApplet extends Applet implements Runnable //Applet支持線程
{
private Thread athread; //線程
private SimpleDateFormat sdateformat; //日期格式
public void init()
{
this.setBackground(Color.white);//背景顏色設為白色
this.athread = null;
}
public void paint(Graphics g)
{
this.sdateformat = new SimpleDateFormat("hh時mm分ss秒");
g.drawString(this.sdateformat.format(new Date()),25,131);
Calendar rightnow = Calendar.getInstance();
int second = rightnow.get(Calendar.SECOND);
int minute = rightnow.get(Calendar.MINUTE);
int hour = rightnow.get(Calendar.HOUR);
//半徑
int R_H = 20,R_M = 4,R_S = 4;
//時針的坐標
//x ====(9-3)[0-6] (3-9)[6-0]
//y ====(12-6)[0-6] (6-12)[6-0]
int H_x ;
int H_y;
//x
if(hour == 0)
{
hour = 12;
}
if( hour = 3 hour = 9 )
{
H_x = R_H*Math.abs(hour - 9);
}
else
{
if(hour 9)
{
H_x = R_H*Math.abs(hour - 9);
}
else
{
H_x = R_H*Math.abs(hour+3);
}
}
//y
if( hour = 6 hour = 12 )
{
H_y = R_H*Math.abs(hour - 12);
}
else
{
H_y = R_H*hour;
}
//分針的坐標
int M_x;
int M_y;
if(minute == 0)
{
minute = 60;
}
if( minute = 15 minute = 45 )
{
M_x = R_M*Math.abs(minute - 45);
}
else
{
if(minute 45)
{
M_x = R_M*Math.abs(minute - 45);
}
else
{
M_x = R_M*Math.abs(minute+15);
}
}
//y
if( minute = 30 minute 60 )
{
M_y = R_M*Math.abs(minute - 60);
}
else
{
M_y = R_M*minute;
}
//秒針的坐標
int S_x;
int S_y;
if(second == 0)
{
second = 60;
}
if( second = 15 second = 45 )
{
S_x = R_S*Math.abs(second - 45);
}
else
{
if(second 45)
{
S_x = R_S*Math.abs(second - 45);
}
else
{
S_x = R_S*Math.abs(second+15);
}
}
//y
if( second = 30 second = 60 )
{
S_y = R_S*Math.abs(second - 60);
}
else
{
S_y = R_S*second;
}
// g.drawString(String.valueOf(second),25,50);
// g.drawString(String.valueOf(minute),25,60);
// g.drawString(String.valueOf(hour),25,70);
// g.drawString(String.valueOf(H_x),25,80);
// g.drawString(String.valueOf(H_y),25,90);
g.drawOval(0,0,120,120);//距離相差10像素
g.setColor(Color.darkGray);
g.drawString("9",5,65);
g.drawString("3",110,65);
g.drawString("12",55,15);
g.drawString("6",55,115);
g.drawString("1",80,20);
g.drawString("2",100,40);
g.drawString("4",100,90);
g.drawString("5",80,110);
g.drawString("7",30,110);
g.drawString("8",10,90);
g.drawString("10",10,40);
g.drawString("11",30,20);
g.setColor(Color.red);
g.drawLine(60,60,H_x,H_y);//前一個點表示起點,另一個表示終點
g.setColor(Color.blue);
g.drawLine(60,60,M_x,M_y);
g.setColor(Color.yellow);
g.drawLine(60,60,S_x,S_y);
}
public void start()
{
if(athread == null)
{
athread = new Thread(this);
athread.start();
}
}
public void stop()
{
if(athread != null)
{
athread.interrupt();
athread = null;
}
}
public void run()
{
while(athread != null)
{
repaint();
try
{
athread.sleep(1000);
}
catch(InterruptedException e)
{
}
}
}
}
利用死循環和線程,讓線程在循環中每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();
}
}
你可以試下代碼,看看是不是你要的效果
可以的,可以實時動態顯示當前時間與當前日期,代碼結構簡潔、清晰、明了,(網絡搜集,供參考)
知識的匯總:
1.HTML5
2.CSS3
3.JavaScript
重難點匯總:
1.各個指針的旋轉角度的獲取,首先要明確以下概念:
一周為360度、12小時、60分鐘、60秒;
公式:一周的度數/一周的時間;
即得出時針每過一小時要旋轉30度;
分針每過一分鐘要旋轉6度;
秒針每過一秒鐘要旋轉6度;
下面是代碼部分:
HTML:
div id="box"
div id="h"/div
div id="min"/div
div id="s"div class="cen"/div/div
div id="data"/div
/div
CSS3:
body{
background-color: #aaa;
margin: 0px;
padding: 0px;
}
#box{
width: 400px;
height: 400px;
border-radius: 100%;
background: url(img/4706.jpg_wh860.jpg)0px 0px no-repeat;
background-size: 400px;
position: absolute;
left: 500px;
top: 200px;
}
#h{
width: 100px;
height: 10px;
background-color: red;
position: relative;
top: 195px;
left: 200px;
}
#min{
width: 140px;
height: 10px;
background-color: yellow;
position: relative;
top: 185px;
left: 200px;
}
#s{
width: 180px;
height: 10px;
background-color: blue;
position: relative;
top: 175px;
left: 200px;
}
.cen{
width: 10px;
height: 10px;
background-color: white;
border-radius: 100%;
}
#data{
position: relative;
top: 100px;
left: 150px;
color: red;
font-size: 20px;
}
JavaScript:
function tim(){
var d = new Date(),//獲取當前系統時間
year = d.getFullYear(),//得到當前年份
mon = d.getMonth(),//得到當前月份
date = d.getDate(), //得到當前日期
hours = d.getHours(), //得到當前小時
minutes = d.getMinutes(), //得到當前分鐘
seconds = d.getSeconds();//得到當前秒
var hou = "";
if(hours12){
hou = "下午";
}
else{
hou = "上午";
}
document.getElementById("data").innerHTML= year+"年"+mon+"月"+date+"日"+"
"+hou;
var n = document.getElementById("s");//獲取秒針ID
n.style.transform = "rotate("+(seconds*6-90)+"deg)";//通過當前秒數,得到秒針旋轉度數
n.style.transformOrigin = "left";//設置秒針旋轉的基點
var i = document.getElementById("min");//獲取分針ID
i.style.transform = "rotate("+(minutes*6-90)+"deg)"http://通過當前分鐘數,得到分針旋轉度數
i.style.transformOrigin = "left";//設置分針旋轉的基點
var h = document.getElementById("h");//獲取時針ID
h.style.transform = "rotate("+((hours*30)+(minutes*0.5)-90)+"deg)"http://通過當前小時數,得到時針旋轉度數
h.style.transformOrigin = "left";//設置時針旋轉的基點
}
setInterval("tim()",1000)
                本文名稱:java動態時間代碼 java編寫一個動態日歷
                
                本文URL:http://www.yijiale78.com/article12/ddgcpdc.html
            
成都網站建設公司_創新互聯,為您提供微信小程序、App設計、自適應網站、網站建設、靜態網站、網站收錄
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯
