I want to compare the date part of two java.util.Date
objects. How can I achieve this? I am not looking to comparing the date, month and year separately.
Thanks in advance!
I want to compare the date part of two java.util.Date
objects. How can I achieve this? I am not looking to comparing the date, month and year separately.
Thanks in advance!
A Date
is just an instant in time. It only really "means" anything in terms of a date when you apply a calendar and time zone to it. As such, you should really be looking at Calendar
, if you want to stick within the standard Java API - you can create a Calendar
object with the right Date
and time zone, then set the time components to 0.
However, it would be nicer to use Joda Time to start with, and its LocalDate
type, which more accurately reflects what you're interested in.
setTime()
method. That was the unavoidable source of the problem. Also, the compare function i am using has Date as its parameters and not calendar. So, needed something to do with dates. Anyhow, Thanks a lot! –
Ennis The commons-lang DateUtils provide a nice solution for this problem:
With this you can compare two Date
instances in a single line, loosing sight of every part of the Date
you want.
Date date1 = new Date(2011, 8, 30, 13, 42, 15);
Date date2 = new Date(2011, 8, 30, 15, 23, 46);
int compareTo = DateUtils.truncatedCompareTo(date1, date2,
Calendar.DAY_OF_MONTH);
In this example the value of compareTo
is 0.
A Date
is just an instant in time. It only really "means" anything in terms of a date when you apply a calendar and time zone to it. As such, you should really be looking at Calendar
, if you want to stick within the standard Java API - you can create a Calendar
object with the right Date
and time zone, then set the time components to 0.
However, it would be nicer to use Joda Time to start with, and its LocalDate
type, which more accurately reflects what you're interested in.
setTime()
method. That was the unavoidable source of the problem. Also, the compare function i am using has Date as its parameters and not calendar. So, needed something to do with dates. Anyhow, Thanks a lot! –
Ennis Use Calendar.set()
with Calendar.HOUR_OF_DAY
, Calendar.MINUTE
, Calendar.SECOND
and Calendar.MILLISECOND
to set all those fields to 0.
The answers to this duplicate question will be useful: Resetting the time part of a timestamp in Java
java.sql.*
package –
Ennis Find below a solution which employs Joda Time and supports time zones.
So, you will obtain date and time (into currentDate
and currentTime
) in the currently configured timezone in the JVM.
Please notice that Joda Time does not support leap seconds. So, you can be some 26 or 27 seconds off the true value. This probably will only be solved in the next 50 years, when the accumulated error will be closer to 1 min and people will start to care about it.
See also: https://en.wikipedia.org/wiki/Leap_second
/**
* This class splits the current date/time (now!) and an informed date/time into their components:
* <lu>
* <li>schedulable: if the informed date/time is in the present (now!) or in future.</li>
* <li>informedDate: the date (only) part of the informed date/time</li>
* <li>informedTime: the time (only) part of the informed date/time</li>
* <li>currentDate: the date (only) part of the current date/time (now!)</li>
* <li>currentTime: the time (only) part of the current date/time (now!)</li>
* </lu>
*/
public class ScheduleDateTime {
public final boolean schedulable;
public final long millis;
public final java.util.Date informedDate;
public final java.util.Date informedTime;
public final java.util.Date currentDate;
public final java.util.Date currentTime;
public ScheduleDateTime(long millis) {
final long now = System.currentTimeMillis();
this.schedulable = (millis > -1L) && (millis >= now);
final TimeZoneUtils tz = new TimeZoneUtils();
final java.util.Date dmillis = new java.util.Date( (millis > -1L) ? millis : now );
final java.time.ZonedDateTime zdtmillis = java.time.ZonedDateTime.ofInstant(dmillis.toInstant(), java.time.ZoneId.systemDefault());
final java.util.Date zdmillis = java.util.Date.from(tz.tzdate(zdtmillis));
final java.util.Date ztmillis = new java.util.Date(tz.tztime(zdtmillis));
final java.util.Date dnow = new java.util.Date(now);
final java.time.ZonedDateTime zdtnow = java.time.ZonedDateTime.ofInstant(dnow.toInstant(), java.time.ZoneId.systemDefault());
final java.util.Date zdnow = java.util.Date.from(tz.tzdate(zdtnow));
final java.util.Date ztnow = new java.util.Date(tz.tztime(zdtnow));
this.millis = millis;
this.informedDate = zdmillis;
this.informedTime = ztmillis;
this.currentDate = zdnow;
this.currentTime = ztnow;
}
}
public class TimeZoneUtils {
public java.time.Instant tzdate() {
final java.time.ZonedDateTime zdtime = java.time.ZonedDateTime.now();
return tzdate(zdtime);
}
public java.time.Instant tzdate(java.time.ZonedDateTime zdtime) {
final java.time.ZonedDateTime zddate = zdtime.truncatedTo(java.time.temporal.ChronoUnit.DAYS);
final java.time.Instant instant = zddate.toInstant();
return instant;
}
public long tztime() {
final java.time.ZonedDateTime zdtime = java.time.ZonedDateTime.now();
return tztime(zdtime);
}
public long tztime(java.time.ZonedDateTime zdtime) {
final java.time.ZonedDateTime zddate = zdtime.truncatedTo(java.time.temporal.ChronoUnit.DAYS);
final long millis = zddate.until(zdtime, java.time.temporal.ChronoUnit.MILLIS);
return millis;
}
}
LocalDate ld =
myUtilDate.toInstant()
.atZone( ZoneId.of( "America/Montreal" ) )
.toLocalDate() ;
You are using troublesome old legacy date-time classes. Avoid them.
Instead use java.time classes. These supplant the old classes as well as the Joda-Time library.
Convert your java.util.Date
to an Instant
.
The Instant
class represents a moment on the timeline in UTC with a resolution of nanoseconds.
Instant instant = myUtilDate.toInstant();
Apply a time zone. Time zone is crucial. For any given moment the date varies around the globe by zone. For example, a few minutes after midnight in Paris France is a new day while also being “yesterday” in Montréal Québec.
Apply a ZoneId
to get a ZonedDateTime
object.
ZoneId z = ZoneId.of( "America/Montreal" );
ZonedDateTime zdt = instant.atZone( z );
Local…
typesThe LocalDate
class represents a date-only value without time-of-day and without time zone. Likewise, the LocalTime
represents a time-of-day without a date and without a time zone. You can think of these as two components which along with a ZoneId
make up a ZonedDateTime
. You can extract these from a ZonedDateTime
.
LocalDate ld = zdt.toLocalDate();
LocalTime lt = zdt.toLocalTime();
If your goal is merely generating Strings for presentation to the user, no need for the Local…
types. Instead, use DateTimeFormatter
to generate strings representing only the date-portion or the time-portion. That class is smart enough to automatically localize while generating the String.
Specify a Locale
to determine (a) the human language used for translating name of day, name of month, and such, and (b) the cultural norms for deciding issues such as abbreviation, capitalization, punctuation, and such.
Locale l = Locale.CANADA_FRENCH ; // Or Locale.US, Locale.ITALY, etc.
DateTimeFormatter fDate = DateTimeFormatter.ofLocalizedDate( FormatStyle.MEDIUM ).withLocale( locale );
String outputDate = zdt.format( fDate );
DateTimeFormatter fTime = DateTimeFormatter.ofLocalizedTime( FormatStyle.MEDIUM ).withLocale( locale );
String outputTime = zdt.format( fTime );
The java.time framework is built into Java 8 and later. These classes supplant the old troublesome date-time classes such as java.util.Date
, .Calendar
, & java.text.SimpleDateFormat
.
The Joda-Time project, now in maintenance mode, advises migration to java.time.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations.
Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport and further adapted to Android in ThreeTenABP (see How to use…).
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time.
© 2022 - 2024 — McMap. All rights reserved.