
正文
Java中时间代码 java代码生成时间戳
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
一段显示当下时间的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);
}
}
相关问答
Q1: java中计算两个时间相差多少分钟
java中计算两个时间相差多少分钟,具体代码如下:
public static String getDatePoor(Date endDate, Date nowDate) {
long nd = 1000 * 24 * 60 * 60;
long nh = 1000 * 60 * 60;
long nm = 1000 * 60;
// long ns = 1000;
// 获得两个时间的毫秒时间差异
long diff = endDate.getTime() - nowDate.getTime();
// 计算差多少天
long day = diff / nd;
// 计算差多少小时
long hour = diff % nd / nh;
// 计算差多少分钟
long min = diff % nd % nh / nm;
// 计算差多少秒//输出结果
// long sec = diff % nd % nh % nm / ns;
return day + "天" + hour + "小时" + min + "分钟";
}
data默认有toString()
输出格林威治时间,比如说Date date = new Date();
String toStr = date.toString();
扩展资料
Java语言的优良特性使得Java应用具有无比的健壮性和可靠性,这也减少了应用系统的维护费用。Java对对象技术的全面支持和Java平台内嵌的API能缩短应用系统的开发时间并降低成本。
Java的编译一次,到处可运行的特性使得它能够提供一个随处可用的开放结构和在多平台之间传递信息的低成本方式。特别是Java企业应用编程接口(Java Enterprise APIs)为企业计算及电子商务应用系统提供了有关技术和丰富的类库。
1、JDBC(Java Database Connectivity)提供连接各种关系数据库的统一接口。
2、EJB(Enterprise JavaBeans)使得开发者方便地创建、部署和管理跨平台的基于组件的企业应用。
3、Java RMI(Java Remote Method Invocation)用来开发分布式Java应用程序。一个Java对象的方法能被远程Java虚拟机调用。这样,远程方法激活可以发生在对等的两端,也可以发生在客户端和服务器之间,只要双方的应用程序都是用Java写的。
4、Java IDL(Java Interface Definition Language) 提供与CORBA(Common Object Request Broker Architecture)的无逢的互操作性。这使得Java能集成异构的商务信息资源。
5、JNDI(Java Naming and Directory Interface)提供从Java平台到的统一的无逢的连接。这个接口屏蔽了企业网络所使用的各种命名和目录服务。
6、JMAPI(Java Management API)为异构网络上系统、网络和服务管理的开发提供一整套丰富的对象和方法。
7、JMS(Java Message Service)提供企业消息服务,如可靠的消息队列、发布和订阅通信、以及有关推拉(Push/Pull)技术的各个方面。
8、JTS(Java transaction Service)提供存取事务处理资源的开放标准,这些事务处理资源包括事务处理应用程序、事务处理管理及监控。
参考资料:百度百科▬Java计算
Q2: JAVA中获取系统当前时间该怎么写?
一. 获取当前系统时间和日期并格式化输出:\x0d\x0a\x0d\x0aimport java.util.Date; \x0d\x0aimport java.text.SimpleDateFormat;\x0d\x0a\x0d\x0apublic class NowString { \x0d\x0a public static void main(String[] args) { \x0d\x0a SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式\x0d\x0a System.out.println(df.format(new Date()));// new Date()为获取当前系统时间\x0d\x0a } \x0d\x0a} \x0d\x0a\x0d\x0a二. 在数据库里的日期只以年-月-日的方式输出,可以用下面两种方法:\x0d\x0a\x0d\x0a1、用convert()转化函数:\x0d\x0a\x0d\x0aString sqlst = "select convert(varchar(10),bookDate,126) as convertBookDate from roomBook where bookDate between '2007-4-10' and '2007-4-25'";\x0d\x0a\x0d\x0aSystem.out.println(rs.getString("convertBookDate")); \x0d\x0a\x0d\x0a2、利用SimpleDateFormat类:\x0d\x0a\x0d\x0a先要输入两个java包:\x0d\x0a\x0d\x0aimport java.util.Date; \x0d\x0aimport java.text.SimpleDateFormat;\x0d\x0a\x0d\x0a然后:\x0d\x0a\x0d\x0a定义日期格式:SimpleDateFormat sdf = new SimpleDateFormat(yy-MM-dd);\x0d\x0a\x0d\x0asql语句为:String sqlStr = "select bookDate from roomBook where bookDate between '2007-4-10' and '2007-4-25'";\x0d\x0a\x0d\x0a输出:\x0d\x0a\x0d\x0aSystem.out.println(df.format(rs.getDate("bookDate")));
Q3: Java代码中如何获得当前时间
有两种方法Java中时间代码:
方法一Java中时间代码:用java.util.Date类来实现Java中时间代码,并结合java.text.DateFormat类来实现时间的格式化,看下面代码:
import java.util.*;
import java.text.*;
//以下默认时间日期显示方式都是汉语语言方式
//一般语言就默认汉语就可以Java中时间代码了,时间日期的格式默认为MEDIUM风格,比如:2008-6-16 20:54:53
//以下显示的日期时间都是再Date类的基础上的来的,还可以利用Calendar类来实现见类TestDate2.java
public class TestDate {
public static void main(String[] args) {
Date now = new Date();
Calendar cal = Calendar.getInstance();
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方式显示时间: Mon Jun 16 20:54:53 CST 2008
用DateFormat.getDateInstance()格式化时间后为:2008-6-16
用DateFormat.getDateTimeInstance()格式化时间后为:2008-6-16 20:54:53
用DateFormat.getTimeInstance()格式化时间后为:20:54:53
用DateFormat.getInstance()格式化时间后为:08-6-16 下午8:54
用DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL)格式化时间后为
:2008年6月16日 星期一 下午08时54分53秒 CST
用DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG)格式化时间后为
:2008年6月16日 下午08时54分53秒
用DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT)格式化时间后
为:08-6-16 下午8:54
用DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM)格式化时间
后为:2008-6-16 20:54:53
方法二:用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);//显示今天是一周的第几天(Java中时间代码我做的这个例子正好是周二,故结果显示2,如果你再周6运行,那么显示6)
}
}
运行结果是:
用Calendar.getInstance().getTime()方式显示时间: Mon Jun 16 21:54:21 CST 2008
用Calendar获得日期是:2008年5月16日
用Calendar获得时间是:9时54分21秒
2
总结:中的来说,方法二是最方便的,方法一显得分笨拙,不过看个人喜欢了。
还有一种方法利用System.currentTimeMillis()也可以。
Q4: java script时间代码
下面这个javascript是每秒显示一次时间,你只需要把下面的showTime()函数里面程序一修改就可以得到你想要的结果了
setInterval("showTime()", 1000);
function showTime()
{
var today = new Date();
alert("The time is: " + today.toString());
} 答案补充 setInterval("showTime()", 1000);
function showTime()
{
//你把这个时间变成你想要的就可以了啊,然后每过一秒时间就加一秒
var today = new Date(2008,11,30,11,20,45);
alert("The time is: " + today.toString());
} 答案补充 获取日期的时间方法
getYear(): 返回年数
getMonth():返回当月号数
getDate(): 返回当日号数
getDay():返回星期几
getHours():返回小时数
getMintes(:返回分钟数
getSeconds():返回秒数
getTime() : 返回毫秒数
设置日期和时间:
setYear();设置年
setDate():设置当月号数
setMonth():设置当月份数
setHours():设置小时数
setMintes():设置分钟数
setSeconds():设置秒数
setTime ():设置毫秒数
例子:
var d = new Date("2008/11/30");
d.setMonth(d.getMonth() + 1 + 1);//加一个月,同理,可以加一天:getDate()+1,加一年:getYear()+1
关于Java中时间代码和java代码生成时间戳的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。







