How to get date part (dispose of time part) from java.util.Date?
Asked Answered
E

5

5

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!

Ennis answered 30/9, 2011 at 11:18 Comment(2)
thanks for the edit Charles :)Ennis
I've answered a similar question at: #1363807Ashby
S
3

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.

Shwa answered 30/9, 2011 at 11:22 Comment(5)
I did the setting calendar components to 0 and it works!. Actually i was working on calendar objects only but i set the date using the 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
I can't use Joda Time as I am working on a product and adding an API for something so trivial would be a waste.Ennis
@Piyush: You say that now... but come back and say it after you've maintained the app for a few years and been constantly bitten by the flaws of the built-in API... seriously, if there's any way you can start using Joda Time here, you'll get a major win in terms of code readability and maintainability.Shwa
@Jon look i know the long term issues. but the thing is i am currently facing only one problem an i can use java API to solve it. Moreover, IBM has a very difficult process for adding new APIs to the product and I am no decision maker here. I just write de codez :PEnnis
@Piyush: Okay, well don't say I didn't warn you... :)Shwa
M
4

The commons-lang DateUtils provide a nice solution for this problem:

watch this

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.

Microampere answered 30/9, 2011 at 11:40 Comment(1)
i was a bit hesitant to use api methods for this as it would require new imports into my class.Ennis
S
3

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.

Shwa answered 30/9, 2011 at 11:22 Comment(5)
I did the setting calendar components to 0 and it works!. Actually i was working on calendar objects only but i set the date using the 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
I can't use Joda Time as I am working on a product and adding an API for something so trivial would be a waste.Ennis
@Piyush: You say that now... but come back and say it after you've maintained the app for a few years and been constantly bitten by the flaws of the built-in API... seriously, if there's any way you can start using Joda Time here, you'll get a major win in terms of code readability and maintainability.Shwa
@Jon look i know the long term issues. but the thing is i am currently facing only one problem an i can use java API to solve it. Moreover, IBM has a very difficult process for adding new APIs to the product and I am no decision maker here. I just write de codez :PEnnis
@Piyush: Okay, well don't say I didn't warn you... :)Shwa
W
2

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

Wheaton answered 30/9, 2011 at 11:21 Comment(1)
yup, this is what i did :). Although, the questions are different. He is using java.sql.* packageEnnis
A
1

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;
    }
}
Ashby answered 10/7, 2015 at 12:13 Comment(0)
B
1

tl;dr

LocalDate ld = 
    myUtilDate.toInstant()
              .atZone( ZoneId.of( "America/Montreal" ) )
              .toLocalDate() ;

Avoid legacy date-time classes

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

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();

Time Zone

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… types

The 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();

Strings

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

About java.time

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.

Branton answered 21/8, 2016 at 21:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.