-1

I want to get a Date without time, but always failed. below is my codes:

    long curLong = System.currentTimeMillis();
    curLong = curLong -  curLong % TimeUnit.DAYS.toMillis(1);
    Date date = new Date(curLong);
    System.out.println("date = " + date);

the output:

date = Mon Oct 28 08:00:00 CST 2019

anyone knows why? Thank you

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Alan
  • 143
  • 1
  • 6
  • 4
    Because `java.util.Date` represents an instant in time. You should use the `java.time` API instead, such as `LocalDate`. – Slaw Oct 28 '19 at 10:45
  • 4
    How about `LocalDate.now()`? – Jörn Horstmann Oct 28 '19 at 10:45
  • 5
    General advice: Don't use `java.util.Date` nor `java.sql.Date`, `java.util.Calendar`, and all the related classes. Ignore all StackOverflow or internet advice that tells you to use them. Use only the `java.time` classes, and only tolerate the above classes in legacy code. – RealSkeptic Oct 28 '19 at 10:47
  • What you've written ought to get midnight GMT. I don't know why that would convert to 8 AM CST. That doesn't seem to add up right. – khelwood Oct 28 '19 at 10:52
  • @khelwood 00:00t in GMT agrees with 08:00 China Standard Time, so this is probably what CST stands for in this case. The question was also posted from Nanjing, China. Those d*rn ambiguous time zone abbreviations… – Ole V.V. Oct 28 '19 at 20:26
  • @OleV.V. Thank you for resolving the mystery. – khelwood Oct 28 '19 at 21:09

4 Answers4

4

It is not recommended to use java.util.Date anymore. It was called Date but doesn't necessarily hold only the date information but information about the time additionally.

Use this:

LocalDate today = LocalDate.now();

and print it as

System.out.println(today.format(DateTimeFormatter.ISO_DATE);

using the ISO date format. You can define your own formatting pattern using a

DateTimeFormatter.ofPattern("dd.MM.yyyy");

for example.

deHaar
  • 17,687
  • 10
  • 38
  • 51
  • Any notable difference between using `DateTimeFormatter.ISO_DATE` and and just `toString()`? – Glains Oct 28 '19 at 11:27
  • @Glains I think [`toString()`](https://docs.oracle.com/javase/8/docs/api/index.html) will give an output *in the ISO-8601 format uuuu-MM-dd*, while [`ISO_DATE`](https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html#ISO_DATE) will put an offset if available. Otherwise there is no obvious or documented difference I could find for now. – deHaar Oct 28 '19 at 11:33
3

You can use java.time.LocalDate.now() to get just the date.

Anyway, your case doesn't work as you expect because you are doing nothing to remove the time from the date: you are just "repressing" it, that's why it's zero. If you want to continue this way you could always substring it (substring the Date.toString() of course I meant).

Hope I helped.

Sterconium
  • 559
  • 4
  • 20
3

java.util.Date's javadoc states:

The class Date represents a specific instant in time, with millisecond precision.

Thats why you have date with time

If you want a date you can use : java.time.LocalDate.now() (Java 8+)

Mark Bramnik
  • 39,963
  • 4
  • 57
  • 97
3

First of all, stop using the old java.util.Date. The new Java 8 date and time API has much better classes for all date and time operations.

The LocalDate class does exactly what you want. The current date can be obtained by LocalDate.now().

It also has a lot of facilities to add and subtract days, months etc. and it takes into consideration all the calendar special cases for you.

jbx
  • 21,365
  • 18
  • 90
  • 144