Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2006

Tip: Looking for answers? Try searching our database.

Is it possible to get a Date object passing milliseconds as argument?

Thread view: 
dkarthik@gmail.com - 20 Jun 2006 16:29 GMT
Hi,

I'm a newbie to Java programming. I am developing a log manager
application, where in log files generated by some other application are
processed and copied to remote location. The log files generated by the
app has name something like below:

rhlin1_1150807460297_1001.log

where in rhlin1 is the hostname and the next value is the
currentTimeMillis() when the file was rolled over and 1001 is some 4
digit consecutive number.

I tried the following snippet of code, to get the date corresponding to
milliseconds value:

import java.util.Date;
import java.util.Locale;
import java.text.DateFormat;
import java.text.ParseException;

public class TimeMillis {

  public static void main(String [] args) {

     String some_time = "1150807460297";
     Date aDate;
     DateFormat fmt = DateFormat.getDateInstance(DateFormat.FULL,
Locale.US);
     try
     {
         aDate = fmt.parse(some_time);
         System.out.println("Date is: "+fmt.format(aDate));
     }catch (ParseException e) {
        System.out.println(e);
     }
     return;
 }
}

and got an exception:

java.text.ParseException: Unparseable date: "1150807460297"

Is it possible to get the Date corresponding to milliseconds by any
other means? I don't have the control over the application that is
generating log files with this format.

Thanks & Regards,

Karthik
Alex Whitney - 20 Jun 2006 16:45 GMT
I don't think it is possible to get the date from the millisecond
count.

If you want to get the date, use the Calendar object:
http://java.sun.com/j2se/1.5.0/docs/api/java/util/Calendar.html

> Hi,
>
[quoted text clipped - 47 lines]
>
> Karthik
Rogan Dawes - 20 Jun 2006 16:46 GMT
> Hi,
>
[quoted text clipped - 11 lines]
> I tried the following snippet of code, to get the date corresponding to
> milliseconds value:

This should be parsed as a Long, and then converted to a Date.

try {
    long millis = Long.parseString("1150807460297").longValue();
    Date date = new Date(millis);
} catch (NumberFormatException nfe) {
    // handle the error
}

Here is a suggestion, for a newbie. Java's documentation is first rate,
in 99.99% of cases.

Your first point of call should therefore be the JavaDocs. I find that
the easiest way to get to the JavaDocs is to google for them.

e.g. "java date" gives the first two references to v1.3.1 and 1.4.2 of
the javadocs for the date class.

http://java.sun.com/j2se/1.4.2/docs/api/java/util/Date.html

The constructor you want is:

<http://java.sun.com/j2se/1.4.2/docs/api/java/util/Date.html#Date(long)>

Regards,

Rogan
Alex Whitney - 20 Jun 2006 16:54 GMT
> > Hi,
> >
[quoted text clipped - 39 lines]
>
> Rogan

Ahh, I didn't know the millisecond count starts from a specific time.
dkarthik@gmail.com - 21 Jun 2006 06:14 GMT
Hi Rogan,

Thanks for your reply. Actually, I tried declaring some_time as Long
and provided the milliseconds value, but I wasn't aware of
parseString() and longValue() methods.

As one of the poster mentioned, I was trying to use the Date(long)
constructor only.

I checked the Java documentation, but wasn't able to get clues on how
to pass long value to the Date() constructor.

Anyway, thanks for all your time and responses.

Thanks & Regards,

Karthik

> > Hi,
> >
[quoted text clipped - 39 lines]
>
> Rogan
Rogan Dawes - 21 Jun 2006 08:14 GMT
> Hi Rogan,
>
[quoted text clipped - 13 lines]
>
> Karthik

Actually, I had a tyop in my snippet. There is no such method as
Long.parseString(String), you should use Long.parseLong(String) instead.

Sorry for the confusion.

Rogan

>>> Hi,
>>>
[quoted text clipped - 39 lines]
>>
>> Rogan
Eric Sosman - 20 Jun 2006 16:52 GMT
dkarthik@gmail.com wrote On 06/20/06 11:29,:
> Hi,
>
[quoted text clipped - 23 lines]
>       String some_time = "1150807460297";
>       Date aDate;

    Delete from here ...

>       DateFormat fmt = DateFormat.getDateInstance(DateFormat.FULL,
> Locale.US);
[quoted text clipped - 5 lines]
>          System.out.println(e);
>       }

... to here, and replace with

    aDate = new Date(Long.parseLong(some_time));

   (If desired, you could also use try/catch in case parseLong
throws NumberFormatException for a bad some_time.)

>       return;
>   }
[quoted text clipped - 7 lines]
> other means? I don't have the control over the application that is
> generating log files with this format.

Signature

Eric.Sosman@sun.com



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.