Date.getTime() not including time?

為{幸葍}努か 提交于 2019-12-07 04:01:24

问题


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?

回答1:


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



回答2:


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




回答3:


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.




回答4:


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.




回答5:


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




回答6:


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




回答7:


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.




回答8:


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



回答9:


import java.util.*;

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


来源:https://stackoverflow.com/questions/74620/date-gettime-not-including-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!