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

D

dkarthik

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
 
R

Rogan Dawes

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
 
E

Eric Sosman

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.)
 
A

Alex Whitney

Rogan said:
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.
 
D

dkarthik

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
 
R

Rogan Dawes

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top