-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

6
  • 4
    Because java.util.Date represents an instant in time. You should use the java.time API instead, such as LocalDate. Commented Oct 28, 2019 at 10:45
  • 4
    How about LocalDate.now()? Commented Oct 28, 2019 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. Commented Oct 28, 2019 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. Commented Oct 28, 2019 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… Commented Oct 28, 2019 at 20:26

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Any notable difference between using DateTimeFormatter.ISO_DATE and and just toString()?
@Glains I think toString() will give an output in the ISO-8601 format uuuu-MM-dd, while ISO_DATE will put an offset if available. Otherwise there is no obvious or documented difference I could find for now.
I would add a note about time zone. LocalDate.now() gets the current date as seen in the JVM’s current default time zone. You may want to specify a time zone explicitly, such as LocalDate.now( ZoneId.of( "Asia/Tokyo" ) ). Even if you want the default time zone, I recommend saying so explicitly: LocalDate.now( ZoneId.systemDefault() ).
4

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.

Comments

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.

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.