Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the Time component of a Java Date or Calendar

Is there a simple or elegant way to grab only the time of day (hours/minutes/seconds/milliseconds) part of a Java Date (or Calendar, it really doesn't matter to me)? I'm looking for a nice way to separately consider the date (year/month/day) and the time-of-day parts, but as far as I can tell, I'm stuck with accessing each field separately.

I know I could write my own method to individually grab the fields I'm interested, but I'd be doing it as a static utility method, which is ugly. Also, I know that Date and Calendar objects have millisecond precision, but I don't see a way to access the milliseconds component in either case.


Edit: I wasn't clear about this: using one of the Date::getTime() or Calendar::getTimeInMillis is not terribly useful to me, since those return the number of milliseconds since the epoch (represented by that Date or Calendar), which does not actually separate the time of day from the rest of the information.

@Jherico's answer is the closest thing, I think, but definitely is something I'd still have to roll into a method I write myself. It's not exactly what I'm going for, since it still includes hours, minutes, and seconds in the returned millisecond value - though I could probably make it work for my purposes.

I still think of each component as separate, although of course, they're not. You can write a time as the number of milliseconds since an arbitrary reference date, or you could write the exact same time as year/month/day hours:minutes:seconds.milliseconds.

This is not for display purposes. I know how to use a DateFormat to make pretty date strings.


Edit 2: My original question arose from a small set of utility functions I found myself writing - for instance:

  • Checking whether two Dates represent a date-time on the same day;
  • Checking whether a date is within a range specified by two other dates, but sometimes checking inclusively, and sometimes not, depending on the time component.

Does Joda Time have this type of functionality?


Edit 3: @Jon's question regarding my second requirement, just to clarify: The second requirement is a result of using my Dates to sometimes represent entire days - where the time component doesn't matter at all - and sometimes represent a date-time (which is, IMO, the most accurate word for something that contains year/month/day and hours:minutes:seconds:...).

When a Date represents an entire day, its time parts are zero (e.g. the Date's "time component" is midnight) but the semantics dictate that the range check is done inclusively on the end date. Because I just leave this check up to Date::before and Date::after, I have to add 1 day to the end date - hence the special-casing for when the time-of-day component of a Date is zero.

Hope that didn't make things less clear.

like image 727
Matt Ball Avatar asked Sep 01 '09 16:09

Matt Ball


People also ask

Can we get timezone from date Java?

DateFormat getTimeZone() Method in Java with ExamplesThe getTimeZone() method in DateFormat class is used to return the current time-zone of the calendar of this DateFormat. Parameters: The method does not take any parameters. Return Value: The method returns timezone associated with the calendar of DateFormat.

How does Java get the time?

The getTime() method of Java Date class returns the number of milliseconds since January 1, 1970, 00:00:00 GTM which is represented by Date object. Parameters: The function does not accept any parameter. Return Value: It returns the number of milliseconds since January 1, 1970, 00:00:00 GTM.

How do I get time without date in Java?

The Date object is initialized with the current date and time when its no-arg constructor is called. To get only time (without date) in the specific format, use the SimpleDateFormat class. That's all about getting time without a date in Java.


2 Answers

Okay, I know this is a predictable answer, but... use Joda Time. That has separate representations for "a date", "an instant", "a time of day" etc. It's a richer API and a generally saner one than the built-in classes, IMO.

If this is the only bit of date/time manipulation you're interested in then it may be overkill... but if you're using the built-in date/time API for anything significant, I'd strongly recommend that you move away from it to Joda as soon as you possibly can.

As an aside, you should consider what time zone you're interested in. A Calendar has an associated time zone, but a Date doesn't (it just represents an instant in time, measured in milliseconds from the Unix epoch).

like image 83
Jon Skeet Avatar answered Sep 24 '22 23:09

Jon Skeet


Extracting the time portion of the day should be a matter of getting the remainder number of milliseconds when you divide by the number of milliseconds per day.

long MILLIS_PER_DAY = 24 * 60 * 60 * 1000;
Date now = Calendar.getInstance().getTime();
long timePortion = now.getTime() % MILLIS_PER_DAY;

Alternatively, consider using joda-time, a more fully featured time library.

like image 21
Jherico Avatar answered Sep 21 '22 23:09

Jherico