<p>Can't understand why the following takes place:</p>

<pre><code>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?
</code></pre>

[{'answer_id': 74652, 'author': 'Vinko Vrsalovic', 'author_id': 5190, 'author_profile': 'https://Stackoverflow.com/users/5190', 'pm_score': 5, 'selected': True, 'text': '<p>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?</p>\n\n<pre><code>String date = "06-04-2007 07:05:00.999";\nSimpleDateFormat fmt = new SimpleDateFormat("MM-dd-yyyy HH:mm:ss.S");\nDate myDate = fmt.parse(date);\n\nSystem.out.println(myDate); \nlong timestamp = myDate.getTime();\nSystem.out.println(timestamp);\n</code></pre>\n'}, {'answer_id': 74683, 'author': 'Mostlyharmless', 'author_id': 12881, 'author_profile': 'https://Stackoverflow.com/users/12881', 'pm_score': 2, 'selected': False, 'text': '<p>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.</p>\n\n<p>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.</p>\n'}, {'answer_id': 74685, 'author': 'ScArcher2', 'author_id': 1310, 'author_profile': 'https://Stackoverflow.com/users/1310', 'pm_score': 0, 'selected': False, 'text': '<p>When you parse a date it only uses the information you provide.\nIn this case it only knows MM-dd-yyyy HH:mm.</p>\n\n<p>Creating a new date object returns the current system date/time (number of milliseconds since the epoch). </p>\n'}, {'answer_id': 74694, 'author': 'tim_yates', 'author_id': 6509, 'author_profile': 'https://Stackoverflow.com/users/6509', 'pm_score': 0, 'selected': False, 'text': '<p>toString() of a Date object does not show you the milliseconds...  But they are there</p>\n\n<p>So new Date() is an object with milisecond resolution, as can be seen by:</p>\n\n<pre><code>  System.out.printf( "ms = %d\\n", myDate.getTime() % 1000 ) ;\n</code></pre>\n\n<p>However, when you construct your date with SimpleDateFormat, no milliseconds are passed to it</p>\n\n<p>Am I missing the question here?</p>\n\n<p>[edit] Hahaha...way too slow ;)</p>\n'}, {'answer_id': 75140, 'author': 'laz', 'author_id': 8753, 'author_profile': 'https://Stackoverflow.com/users/8753', 'pm_score': 0, 'selected': False, 'text': '<p>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.</p>\n\n<p>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.</p>\n'}, {'answer_id': 76006, 'author': 'Michael', 'author_id': 13379, 'author_profile': 'https://Stackoverflow.com/users/13379', 'pm_score': 0, 'selected': False, 'text': '<p>The <code>getTime()</code> method of <code>Date</code> 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 <strong>not</strong> be used to display a human-readable version of your Date.</p>\n\n<p>Use the <code>SimpleDateFormat.format()</code> method instead.  Here is a revised version of part of your code that I think may solve your problem:</p>\n\n<pre><code>String date = "06-04-2007 07:05:23:123";\nSimpleDateFormat fmt = new SimpleDateFormat("MM-dd-yyyy HH:mm:ss:S");\nDate myDate = fmt.parse(date); \n\nSystem.out.println(myDate);  //Mon Jun 04 07:05:23 EDT 2007\nString formattedDate = fmt.format(myDate);\nSystem.out.println(formattedDate); //06-04-2007 07:05:23:123\n</code></pre>\n'}, {'answer_id': 78965, 'author': 'Ryan Delucchi', 'author_id': 9931, 'author_profile': 'https://Stackoverflow.com/users/9931', 'pm_score': 2, 'selected': False, 'text': '<p>Instead of using the Sun JDK Time/Date libraries (which leave much to be desired) I recommend taking a look at <a href="http://joda-time.sourceforge.net" rel="nofollow noreferrer">http://joda-time.sourceforge.net</a>.</p>\n\n<p>This is a very mature and active sourceforge project and has a very elegant API.</p>\n'}, {'answer_id': 647220, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': '<pre><code>import java.util.*;\n\npublic class Time {\n    public static void main(String[] args) {\n        Long l = 0L;\n        Calendar c = Calendar.getInstance();\n        //milli sec part of current time\n        l = c.getTimeInMillis() % 1000;  \n        //current time without millisec\n        StringBuffer sb = new StringBuffer(c.getTime().toString());\n        //millisec in string\n        String s = ":" + l.toString();\n        //insert at right place\n        sb.insert(19, s);\n        //ENJOY\n        System.out.println(sb);\n    }\n}\n</code></pre>\n'}, {'answer_id': 61567225, 'author': 'Basil Bourque', 'author_id': 642706, 'author_profile': 'https://Stackoverflow.com/users/642706', 'pm_score': 1, 'selected': False, 'text': '<h1>tl;dr</h1>\n\n<p>The accepted <a href="https://stackoverflow.com/a/74652/642706">Answer by Vinko Vrsalovic</a> is correct. Your input is whole minutes, so the milliseconds for fractional second should indeed be zero.</p>\n\n<p>Use <em>java.time</em>.</p>\n\n<pre><code>LocalDateTime.parse\n( \n    "06-04-2007 07:05" , \n    DateTimeFormatter.ofPattern( "MM-dd-uuuu HH:mm" ) \n)\n.atZone\n(\n    ZoneId.of( "Africa/Casablanca" ) \n)\n.toInstant()\n.getEpochMilli()\n</code></pre>\n\n<h1><em>java.time</em></h1>\n\n<p>The modern approach uses the <em>java.time</em> classes defined in JSR 310 that years ago supplanted the terrible classes you are using.</p>\n\n<p>Define a formatting pattern to match your input. FYI: Learn to use standard ISO 8601 formats for exchanging date-time values as text.</p>\n\n<pre><code>String input = "06-04-2007 07:05" ;\nDateTimeFormatter f = DateTimeFormatter.ofPattern( "MM-dd-uuuu HH:mm" ) ;\n</code></pre>\n\n<p>Parse your input as a <code>LocalDateTime</code>, as it lacks an indicator of time zone or offset-from-UTC.</p>\n\n<pre><code>LocalDateTime ldt = LocalDateTime.parse( input , f ) ;\n</code></pre>\n\n<p>This represents a date and a time-of-day, but lacks the context of a time zone or offset. So we do not know if you meant 7 AM in Tokyo Japan, 7 AM in Toulouse France, or 7 AM in Toledo Ohio US. This issue of time zone is crucial, because your desired count of milliseconds is a count since the first moment of 1970 as seen in UTC (an offset of zero hours-minutes-seconds), 1970-01-01T00:00Z. </p>\n\n<p>So we must place your input value, the <a href="https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/time/LocalDateTime.html" rel="nofollow noreferrer"><code>LocalDateTime</code></a> object, in the context of a time zone or offset. </p>\n\n<p>If your input was intended to represent a date and time in UTC, use <a href="https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/time/OffsetDateTime.html" rel="nofollow noreferrer"><code>OffsetDateTime</code></a> with <a href="https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/time/ZoneOffset.html#UTC" rel="nofollow noreferrer"><code>ZoneOffset.UTC</code></a>.</p>\n\n<pre><code>OffsetDateTime odt = ldt.atOffset( ZoneOffset.UTC ) ;  // Do this if your date and time represent a moment as seen in UTC. \n</code></pre>\n\n<p>If your input was intended to represent a date and time as seen through the wall-clock time used by the people of a particular region, use <a href="https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/time/ZonedDateTime.html" rel="nofollow noreferrer"><code>ZonedDateTime</code></a>. </p>\n\n<pre><code>ZoneId z = ZoneId.of( "Asia/Tokyo" ) ;\nZonedDateTime zdt = ldt.atZone( z ) ;\n</code></pre>\n\n<p>Next we want to interrogate for the count of milliseconds since the epoch of first moment of 1970 in UTC. With either a <code>OffsetDateTime</code> or <code>ZonedDateTime</code> object in hand, extract a <code>Instant</code> by calling <code>toInstant</code>. </p>\n\n<pre><code>Instant instant = odt.toInstant() ;\n</code></pre>\n\n<p>…or…</p>\n\n<pre><code>Instant instant = zdt.toInstant() ;\n</code></pre>\n\n<p>Now get count of milliseconds. </p>\n\n<pre><code>long millisecondsSinceEpoch = instant.toEpochMilli() ;\n</code></pre>\n\n<p>By the way, I suggest you not track time by a count of milliseconds. Use ISO 8601 formatted text instead: easy to parse by machine, easy to read by humans across cultures. A count of milliseconds is neither. </p>\n\n<hr>\n\n<p><a href="https://i.stack.imgur.com/CNlDF.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/CNlDF.png" alt="Table of date-time types in Java, both modern and legacy"></a></p>\n\n<hr>\n\n<h1>About <em>java.time</em></h1>\n\n<p>The <a href="https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/time/package-summary.html" rel="nofollow noreferrer"><em>java.time</em></a> framework is built into Java 8 and later. These classes supplant the troublesome old <a href="https://en.wikipedia.org/wiki/Legacy_system" rel="nofollow noreferrer">legacy</a> date-time classes such as <a href="https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Date.html" rel="nofollow noreferrer"><code>java.util.Date</code></a>, <a href="https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Calendar.html" rel="nofollow noreferrer"><code>Calendar</code></a>, &amp; <a href="https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/text/SimpleDateFormat.html" rel="nofollow noreferrer"><code>SimpleDateFormat</code></a>.</p>\n\n<p>To learn more, see the <a href="http://docs.oracle.com/javase/tutorial/datetime/TOC.html" rel="nofollow noreferrer"><em>Oracle Tutorial</em></a>. And search Stack Overflow for many examples and explanations. Specification is <a href="https://jcp.org/en/jsr/detail?id=310" rel="nofollow noreferrer">JSR 310</a>.</p>\n\n<p>The <a href="http://www.joda.org/joda-time/" rel="nofollow noreferrer"><em>Joda-Time</em></a> project, now in <a href="https://en.wikipedia.org/wiki/Maintenance_mode" rel="nofollow noreferrer">maintenance mode</a>, advises migration to the <a href="https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/time/package-summary.html" rel="nofollow noreferrer">java.time</a> classes.</p>\n\n<p>You may exchange <em>java.time</em> objects directly with your database. Use a <a href="https://en.wikipedia.org/wiki/JDBC_driver" rel="nofollow noreferrer">JDBC driver</a> compliant with <a href="http://openjdk.java.net/jeps/170" rel="nofollow noreferrer">JDBC 4.2</a> or later. No need for strings, no need for <code>java.sql.*</code> classes. Hibernate 5 &amp; JPA 2.2 support <em>java.time</em>. </p>\n\n<p>Where to obtain the java.time classes? </p>\n\n<ul>\n<li><a href="https://en.wikipedia.org/wiki/Java_version_history#Java_SE_8" rel="nofollow noreferrer"><strong>Java SE 8</strong></a>, <a href="https://en.wikipedia.org/wiki/Java_version_history#Java_SE_9" rel="nofollow noreferrer"><strong>Java SE 9</strong></a>, <a href="https://en.wikipedia.org/wiki/Java_version_history#Java_SE_10" rel="nofollow noreferrer"><strong>Java SE 10</strong></a>, <a href="https://en.wikipedia.org/wiki/Java_version_history#Java_SE_11" rel="nofollow noreferrer"><strong>Java SE 11</strong></a>, and later  - Part of the standard Java API with a bundled implementation.\n\n<ul>\n<li>Java 9 adds some minor features and fixes.</li>\n</ul></li>\n<li><a href="https://en.wikipedia.org/wiki/Java_version_history#Java_SE_6" rel="nofollow noreferrer"><strong>Java SE 6</strong></a> and <a href="https://en.wikipedia.org/wiki/Java_version_history#Java_SE_7" rel="nofollow noreferrer"><strong>Java SE 7</strong></a>\n\n<ul>\n<li>Most of the <em>java.time</em> functionality is back-ported to Java 6 &amp; 7 in <a href="http://www.threeten.org/threetenbp/" rel="nofollow noreferrer"><strong><em>ThreeTen-Backport</em></strong></a>.</li>\n</ul></li>\n<li><a href="https://en.wikipedia.org/wiki/Android_(operating_system)" rel="nofollow noreferrer"><strong>Android</strong></a>\n\n<ul>\n<li>Later versions of Android bundle implementations of the <em>java.time</em> classes.</li>\n<li>For earlier Android (&lt;26), the <a href="https://github.com/JakeWharton/ThreeTenABP" rel="nofollow noreferrer"><strong><em>ThreeTenABP</em></strong></a> project adapts <a href="http://www.threeten.org/threetenbp/" rel="nofollow noreferrer"><strong><em>ThreeTen-Backport</em></strong></a> (mentioned above). See <a href="http://stackoverflow.com/q/38922754/642706"><em>How to use ThreeTenABP…</em></a>.</li>\n</ul></li>\n</ul>\n'}]
['https://Stackoverflow.com/questions/74620', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/10675/']