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

Discussion in 'Java' started by dkarthik@gmail.com, Jun 20, 2006.

  1. Guest

    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
    , Jun 20, 2006
    #1
    1. Advertising

  2. Alex Whitney Guest

    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

    wrote:
    > 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, Jun 20, 2006
    #2
    1. Advertising

  3. Rogan Dawes Guest

    wrote:
    > 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:
    >


    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
    Rogan Dawes, Jun 20, 2006
    #3
  4. Eric Sosman Guest

    wrote On 06/20/06 11:29,:
    > 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;


    Delete from here ...

    > 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);
    > }


    .... 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;
    > }
    > }
    >
    > 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.



    --
    Eric Sosman, Jun 20, 2006
    #4
  5. Alex Whitney Guest

    Rogan Dawes wrote:
    > wrote:
    > > 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:
    > >

    >
    > 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


    Ahh, I didn't know the millisecond count starts from a specific time.
    Alex Whitney, Jun 20, 2006
    #5
  6. Guest

    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

    Rogan Dawes wrote:
    > wrote:
    > > 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:
    > >

    >
    > 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
    , Jun 21, 2006
    #6
  7. Rogan Dawes Guest

    wrote:
    > 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


    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

    >
    > Rogan Dawes wrote:
    >> wrote:
    >>> 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:
    >>>

    >> 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

    >
    Rogan Dawes, Jun 21, 2006
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter Grison

    Date, date date date....

    Peter Grison, May 28, 2004, in forum: Java
    Replies:
    10
    Views:
    3,250
    Michael Borgwardt
    May 30, 2004
  2. Erwin Moller
    Replies:
    80
    Views:
    8,633
    Erwin Moller
    Aug 27, 2004
  3. Replies:
    5
    Views:
    72,273
    Lee Fesperman
    Mar 9, 2005
  4. Guy
    Replies:
    2
    Views:
    184
    Dr John Stockton
    Dec 5, 2003
  5. Replies:
    9
    Views:
    305
    Michal Kleczek
    May 27, 2013
Loading...

Share This Page