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"));
Related
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).
Is there any way to update only a Date's time path?
I tried Date.setTime() but it replaces the date path too. I there any java method or the only way is to set hour, minute, second and milisecond?
Thank you
A Java Date is just a wrapper around a long that counts time from the epoch (January 1, 1970). Much more flexible is Calendar. You can create a Calendar from a Date:
Date date = . . .;
Calendar cal = new GregorianCalendar();
cal.setTime(date);
Then you can set various fields of the Calendar:
cal.set(Calendar.HOUR_OF_DAY, 8);
// etc.
I would start by moving away from java.util.Date entirely. Ideally, use Joda Time as it's a far more capable date/time library.
Otherwise, you should use java.util.Calendar. A java.util.Date doesn't have a particular date/time until you decide what time zone you're interested in - it just represents an instant in time, which different people around the world will consider to be a different date and time of day.
You'll want to take a look at java.util.Calender.
It will allow you to change the individual parts of the date/time.
Calendar cal = Calender.getInstance();
cal.setTime(date);
cal.set(Calender.HOUR, hour);
Alternatively, as has already being suggested, I'd take a look at Joda Time
I want to get the hour of day in 24hour cycle. I know that date.getHours() is depreciated. But I'm tempted to use it since I cant get the same result out of calendar.hour call.
It seems call calendar.setTime(now) does not pick up the current date value. Anybody knows what's going on here?
much appreciated.
Date d = new Date();
Calendar c = Calendar.getInstance();
c.setTime(d);
System.out.println("hour: "+Math.abs(c.HOUR-24)+", mins: "+Math.abs(c.MINUTE-60)+", d.h: "+d.getHours() +", d.m: "+d.getMinutes());
You're accessing the HOUR and MINUTE static property of Calendar. These are used as a sort of public static enum so that when you call Calendar.get() or Calendar.set() you can refer to a specific field. To get the actual hours contained within the calendar, you should use:
c.get(Calendar.HOUR);
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.
What's the most efficient way to remove the time portion from a Java date object using only Classes from within the JDK?
I have the following
myObject.getDate() =
{java.util.Date}"Wed May 26 23:59:00
BST 2010"
To reset the time back to 00:00:00, I'm doing the following
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
Date myDate = sdf.parse(sdf.format(myObject.getDate()));
The output is now
myDate = {java.util.Date}"Wed May 26
00:00:00 BST 2010"
Is there a better way to achieve the same result?
More verbose, but probably more efficient:
Calendar cal = GregorianCalendar.getInstance();
cal.setTime(date);
// cal.set(Calendar.HOUR, 0); // As jarnbjo pointed out this isn't enough
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
Also, you don't need to worry about locale settings, which may cause problems with string to date conversions.
If you have Apache commons, you can use DateUtils.truncate():
Date myDate = DateUtils.truncate(myObject.getDate(), Calendar.DATE)
(If you don't have access to Apache Commons, DateUtils.truncate() is implemented basically the same as kgiannakakis's answer.)
Now, if you want "clever" code that is very fast, and you don't mind using deprecated functions from java.util.Date, here is another solution. (Disclaimer: I wouldn't use this code myself. But I have tested it and it works, even on days when DST starts/ends.)
long ts = myObject.getDate().getTime() - myObject.getDate().getTimezoneOffset()*60000L;
Date myDate = new Date(ts - ts % (3600000L*24L));
myDate.setTime(myDate.getTime() + myDate.getTimezoneOffset()*60000L);
These methods are deprecated, but given a Date, you can do something like this:
Date d = ...;
d.setHours(0);
d.setMinutes(0);
d.setSeconds(0);
You should use a Calendar if possible. You'd then use the cal.set(Calendar.MINUTE, 0), etc.
It should be mentioned that if it's at all an option, you should use Joda-Time.
Related questions
Why were most java.util.Date methods deprecated?
Are you taking time zone into consideration? I see you have BST right now, but what when BST is over? Do you still wish to use BST?
Anyway, I'd suggest you have a look at DateMidnight from JodaTime and use that.
Things may vary depending on how you want to handle the time zones. But in it's simplest form, it should be as simple as:
DateMidnight d = new DateMidnight(myObject.getDate());
If you must convert back go java.util.Date:
Date myDate = d.toDate();
myDate.setTime(myDate.getTime()-myDate.getTime()%86400000)
might do the trick. Talk about quick&dirty.