问题,在JAVA里面要获取当前的时间。
我是使用了 Date today = new Date(); 来获取到当前的时间。
当然我也可以用System.currentTimeMillis()的方式来获取时间。
但是问题来了。
请看一下简单的代码import java.util.Date;public class MyDate()
{
private Date date = new Date(); public int currentFormatDate()
{
int c_time = date.getHours()*3600+date.getMinutes()*60+date.getSeconds();
return c_time;
}}
_____________________________________________public Class USEmyDate()
{
private MyDate md = new MyDate(); public USEmyDate()
{
for(int i = 0 ; i < 3 ; i++)
{
md.currentFormatDate();
}
}}///////////////////////////////////////////////
问题现在就是在这里了,很明显,如果new Date的地方不正确那么在USEmyDate类里面使用的都是第一次获取到的时间。
但是想实时的获取当前的时间,如果我把 private Date date = new Date(); 这句代码放入了currentFormatDate()
方法里面。如果刷新的比较快的话,很容易导致虚拟机崩溃。
我想问下各位大侠,有什么更好的方法?谢谢。保证获取到的时间是最新的,但是同时又要少,或者不创建 Date这个对象。
如果用System.currentTimeMillis()东西的话,我现在不知道怎么样转换他的时间算法。因为这个东西怎么样都是和年份
有关的,但是我现在不需要这个。希望高手门帮忙。
我是使用了 Date today = new Date(); 来获取到当前的时间。
当然我也可以用System.currentTimeMillis()的方式来获取时间。
但是问题来了。
请看一下简单的代码import java.util.Date;public class MyDate()
{
private Date date = new Date(); public int currentFormatDate()
{
int c_time = date.getHours()*3600+date.getMinutes()*60+date.getSeconds();
return c_time;
}}
_____________________________________________public Class USEmyDate()
{
private MyDate md = new MyDate(); public USEmyDate()
{
for(int i = 0 ; i < 3 ; i++)
{
md.currentFormatDate();
}
}}///////////////////////////////////////////////
问题现在就是在这里了,很明显,如果new Date的地方不正确那么在USEmyDate类里面使用的都是第一次获取到的时间。
但是想实时的获取当前的时间,如果我把 private Date date = new Date(); 这句代码放入了currentFormatDate()
方法里面。如果刷新的比较快的话,很容易导致虚拟机崩溃。
我想问下各位大侠,有什么更好的方法?谢谢。保证获取到的时间是最新的,但是同时又要少,或者不创建 Date这个对象。
如果用System.currentTimeMillis()东西的话,我现在不知道怎么样转换他的时间算法。因为这个东西怎么样都是和年份
有关的,但是我现在不需要这个。希望高手门帮忙。
解决方案 »
- swing中为什么把JTextArea放在JPanel中不显示?
- JSON问题.
- 关于socket的小问题???
- 显示/隐藏ComboBox控件的滚动条提示
- 求救关于jbuilder9的问题,在线等待
- 怎样在两个程序中实现共用同一个文件(包括写)?高分相赠!
- 还有一个很菜的问题
- method can not be overriden to be more private 怎么解释?
- 我是初学者,请问用何开发工具JAVA程序最好?能否象VB6.0那样可是编程?
- 请问在NETSCAPE中如何将JAVA APPLET的权限修改为可对本地文件系统操作?
- LinkedList 的简单的问题啊
- jsp 图片引用问题
把你的代码改一下:
public class MyDate()
{
public int currentFormatDate(Date date)
{
int c_time = date.getHours()*3600+date.getMinutes()*60+date.getSeconds();
return c_time;
} }
_____________________________________________
public Class USEmyDate()
{
private MyDate md = new MyDate(); public USEmyDate()
{
for(int i = 0 ; i < 3 ; i++)
{
md.currentFormatDate(new Date());
}
} }
{
public USEmyDate()
{
Date d = new Date();
for(int i = 0 ; i < 3 ; i++)
{
d.getHours()*3600+d.getMinutes()*60+d.getSeconds(); }
} }
package Test;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
public class qdb
{
public static void main(String[] args)
{
Clock clock=new Clock();
Clock.start(clock);
}
}
class Clock implements Runnable
{
private static SimpleDateFormat format=new SimpleDateFormat("MM月dd日 hh:mm:ss");
private long startTime=System.currentTimeMillis();
public void run()
{
while(!Thread.interrupted())
{
System.out.println(format.format(new Date(startTime)));
try
{
Thread.sleep(1000);
}
catch(Exception e)
{
}
startTime+=1000;
}
}
public static void start(Runnable runnable)
{
ExecutorService es=Executors.newCachedThreadPool();
es.execute(runnable);
es.shutdown();
}
}是这意思么?
{
private final Date date = new Date(); public int currentFormatDate()
{
date.setTime(System.currentTimeMillis);
return date.getHours() * 3600 + date.getMinutes() * 60 + date.getSeconds();
}}
public class MyDate()
{
public int currentFormatDate(Date date)
{
int c_time = date.getHours()*3600+date.getMinutes()*60+date.getSeconds();
return c_time;
} } public Class USEmyDate()
{
private MyDate md = new MyDate(); public USEmyDate()
{
for(int i = 0 ; i < 3 ; i++)
{
md.currentFormatDate(new Date());
}
} }
7楼的为什么不错? try
{
Thread.sleep(1000);
}
catch(Exception e)
{
}
startTime+=1000;sleep所使用的时间是约数,线程被唤醒的时间大于等于它的参数。而不是精确等于,所以这样累加下去的话,误差会越来越大。
for(int i=0; i <19;i++)
{
Data date = new Date();
.....
}如果使用这样方式容易导致 VM 异常。错误信息:#
# An unexpected error has been detected by HotSpot Virtual Machine:
#
# EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x00000000, pid=2984, tid=3952
#
# Java VM: Java HotSpot(TM) Client VM (1.5.0_11-b03 mixed mode)
# Problematic frame:
# C 0x00000000
#
# An error report file with more information is saved as hs_err_pid2984.log
#
# If you would like to submit a bug report, please visit:
# http://java.sun.com/webapps/bugreport/crash.jsp
#
public String getDate() {
Date myDate = new Date();
myDate.setSeconds(myDate.getSeconds() + 10);
SimpleDateFormat dateformat = new SimpleDateFormat("yyyyMMddHHmmss");
String myString = dateformat.format(myDate);
return myString;
}