快上网专注成都网站设计 成都网站制作 成都网站建设
成都网站建设公司服务热线:028-86922220

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

JAVA代码显示时间的 java显示当前时间的代码

JAVA窗口显示当前时间

用 java的 swing做个图形界面 然后显示当前的时间:

10年积累的成都网站建设、网站设计经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先网站设计制作后付款的网站建设流程,更有永兴免费网站建设让你可以放心的选择与我们合作。

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();//自动适应窗口大小

this.setSize(160, 80);

this.setVisible(true);

}

//设置Timer 1000ms实现一次动作 实际是一个线程

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();

//转换日期显示格式

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中如何设置时间和显示时间??

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 如何显示当前系统日期与时间

通过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

}

一段显示当下时间的JAVA代码

private Shape rect;            //背景矩形

private Font font;            //设置字体

private Date date;            //现在的时间

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("现在的时间是", 20, 30);

date = new Date();

g2.drawString(String.format("%tr", date), 50, 60);       

}

}


当前标题:JAVA代码显示时间的 java显示当前时间的代码
网页URL:http://6mz.cn/article/doecpoi.html

其他资讯