问题,在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()东西的话,我现在不知道怎么样转换他的时间算法。因为这个东西怎么样都是和年份
有关的,但是我现在不需要这个。希望高手门帮忙。

解决方案 »

  1.   


    把你的代码改一下:
    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()); 
          } 
      } } 
      

  2.   

    Calender cal = Calenger.getInstance();
      

  3.   

    public Class USEmyDate

        public USEmyDate() 
      { 
          Date d = new Date();
          for(int i = 0 ; i < 3 ; i++) 
          { 
              d.getHours()*3600+d.getMinutes()*60+d.getSeconds();       } 
      } } 
      

  4.   

    为什么不用= System.currentTimeMillis()?
      

  5.   

    你应该可以把它的年份部分减去的吧。年份应该是一个相对固定的部分,可以只得到一次,然后从currentTimeMillis()中减去,可不可以?
      

  6.   


    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();
    }
    }是这意思么?
      

  7.   

    import java.util.Date;public class MyDate()
    {
        private final Date date = new Date();    public int currentFormatDate()
        {
          date.setTime(System.currentTimeMillis);
          return date.getHours() * 3600 + date.getMinutes() * 60 + date.getSeconds();
        }}
      

  8.   

    为什么不用Calender cal = Calenger.getInstance();
      

  9.   

    把你的代码改一下: 
    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()); 
          } 
      } } 
      

  10.   

    Date少用,最好用Calender操作起来很方便
      

  11.   

    Date getHours()等很多方法都已经deprecated了。建议Calender
      

  12.   


    7楼的为什么不错?            try
                {
                    Thread.sleep(1000);
                }
                catch(Exception e)
                {
                    
                }
                startTime+=1000;sleep所使用的时间是约数,线程被唤醒的时间大于等于它的参数。而不是精确等于,所以这样累加下去的话,误差会越来越大。
      

  13.   

    我比较懊恼就是,因为现在面对的这个工程,已经是积压了10多年的东西。里面很多东西都写得不规范。例如
    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
    #
      

  14.   


    public String getDate() {
       Date myDate = new Date();
       myDate.setSeconds(myDate.getSeconds() + 10);
       SimpleDateFormat dateformat = new SimpleDateFormat("yyyyMMddHHmmss");
       String myString = dateformat.format(myDate);
       return myString;
    }