-2

So i don't know if I can trust java any more. In the console it actually prints that date, even though I did everything correctly. What could be the problem? How can I make sure it NEVER happens, since it's crucial for my application?

Windows 10,

JRE/JDK 1.8.0_181

Using java.util.Date

And no, it's not the problem with IDE showing me date at runtime. This very date is actually printed in console as well.

enter image description here

It's Mon Jul 30 13:42:43 MSK 2018 if I print:

Date x = new Date(System.currentTimeMillis()); 
System.out.println(x.toString());

UPDATE. Clearer version. Difference in time is because was debugging and going through each line.

code:

long currentTimeStamp = java.lang.System.currentTimeMillis();
System.out.println(currentTimeStamp);
Date date = new java.util.Date(currentTimeStamp);
System.out.println(date);


long currentTimeStamp2 = java.lang.System.currentTimeMillis();
System.out.println(currentTimeStamp2);
Date date2 = new java.util.Date(currentTimeStamp2);
System.out.println(date2.toString());

output:

Connected to the target VM, address: '127.0.0.1:58669', transport: 'socket'
1532948728528
Thu Jan 01 03:00:00 MSK 1970
1532948743905
Mon Jul 30 14:05:43 MSK 2018
maklas
  • 332
  • 2
  • 9
  • 5
    What is your system current time ? – Alpesh Jikadra Jul 30 '18 at 10:41
  • 1
    same code returns "Mon Jul 30 12:40:36 CEST 2018" for me - so what is your system time ? – Emerson Cod Jul 30 '18 at 10:41
  • It's Mon Jul 30 13:42:43 MSK 2018 if I print: `Date x = new Date(System.currentTimeMillis()); System.out.println(x.toString());` – maklas Jul 30 '18 at 10:43
  • So your code works fine; your problem is just that your IDE is describing the date wrong. Is that it? – khelwood Jul 30 '18 at 10:45
  • 1
    Don't use old Date, just that. Use new time API – azro Jul 30 '18 at 10:46
  • No. I said it prints in console that date as well. Nothing is wrong with `System.curretTimeMillis()`, Nothing is worng with IDE as well. – maklas Jul 30 '18 at 10:46
  • Strange, everything is working as expected on my machine. – Glains Jul 30 '18 at 10:48
  • @Glains I know, right? It seems like there could be nothing wrong with that. 2 lines, simple. But it's not the case – maklas Jul 30 '18 at 10:50
  • 4
    If nothing's wrong with millis or the ide then what's the issue? Please describe the problem specifically. – Dave Newton Jul 30 '18 at 10:50
  • Do you have your own class called `Date` or your own class called `System` that are doing something out of the ordinary? – Dawood ibn Kareem Jul 30 '18 at 10:51
  • 1
    Are you saying that `System.out.println(x)` and `System.out.println(x.toString())` produce completely different results? – khelwood Jul 30 '18 at 10:53
  • @DawoodibnKareem Of course not. I'm not here to screw with people. it's `java.utils.Date` and `java.lang.System` I've been coding for two years. So these kinda questions out of the way.. – maklas Jul 30 '18 at 10:54
  • @khelwood Yes I do, I checked 10 times before posting – maklas Jul 30 '18 at 10:55
  • Whats `date /T` giving you in the console? – Glains Jul 30 '18 at 10:56
  • 2
    You'd be surprised how many people ask questions here when they've written their own version of something like `Date` or `String`, forgotten about it, then start wondering why the usual `Date` or `String` functionality doesn't work any more. It's always worth eliminating stuff like that. – Dawood ibn Kareem Jul 30 '18 at 10:56
  • 2
    Your experience is not relevant. I've been programming for 40 years and still make stupid mistakes. When the issue is poorly described, and suggests a problem with a runtime that's been pretty solid for decades, people are going to ask you to question your assumptions, and rightly so. – Dave Newton Jul 30 '18 at 10:58
  • @DawoodibnKareem I understand that. Just pointed out that it's not as simple. I'm pretty sure problem lies somewhere else. Probably the fact that during debugging, before going down into `new Date(System.currentTimeMillis())` I turn out to be in a class loader, loading Date.class might be the key to resolving the problem. is It because it happens too fast? Right after main method begins – maklas Jul 30 '18 at 10:59
  • Is what happening too fast? Class loading?! No. – Dave Newton Jul 30 '18 at 11:00
  • 2
    So the [mcve] for this ought to show you printing `x` and printing `x.toString()` (in the same program) and then show the output with the two different results. Your screenshot is really just introducing confusion. – khelwood Jul 30 '18 at 11:00
  • @Glains `date /T` gives `30-Jul-18` – maklas Jul 30 '18 at 11:00
  • 1
    Well, when I first read your problem, looked up what MSK is, I was thinking "this is consistent with `System.currentTimeMillis()` returning `0`". But when you claimed that `System.out.println(x)` prints something different from `System.out.println(x.toString())`, I abandoned that line of thought. There's no way that can happen unless you've been hacking the Java runtime. – Dawood ibn Kareem Jul 30 '18 at 11:01
  • I updated my question. And yes, toString() seems to be making some kind of difference – maklas Jul 30 '18 at 11:10
  • @maklas This does not make any sense, whether you pass an `Object` or a `String` to `System.out.println`, `toString` will be called internally if the parameter turns out to be an `Object`. The result should be the same. – Glains Jul 30 '18 at 11:16
  • @Glains I know that. But it just doesn't happen. – maklas Jul 30 '18 at 11:21
  • 1
    I'm voting to close the question because the problem can't be reproduced with the code given in the question: https://ideone.com/tSuir6 – assylias Jul 30 '18 at 12:07
  • Since there's no trivial way this can happen it'll be pretty important to include any and all relevant information, e.g., "the IDE" can mean just about anything (but looks like IntelliJ, version completely unknown), anything related to your classpath, libraries, pre-run settings, any byte code libraries (or any other libraries), and so on. – Dave Newton Jul 30 '18 at 13:06
  • @maklas In a comment above, you typed `java.utils.Date` with an `s` where it should be `java.util.Date`. That kind of error is why folks are asking about the basics. – Basil Bourque Jul 30 '18 at 17:23
  • Managed to reproduce problem while using another machine, jdk, jre and version of Intellij with different plugins. You can try it yourself. Step into Date constructor during debug. – maklas Jul 31 '18 at 15:37

