views:

5536

answers:

9

Can't understand why the following takes place:

String date = "06-04-2007 07:05";
SimpleDateFormat fmt = new SimpleDateFormat("MM-dd-yyyy HH:mm");
Date myDate = fmt.parse(date); 

System.out.println(myDate);  //Mon Jun 04 07:05:00 EDT 2007
long timestamp = myDate.getTime();
System.out.println(timestamp); //1180955100000 -- where are the milliseconds?

// on the other hand...

myDate = new Date();
System.out.println(myDate);  //Tue Sep 16 13:02:44 EDT 2008
timestamp = myDate.getTime();
System.out.println(timestamp); //1221584564703 -- why, oh, why?
+14  A: 

What milliseconds? You are providing only minutes information in the first example, whereas your second example grabs current date from the system with milliseconds, what is it you're looking for?

String date = "06-04-2007 07:05:00.999";
SimpleDateFormat fmt = new SimpleDateFormat("MM-dd-yyyy HH:mm:ss.S");
Date myDate = fmt.parse(date);

System.out.println(myDate); 
long timestamp = myDate.getTime();
System.out.println(timestamp);
Vinko Vrsalovic
There are no seconds or minutes or hours either! getTime() returns only the long integer representing the date from time 00:00:00. There is no possibility of parsing seconds: they are simply not present in my data. =)
Jake
Why don't you open a new question with your actual code and a bit of sample data if this hasn't solved your doubt? I guess your problem might be somewhere else.
Vinko Vrsalovic
1180955100000 does contain an hour and a minute component as would be expected. You should accept Vinko's answer.
ScArcher2
Yeah, I realized I was just being stupid. I was printing the object and it was displaying only the date, despite the time data being present. Thanks for the help!
Jake
+1  A: 

agree with above, there shouldn't be any milliseconds. . .

Bob Dizzle
+1  A: 

Because simple date format you specified discards the milliseconds. So the resulting Date object does not have that info. So when you print it out, its all 0s.

On the other hand, the Date object does retain the milliseconds when you assign it a value with milliseconds (in this case, using new Date()). So when you print them out, it contains the millisecs too.

Mostlyharmless
A: 

When you parse a date it only uses the information you provide. In this case it only knows MM-dd-yyyy HH:mm.

Creating a new date object returns the current system date/time (number of milliseconds since the epoch).

ScArcher2
A: 

toString() of a Date object does not show you the milliseconds... But they are there

So new Date() is an object with milisecond resolution, as can be seen by:

  System.out.printf( "ms = %d\n", myDate.getTime() % 1000 ) ;

However, when you construct your date with SimpleDateFormat, no milliseconds are passed to it

Am I missing the question here?

[edit] Hahaha...way too slow ;)

tim_yates
A: 

Date.getTime returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by the Date object. So "06-04-2007 07:05" - "01-01-1970 00:00" is equal to 1180955340000 milliseconds. Since the only concern of your question is about the time portion of the date, a rough way of thinking of this calculation is the number of milliseconds between 07:05 and 00:00 which is 25500000. This is evenly divisible by 1000 since neither time has any milliseconds.

In the second date it uses the current time when that line of code is executed. That will use whatever the current milliseconds past the current second are in the calculation. Therefore, Date.getTime will more than likely return a number that is not evenly divisible by 1000.

laz
A: 

The getTime() method of Date returns the number of milliseconds since January 1, 1970 (this date is called the "epoch" because all computer dates are based off of this date). It should not be used to display a human-readable version of your Date.

Use the SimpleDateFormat.format() method instead. Here is a revised version of part of your code that I think may solve your problem:

String date = "06-04-2007 07:05:23:123";
SimpleDateFormat fmt = new SimpleDateFormat("MM-dd-yyyy HH:mm:ss:S");
Date myDate = fmt.parse(date); 

System.out.println(myDate);  //Mon Jun 04 07:05:23 EDT 2007
String formattedDate = fmt.format(myDate);
System.out.println(formattedDate); //06-04-2007 07:05:23:123
Michael Angstadt
+2  A: 

Instead of using the Sun JDK Time/Date libraries (which leave much to be desired) I recommend taking a look at http://joda-time.sourceforge.net.

This is a very mature and active sourceforge project and has a very elegant API.

Ryan Delucchi
A: 

import java.util.*;

public class Time {

public static void main(String[] args) {
    Long l = 0L;
    Calendar c = Calendar.getInstance();
    l = c.getTimeInMillis() % 1000;  //milli sec part of current time
    StringBuffer sb = new StringBuffer(c.getTime().toString());//current time without
                                                                 //millisec
    String s = ":" + l.toString();//millisec in string
    sb.insert(19, s);//insert at right place
    System.out.println(sb);//ENJOY
}

}