-2

I tested this code:

java.util.Date d=new java.util.Date();
System.out.println("date="+d);

The output is:

Sat May 09 02:48:42 CDT 2015

It has no milliseconds...

p.s lets say if to use Date for ConcurrentLinkedHashMap<Date,String> (wiki link) I can see the minimum date value is second :(

EDIT :

public class HashMapTest0 {

    private ConcurrentMap<Date,Date> values=new ConcurrentLinkedHashMap.Builder<Date, Date>()
            .maximumWeightedCapacity(1000)
            .build();

    public static void main(String [] args){new HashMapTest0();}

    HashMapTest0(){


        for(int i=0; i<10; i++){

            Date d=new Date();            
            values.put(d, d);

            try {
                Thread.sleep(10);
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
        }

        for(Date d:values.values()){
            String s=new SimpleDateFormat("dd-MM-yyyy:HH:mm:ss.sss").format(d)
                            .replaceAll("-", "_")
                            .replaceAll(":", "_");
            System.out.println(s);
        }

    }

}

the output maybe something like this :

09_05_2015_04_07_10.010
09_05_2015_04_07_10.010
09_05_2015_04_07_10.010
09_05_2015_04_07_10.010
09_05_2015_04_07_10.010
09_05_2015_04_07_10.010
09_05_2015_04_07_10.010
09_05_2015_04_07_10.010
09_05_2015_04_07_10.010
09_05_2015_04_07_10.010

So my question is how to generate date with real time milliseconds, or is there is a more optimal way?

user390525
  • 263
  • 1
  • 2
  • 18
  • I don't like DateFormatter returns String :( I want to have Comparable interface support – user390525 May 09 '15 at 08:10
  • That's what you use `Date` etc. for. Java 8 has the `java.time` API. – Maarten Bodewes May 09 '15 at 08:11
  • 3
    The Date has milliseconds. They're just not printed by its toString() method. Use a DateFormat to print the date the way you want. – JB Nizet May 09 '15 at 08:11
  • That `Date` doesn't directly show milliseconds when printed, but it *does* have millisecond precission; that's probably where the confusion lies. – Maarten Bodewes May 09 '15 at 08:14
  • The thing is if to put date as lets say HashMap key I cannot see mls support; The minimum filter value is second – user390525 May 09 '15 at 08:17
  • 2
    @user390525 no, it's not. Your test is wrong, just as the one you have in your question. Create a Date. Create another one with the same time in millis + 1 (with `Date secondDate = new Date(firstDate.getTime() + 1)`). Put them all as keys in a HashMap. Print the map size. You'll get 2. Or simpler, read the javadoc of Date: it's not lying to you. – JB Nizet May 09 '15 at 08:19
  • Actually I tried ConcurrentLinkedHashMap https://code.google.com/p/concurrentlinkedhashmap/wiki/ and I can see the filtering is quite strange minimum second by second :( – user390525 May 09 '15 at 08:24
  • Well your code is wrong. But you didn't post it, so we can't tell you where it's wrong. BTW, a map doesn't filter anything. It stores keys and values. – JB Nizet May 09 '15 at 08:27
  • You'll need to explain what you mean by "filtering" - I added information to my answer - both `ConcurrentLinkedHashMap` and `HashMap` use the milliseconds value via `date.hashCode()` – CupawnTae May 09 '15 at 08:27
  • @JBNizet I am interested in items add order :) – user390525 May 09 '15 at 08:31
  • 2
    @user390525 again. You have a bug in your code. Or in the way you interpret what your code does. But since you chose not to post a single line of your code, we can't help. If you don't understand some code, post it, tell us what you expect it to do and what it does instead, and we'll explain. Regarding your current question, you got your answer: printing a date doesn't show its milliseconds, but that doesn't mean it doesn't have millis precision. Printing a picture of you won't show any stomach, although I bet you have one. – JB Nizet May 09 '15 at 08:34
  • @user390525 I think you want a `SortedMap` http://docs.oracle.com/javase/7/docs/api/java/util/SortedMap.html – CupawnTae May 09 '15 at 08:36
  • And if the `String`s you're adding to your map are the `String` representations of your `Date` objects, the same points about converting the `Date` to `String` apply whenever you're doing that conversion. The keys will be millisecond precision, but the `String` representation of those dates will still not include the millisecond precision unless you use something like `DateFormat` – CupawnTae May 09 '15 at 08:43
  • @JBNizet maybe yes but I am not sure; I tried to make tested code which emulate the mentioned issue – user390525 May 09 '15 at 09:05
  • I provided an answer. – JB Nizet May 09 '15 at 09:16

5 Answers5

3
new SimpleDateFormat("dd-MM-yyyy:HH:mm:ss.sss")

You're never printing milliseconds here. Read the javadoc of SimpleDateFormat, or the answers you already got. The pattern for milliseconds is SSS, not sss. sss prints seconds.

Note that even in your incorrect test, you got 10 values being printed. And that is a proof that the 10 dates are different. If they were the same, you'd have a single value printed.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • That's right but I need unique String; And, yes, I guess you right I need the upper case .SSS for milliseconds I just tried; Could you give a tip concerning "".replaceAll(".","_"); I tried the code to replace dots but that causes strange result; So how to replace mls separator dot correctly? – user390525 May 09 '15 at 09:43
  • 1
    Use replace(), not replaceAll(). replaceAll() takes a regex as argument. There is a pattern here: all your questions can be answered by reading the documentation. – JB Nizet May 09 '15 at 09:51
1

The Date object uses milliseconds internally, but when you're displaying it, you're calling its toString() which converts it to the format you see.

If you are only worried about Comparable, that's fine, the comparison will be done on a millisecond basis if you compare the Date objects directly.

A HashMap will use the Date object's equals() method to compare keys, which does a milliseconds comparison:

The result is true if and only if the argument is not null and is a Date object that represents the same point in time, to the millisecond, as this object.

If you want a Map that orders the entries by key, use a SortedMap.

If you want to display the milliseconds value, you can use a DateFormat or date.getTime()

CupawnTae
  • 14,192
  • 3
  • 29
  • 60
  • 1
    Note that the fact that hashCode() uses to milliseconds is irrelevant. hashCode() could always return 0 if it wanted: that would just make the map slower. In the end the map uses equals() to compare two dates, and equals() considers two dates equal if they have the same time, in milliseconds. – JB Nizet May 09 '15 at 08:30
  • @JBNizet good point, fixed – CupawnTae May 09 '15 at 08:31
0

Try this: EDIT

 SimpleDateFormat sdf = new SimpleDateFormat(" E MMM dd hh:mm:ss.SSS yyyy");
  System.out.println(sdf.format(new Date()));

output :

  Sat May 09 01:59:02.718 2015
Rustam
  • 6,485
  • 1
  • 25
  • 25
0

The way to do it in java8 is an Instant represents a point in time (similar to java.util.Date) with nanoseconds precision (unlike the old Date which has milliseconds precision). Representing a point in time using nanoseconds precision requires more space than a Long can provide, therefore the internal representation is composed of two Long fields, the first holds the number of seconds since (or before) the standard Java epoch and the other the number of nanoseconds of the last seconds (so never larger than 999,999,999).

/**Get the current time*/ Instant instant = Instant.now();

/** Output format is ISO-8601*/ System.out.println(instant);


Kumar Abhishek
  • 152
  • 1
  • 11
-2

use JDateTime

JDateTime jdt = new JDateTime(System.currentTimeMillis());
//result is 2015-05-09 16:23:25.344

you can use jdt.getMillisecond() to get millisecond.

reeco
  • 53
  • 3