1 Answers1

1

Inexplicable

As comments on your Question indicate, something very strange is afoot on your machine.

Your count of milliseconds since the epoch reference of 1970-01-01T00:00Z (the number 1,532,946,572,167) is being treated as zero. The debugger shows 1970-01-01T03:00:00.000+0300. That 3 AM with an offset of +03:00 means the same moment as 1970-01-01T00:00Z, the epoch reference. In other words, a count of zero (0) milliseconds since 1970-01-01T00:00Z.

Here is simple code:

// Using terrible old legacy class `Date`.
Date x = new Date( System.currentTimeMillis() ) ;
System.out.println( x ) ;

boolean iJavaUtilsDate = x instanceof java.util.Date ;
System.out.println( "instanceof java.util.Date: " + iJavaUtilsDate ) ;

// Using modern *java.time* class `Instant`. 
Instant y = Instant.now() ;  // Capture the current moment in UTC.
System.out.println( y ) ;

Run that code live at IdeOne.com.

Tue Jul 31 04:18:58 GMT 2018

instanceof java.util.Date: true

2018-07-31T04:18:58.136Z

Suggestions

I suggest adding a call to Instant as shown above to your problem code, out of curiosity.

Verify the actual class in use by calling instanceof as shown in code above.

Try installing a new JDK. Perhaps there is some kind of corruption in your present JDK.

java.time

Also, java.util.Date is a terrible class, part of the troublesome old date-time classes that were supplanted years ago by the modern java.time classes. There is no reason to be using this class nowadays.

If you must interoperate with old code not yet updated to java.time, you can convert back-and-forth by calling new conversion methods added to the old classes.


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154