RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
java代码显示运行日期 java 显示时间

java显示运行时间,怎样设置

使用 java.util.TimerTask 类,开启一个线程。

十年的思礼网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。成都营销网站建设的优势是能够根据用户设备显示端的尺寸不同,自动调整思礼建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。创新互联从事“思礼网站设计”,“思礼网站推广”以来,每个客户项目都认真落实执行。

创建一个 class MyClass extends TimerTask

实现 run() 方法,其中打印出当前时间。

然后在main方法中 new 一个 MyClass

然后:

Timer timer = new Timer();

TimerTask task = MyClass ;

timer.schedule(task, 0, 1*1000);

就会看到每秒打印一个时间了。

java 如何获取应用的运行时间

java获取应用的运行时间,可以利用时间差来获得,使用System.currentTimeMillis()该方法获得此时的时间,代码如下:

package com.qiu.lin.he;

import java.text.ParseException;

public class Ceshi {

public static void main(String[] args) throws ParseException {

double begin = System.currentTimeMillis(); // 程序开始时间,调用系统的当前时间

for (int i = 0; i  10000; i++) {

// 这里执行具体的业务逻辑

System.out.println(i);

}

// 你要运行的程序

double end = System.currentTimeMillis(); // 程序结束时间,调用系统当前时间

double time = end - begin;// 程序的运行时间

System.out.println(time / 60 + "秒");

}

}

运行结果如下:

java 如何获取系统运行时间

有两种方法:

方法一:用java.util.Date类来实现,并结合java.text.DateFormat类来实现时间的格式化,看下面代码:

mport java.util.*; 

import java.text.*;

//以下默认时间日期显示方式都是汉语语言方式

//一般语言就默认汉语就可以了,时间日期的格式默认为MEDIUM风格,比如:2008-6-16 20:54:53

//以下显示的日期时间都是再Date类的基础上的来的,还可以利用Calendar类来实现见类TestDate2.java

public class TestDate { 

public static void main(String[] args) { 

Date now = new Date(); 

DateFormat d1 = DateFormat.getDateInstance(); //默认语言(汉语)下的默认风格(MEDIUM风格,比如:2008-6-16 20:54:53)

String str1 = d1.format(now);

DateFormat d2 = DateFormat.getDateTimeInstance(); 

String str2 = d2.format(now); 

DateFormat d3 = DateFormat.getTimeInstance(); 

String str3 = d3.format(now); 

DateFormat d4 = DateFormat.getInstance(); //使用SHORT风格显示日期和时间

String str4 = d4.format(now);

DateFormat d5 = DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL); //显示日期,周,时间(精确到秒)

String str5 = d5.format(now);

DateFormat d6 = DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG); //显示日期。时间(精确到秒)

String str6 = d6.format(now);

DateFormat d7 = DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT); //显示日期,时间(精确到分)

String str7 = d7.format(now);

DateFormat d8 = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM); //显示日期,时间(精确到分)

String str8 = d8.format(now);//与SHORT风格相比,这种方式最好用

System.out.println("用Date方式显示时间: " + now);//此方法显示的结果和Calendar.getInstance().getTime()一样

System.out.println("用DateFormat.getDateInstance()格式化时间后为:" + str1);

System.out.println("用DateFormat.getDateTimeInstance()格式化时间后为:" + str2);

System.out.println("用DateFormat.getTimeInstance()格式化时间后为:" + str3);

System.out.println("用DateFormat.getInstance()格式化时间后为:" + str4);

System.out.println("用DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL)格式化时间后为:" + str5);

System.out.println("用DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG)格式化时间后为:" + str6);

System.out.println("用DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT)格式化时间后为:" + str7);

System.out.println("用DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM)格式化时间后为:" + str8);

}

}

运行结果:

用Date方式显示时间: Thu Sep 17 09:39:46 CST 2015

用DateFormat.getDateInstance()格式化时间后为:2015-9-17

用DateFormat.getDateTimeInstance()格式化时间后为:2015-9-17 9:39:46

用DateFormat.getTimeInstance()格式化时间后为:9:39:46

用DateFormat.getInstance()格式化时间后为:15-9-17 上午9:39

用DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL)格式化时间后为:2015年9月17日 星期四 上午09时39分46秒 CST

用DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG)格式化时间后为:2015年9月17日 上午09时39分46秒

用DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT)格式化时间后为:15-9-17 上午9:39

用DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM)格式化时间后为:2015-9-17 9:39:46

方法二:用java.util.Calendar类来实现,看下面:

import java.util.*; 

import java.text.*;

//以下是利用Calendar类来实现日期时间的,和Date类相比较比较简单

public class TestDate2 { 

public static void main(String[] args) { 

Calendar ca = Calendar.getInstance();

int year = ca.get(Calendar.YEAR);//获取年份

int month=ca.get(Calendar.MONTH);//获取月份 

int day=ca.get(Calendar.DATE);//获取日

int minute=ca.get(Calendar.MINUTE);//分 

int hour=ca.get(Calendar.HOUR);//小时 

int second=ca.get(Calendar.SECOND);//秒

int WeekOfYear = ca.get(Calendar.DAY_OF_WEEK); 

System.out.println("用Calendar.getInstance().getTime()方式显示时间: " + ca.getTime());

System.out.println("用Calendar获得日期是:" + year +"年"+ month +"月"+ day + "日");

System.out.println("用Calendar获得时间是:" + hour +"时"+ minute +"分"+ second +"秒");

System.out.println(WeekOfYear);//显示今天是一周的第几天(我做的这个例子正好是周二,故结果显示2,如果你再周6运行,那么显示6)

}

}

运行结果是:

用Calendar.getInstance().getTime()方式显示时间: Thu Sep 17 09:40:40 CST 2015

用Calendar获得日期是:2015年8月17日

用Calendar获得时间是:9时40分40秒

5

总结:中的来说,方法二是最方便的,方法一显得分笨拙,不过看个人喜欢了。


文章名称:java代码显示运行日期 java 显示时间
网址分享:http://sczitong.cn/article/dopjhip.html