java - how to compare same days in date - java

I have a long data member that represents a date.
I cast it to a
Date d = new Date(long);
I want to now if a nother date has the same day.
How do I do it?
Thanks.
(For andrew)
Edit :
Found this solution
SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMdd");
return fmt.format(date1).equals(fmt.format(date2));
in here
Comparing two java.util.Dates to see if they are in the same day
looks nice

use the joda api.
http://joda-time.sourceforge.net/
Its a lot easier and better than the Calendar object route in java jdk

Well you can convert them both to calendar Objects and get the calendar objects day and compare that way.
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(LONG VALUE HERE);
int day = cal.get(Calendar.DAY_OF_MONTH);
Do the same thing with the other date, and compare the values.
edit: By the way, you are not casting the long to a date, you are just creating a Date object using a long.

Use apache commons.
DateUtils.isSameDay(date1, date2);

To see if the dates are equal:
date_one.equals(date_two);
To see if just the day is equal, I usually chop the time off the date (setHours(0), setMinutes(0), etc.) and then use the .equals() method.

Use java.util.Calendar for all comparison operations.

Related

Subtract one day in XMLGregorianCalendar

How to subtract one day in XMLGregorianCalendar?
Also while subtracting how to cope up with following problems :
it does not goes to a negative value in case of first day of the month
in case of 1st Jan of a year, where it needs to go back to a previus year
and other similar stuffs.
Please do not suggest to use any other library like Joda-Time. I know they are great, but I need to get this done using XMLGregorianCalendar only.
Thanks
Just convert to a normal GregorianCalendar, do the arithmetic there, then convert back:
GregorianCalendar calendar = xmlCalendar.toGregorianCalendar();
calendar.add(Calendar.DAY_OF_MONTH, -1);
xmlCalendar = datatypeFactory.newXMLGregorianCalendar(calendar);
(This assumes you already have a DatatypeFactory of course. You can always call DatatypeFactory.newInstance() if necessary.)
XMLGregorianCalendar has an add(Duration) method which you could also use for this computation without needing to convert to a GregorianCalendar first. Here's an example:
DatatypeFactory df = DatatypeFactory.newInstance();
XMLGregorianCalendar calendar1 = ...;
XMLGregorianCalendar calendar2 = (XMLGregorianCalendar) calendar1.clone();
calendar2.add(df.newDuration("-P1D"));

JAVA comparing just Times (getTime function)

I have REPORTDATE field where I want to take the value just of the hours and minutes.
reportdate.getTime() returns this for example: 1439967368798
I want to compare if TIME from reportdate (hours and minutes) are between:
if reportdate>=06:45 and reportdate<13:45 then a=1;
if reportdate>=13:45 and reportdate<20:45 then a=2;
if reportdate>=20:45 and reportdate<06:45 then a=3;
I did not succeed to find some instructions on web.
Should I try with this by using the value that I am getting from getTime (1439967368798) or with some other method?
I assume that reportDate is an instance of Date. The getTime() function returns the underlying long of the Date, thats why you see this long number - miliseconds since January 1, 1970, 00:00:00 GMT.
What you need to do is convert the date to a calendar and then use the get() Method with the appropriate field constant.
This information is for Java 7 without any libraries. It would be wise to use a library like JodaTime or the Java8 Time API.
Here is an example how to do that.
Like this?
Date date = new Date();
SimpleDateFormat format = new SimpleDateFormat("HH:mm");
if (date.after(format.parse("06:45")) && date.before(format.parse("13:45"))) {
a=1;
}

Iterate through date ranges without using libraries - Java

Hi I want to iterate through a date range without using any libraries. I want to start on 18/01/2005(want to format it to yyyy/M/d) and iterate in day intervals until the current date. I have formatted the start date, but I dont know how I can add it to a calendar object and iterate. I was wondering if anyone can help. Thanks
String newstr = "2005/01/18";
SimpleDateFormat format1 = new SimpleDateFormat("yyyy/M/d");
Date date = format1.parse(newstr);
Calendar calendar = new GregorianCalendar();
calendar.setTime(date);
while (someCondition(calendar)) {
doSomethingWithTheCalendar(calendar);
calendar.add(Calendar.DATE, 1);
}
Use SimpleDateFormat to parse a string into a Date object or format a Date object into a string.
Use class Calendar for date arithmetic. It has an add method to advance the calendar, for example with a day.
See the API documentation of the classes mentioned above.
Alternatively, use the Joda Time library, which makes these things easier. (The Date and Calendar classes in the standard Java API have a number of design issues and are not as powerful as Joda Time).

Java checking time in a timestamp

Currently I have a timestamp for example say, 2012-06-23 14:24:07.975 and
two times - 8AM and 3PM.
In java, how can I check whether the above timestamp is between the particular two time.
In other words, I need to check whether the time in timestamp (2012-06-23 14:24:07.975) falls between 8AM and 3PM or not.
Any suggestions.
You can convert the timestamp into an actual Date afterwards you convert it into a Calendar from where you can extract the hour and check it against your given hours.
Use SimpleDateFormat for getting a Date out of your Timestamp, if you cannot directly convert it.. Afterwards
Calendar cal = new GregorianCalendar();
cal.setTime(yourDate);
int hour = cal.get(Calendar.HOUR_OF_DAY);
Now do your hour check.
before and after methods should help you to do that. See docs.
I suggest using compareTo method in Date.

Construct Date - An efficient way

May I know what is the most efficient way to construct a date object using a specific day, month, and year.
Date(int year, int month, int day)
This construct is depreciated. Hence, what I usually do is:
Calendar calendar = Calendar.getInstance();
Date date = calendar.set(year, month, date).getTime();
However, my understanding is that Calendar.getInstance() is rather expensive. What is the most efficient way to construct a Date object? Or should I just use Date(int year, int month, int day) quietly without telling the rest?
Please don't suggest using any third-party library.
With this you can avoid the innecesary "now time" instance creation.
Date coolDate = new GregorianCalendar(year, month, day).getTime();
You can check GregorianCalendar javadoc for other constructors. You have date+time, and timezone.
Anyway I agree with Jon Skeet that it's not so expensive. I agree with you that code doesn't need a default "now" initialization.
"Rather expensive" is somewhat vague. Have you actually tried using the code you've supplied, measured it and found it to be too expensive? Do you have a concrete idea of how cheap you need this operation to be?
Also, you haven't specified which time zone you want the value in the Date to represent. UTC? The default time zone? What time of day do you want it to be - midnight, or the current time of day? Your current code will keep the existing time of day - is that really what you want?
(As I mentioned in a comment, I would strongly suggest you move to Joda Time - but even if you don't, you should still check whether or not you've actually got a problem with your existing code before looking for a solution.)
I would simply do this:
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date thisDate = formatter.parse("2010-03-04");
It's pretty efficient from a lines of code standpoint; I can't speak to its runtime efficiency vis a vis Calendar.
Ten years later in 2020: the only right answer is to use classes in java.util.time package.
LocalDate localDate = LocalDate.now();
LocalDate.of(2020, 3, 7);
LocalDate.parse("2020-03-07");
Whatever you use, as long as it's in the Java Standard API, it will involve the use of Calendar (both the Date constructor and SimpleDateFormat use it internally), so there's no point fretting about that class's supposed inefficiency.

Categories

Resources