Current Time in Milliseconds to java.util.date


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 7 of 7

Thread: Current Time in Milliseconds to java.util.date

  1. #1
    Join Date
    Feb 2005
    Posts
    9

    Current Time in Milliseconds to java.util.date

    Just a quickie question for anyone who can spare the time for a newbie:

    How does one go about taking the long returned by System.currentTimeMillis() and assigning it to a Date object?

    I tried using:

    slzDate = (Date)System.currentTimeMillis();

    but got an error stating "inconvertible types."

    Any help to nip this one in the bud? Thanks a ton, I do appreciate it.

  2. #2
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    I would use

    Calendar.getInstance().getTime(), which returns a Date object.

  3. #3
    Join Date
    Jan 2006
    Location
    Sydney
    Posts
    49

    Try something like

    Just to give you an idea:

    Code:
    	
    
    
    	long timeInMillis = System.currentTimeMillis();
    		
    		Calendar cal = Calendar.getInstance();
    		
    		cal.setTimeInMillis(timeInMillis);
    		
    		java.util.Date date = cal.getTime();
    		
    		System.out.println(date)
    Arul

  4. #4
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    Which does in four statements the same thing that my suggested one statement does. This does provide an approach to performing the task when you are dealing with a value from a time which is not the current time ... but you can also use

    Calendar.getInstance().getDate(currentTimeMillis)

    unless you want to capture and keep the Calendar object.
    Last edited by nspils; 04-02-2007 at 08:47 AM.

  5. #5
    Join Date
    Feb 2005
    Posts
    9
    Thanks a bunch, looks like that works.

    I asked my friend about it and he said Date(), if invoked with a no-argument constructor, automagically sets its value to the current time. Will that work too for what I'm doing in this case?

  6. #6
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    This is kind of a funny issue. As you can see from looking at the Date class documentation in the API, most of the constructors and methods are deprecated. Although Calendar still uses a Date object as a representation of a "time" that has date and time values, the manipulation of those classes is usually through the Calendar class. That's why it is a good idea to be familiar with this class.

  7. #7
    Join Date
    Apr 2013
    Posts
    1
    Quote Originally Posted by nspils View Post
    Which does in four statements the same thing that my suggested one statement does. This does provide an approach to performing the task when you are dealing with a value from a time which is not the current time ... but you can also use

    Calendar.getInstance().getDate(currentTimeMillis)

    unless you want to capture and keep the Calendar object.
    Your suggestion is not very tricky or has any positive side effect. At best writing such codes make it extremely unreadable. Even sometime you too will find hard to debug.
    And if anybody thinks he is making code more efficient by writing such lazy code, it should be reminded, you are not. Compiler will have to do all the things which you have not done explicitly. All you did was you save few keystrokes, few lines and made the life of feature reader of the code and probably your own as well more cumbersome.

    So please don't propagate such coding practice.

    Regards,
    Mawia

Similar Threads

  1. Replies: 8
    Last Post: 08-27-2006, 07:44 PM
  2. java program need help!
    By knindza in forum Java
    Replies: 6
    Last Post: 03-23-2006, 04:13 AM
  3. How to use VC library to get the current time in milisecond
    By hai wang in forum authorevents.morrison
    Replies: 1
    Last Post: 11-16-2000, 02:07 PM
  4. Time in milliseconds
    By martin in forum VB Classic
    Replies: 10
    Last Post: 09-20-2000, 05:40 PM
  5. Time in milliseconds
    By martin in forum VB Classic
    Replies: 0
    Last Post: 09-18-2000, 07:01 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center