public void setTimeZone(TimeZone zone)
为此 DateFormat 对象的日历设置时区。
参数:
zone - 给定的新时区。
TimeZone
getDefaultpublic static TimeZone getDefault()
获取此主机的默认 TimeZone。默认 TimeZone 的来源可能随实现的变化而变化。
返回:
默认的 TimeZone。
另请参见:
setDefault(java.util.TimeZone) getTimeZone
public static TimeZone getTimeZone(String ID)
获取给定 ID 的 TimeZone。
参数:
ID - TimeZone 的 ID,要么是缩写(如 "PST" ),要么是全名(如 "America/Los_Angeles"),要么是自定义 ID(如 "GMT-8:00")。注意,对缩写的支持只是出于 JDK 1.1.x 兼容性的考虑,因此应该使用全名。
返回:
指定的 TimeZone,如果给定的 ID 无法理解,则返回 GMT 区域。 DateFormat dateFormat = new SimpleDateFormat()
//dateFormat.setTimeZone(TimeZone.getDefault())
dateFormat.setTimeZone(TimeZone.getTimeZone("EET"))
System.out.println(dateFormat.format(new Date()))
//我实现一个时钟窗口程序给你了,好让你更容易理解,希望对你有帮助。import java.awt.*
import java.awt.event.*
import java.util.*
//世界时钟
public class TimerTest {
public static void main(String[] args) {
new TimerTestFrame("世界时钟")
}
}
class TimerTestFrame extends Frame {
/**
*
*/
private static final long serialVersionUID = 1L
public TimerTestFrame(String s) {
super(s)
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose()
System.exit(0)
}
})
setLayout(new GridLayout(2, 6))
ClockCanvas clk1 = new ClockCanvas("北京", "GMT+8")// 创建时钟
ClockCanvas clk2 = new ClockCanvas("巴黎", "GMT+2")
ClockCanvas clk3 = new ClockCanvas("华盛顿", "GMT-4")
ClockCanvas clk4 = new ClockCanvas("洛衫矶", "GMT-7")
ClockCanvas clk5 = new ClockCanvas("伦敦", "GMT+1")
ClockCanvas clk6 = new ClockCanvas("芝加哥", "GMT-5")
add(clk1)
add(clk2)
add(clk3)
add(clk4)
add(clk5)
add(clk6)
setSize(500, 350)// 设置框架宽高
setVisible(true)
}
}
class ClockCanvas extends Canvas implements Runnable {
/**
*
*/
private static final long serialVersionUID = 1L
private int seconds = 0
private String city
private GregorianCalendar calendar
Thread t
public ClockCanvas(String c, String tz) {
city = c
//也可以通过TimeZone.setTimeZone(String n)函数改变时区,n为时区参数名。
calendar = new GregorianCalendar(TimeZone.getTimeZone(tz))
t = new Thread(this)
t.start()
setSize(125, 125)// 设置画布大小
setBackground(Color.black)
}
// 重写父类的方法绘制时钟图形
public void paint(Graphics g) {
Graphics2Dg2d = (Graphics2D) g
BasicStroke bstroke = new BasicStroke(2.0f)
g2d.setStroke(bstroke)
g2d.setColor(Color.green)
g2d.drawOval(0, 0, 100, 100)
bstroke = new BasicStroke(5.0f)
g2d.setStroke(bstroke)
g2d.drawLine(50, 0, 50, 5)
g2d.drawLine(0, 50, 5, 50)
g2d.drawLine(50, 95, 50, 98)
g2d.drawLine(95, 50, 98, 50)
double hourAngle = 2 * Math.PI * (seconds - 3 * 60 * 60)
/ (12 * 60 * 60)
double minuteAngle = 2 * Math.PI * (seconds - 15 * 60) / (60 * 60)
double secondAngle = 2 * Math.PI * (seconds - 15) / (60)
bstroke = new BasicStroke(5.0f)
g2d.setStroke(bstroke)
g2d.drawLine(50, 50, 50 + (int) (30 * Math.cos(hourAngle)),
50 + (int) (30 * Math.sin(hourAngle)))
bstroke = new BasicStroke(3.0f)
g2d.setStroke(bstroke)
g2d.drawLine(50, 50, 50 + (int) (40 * Math.cos(minuteAngle)),
50 + (int) (40 * Math.sin(minuteAngle)))
bstroke = new BasicStroke(1.0f)
g2d.setStroke(bstroke)
g2d.drawLine(50, 50, 50 + (int) (45 * Math.cos(secondAngle)),
50 + (int) (45 * Math.sin(secondAngle)))
g2d.setColor(Color.red)
g2d.drawString(city, 35, 115)
}
public void timeElapsed() {
//new Date()()获得当前时间
System.out.println(new Date()())
calendar.setTime(new Date()())
seconds = calendar.get(Calendar.HOUR) * 60 * 60
+ calendar.get(Calendar.MINUTE) * 60
+ calendar.get(Calendar.SECOND)
}
public void run() {
try {
while (true) {
Thread.sleep(300)
timeElapsed()
repaint()
}
} catch (InterruptedException e) {
}
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)