
正文
java时间显示代码 java 时间 格式
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
Java 如何显示当前系统日期与时间
通过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
}
相关问答
Q1: 一段显示当下时间的JAVA代码
private Shape rect; //背景矩形
private Font font; //设置字体
private Date date; //现在java时间显示代码的时间
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("现在java时间显示代码的时间是", 20, 30);
date = new Date();
g2.drawString(String.format("%tr", date), 50, 60);
}
}
Q2: java想要在图形界面上实现实时显示时间的功能,可以做成一个类吗,有没有示例代码参考
我之前倒是写过这么一个,是放在某个类里作为私有类实现的,当然你可以改为一个public的,下面是我的代码,供参考
//私有类,实现时间的实时显示
private class DisplayTime extends Thread //利用线程实现实时显示
{
SimpleDateFormat sdf=new SimpleDateFormat("HH:mm:ss"); //24小时制,精确到秒
public void run()
{
while(true)
{
time.setText(sdf.format(new Date()));
try
{
Thread.sleep(1000); //线程休眠一秒
}
catch(InterruptedException e)
{}
}
}
}
关于java时间显示代码和java 时间 格式的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。





