Do you absolutely have to use java.util.Date? I would thoroughly recommend that you use Joda Time or the java.time package from Java 8 instead. In particular, while Date and Calendar always represent a particular instant in time, with no such concept as "just a date", Joda Time does have a type representing this (LocalDate). Your code will be much clearer if you're able to use types which represent what you're actually trying to do.
There are many, many other reasons to use Joda Time or java.time instead of the built-in java.util types - they're generally far better APIs. You can always convert to/from a java.util.Date at the boundaries of your own code if you need to, e.g. for database interaction.
Do you absolutely have to use java.util.Date? I would thoroughly recommend that you use Joda Time or the java.time package from Java 8 instead. In particular, while Date and Calendar always represent a particular instant in time, with no such concept as "just a date", Joda Time does have a type representing this (LocalDate). Your code will be much clearer if you're able to use types which represent what you're actually trying to do.
There are many, many other reasons to use Joda Time or java.time instead of the built-in java.util types - they're generally far better APIs. You can always convert to/from a java.util.Date at the boundaries of your own code if you need to, e.g. for database interaction.
Here is what I used to get today's date with time set to 00:00:00:
DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
Date today = new Date();
Date todayWithZeroTime = formatter.parse(formatter.format(today));
Converting String to Time without Date in Java - Stack Overflow
Java String to Date without time - Stack Overflow
What's are best practices when dealing with dates, without time-of-day?
Should java.util.Date be deprecated?
Just format it accordingly using SimpleDateFormat#format().
String dateString = new SimpleDateFormat("dd/MM/yyyy").format(dateObject);
The java.util.Date object contains information about both date and time. If you want only the date part in a human representable format, then you need to format it into a String. Invoking Date#toString() as you would get when doing System.out.println(dateObject) would only return the date in format dow mon dd hh:mm:ss zzz yyyy. Also see the linked javadoc.
I have a similar solution to @BalusC, except this one will provide a locale-dependent formatting:
String dateString = java.text.DateFormat.getDateInstance().format(dateObject);
In other words your US and EU customers will get different formatting. One they're familiar with.
What is best practice when dealing with date values without time, in JavaScript? My concern is subtle off-by-one bugs caused by local Time Zone (TZ) offset (e.g. +5 hours), when doing date math.
JavaScript's built-in Date type represents a date and time not just the date. Internal representation is an integer of microseconds since 1970-01-01 00:00:00 UTC. Other languages, like Python, have separate Date and DateTime types. Java 8 introduced LocalDate.
You also have things like: new Date('5/18/2020') is local TZ (in US), but new Date('2022-05-18') is UTC. Same with Date.parse(string). And the time zone on most servers in UTC, whereas on the browser side the time zone will vary.
The date values will be used for simple date math in code and will be stored in a SQL database.
Possibilities:
-
Use the an alternate type like integer value, of milliseconds or days (since 1970-01-01), or string in
YYYYMMDDformat. -
This was combined with #1 -
Use
Dateignoring time (as 00:00 local TZ). Convert from/to UTC when reading/writing to database -
Use
Datewith time as 00:00 UTC. Have to be careful not to mix withDatevalues in local TZ (e.g.now = new Date()) -
Use
Datein local TZ, but convert to UTC when read/writing to database. This is a variant of #3. -
Create a
LocalDateclass that enforces midnight. -
Use a library. js-joda has
LocalDate.
I am leaning towards #3 and #6. Some code I am writing:
class LocalDate extends Date {
// Error if time isn't midnight local TZ
// Do not accept string in ISO format
constructor(date?: Date|number|string)
// Convert to 00:00 UTC
// To be used before write to database
toUtc(): Date
// Only return date. Also affects toJSON()
toISOString(): string
// Returns today's date at 00:00 Local TZ
static today(): LocalDate
// Set time to midnight local TZ, without error check.
static fromDateTime(date: Date|number): LocalDate
// Convert to local TZ. Error if not 00:00 UTC.
static fromUtc(date: Date|number): LocalDate
}UPDATE:
Various edits.