I recently discovered a difference between Oracle adds months to a given date (using ADD_MONTHS function) and the way Java adds months to a Calendar object.
For instance in oracle:
select add_months('2009-02-28', +1) from dual;
produced the result: "09-03-31"
And the query:
select add_months('2009-02-28', -1) from dual;
Produces the result "09-01-31"
However in Java, the results of the same calculations (using GregorianCalendar.add() method) are (respectively):
09-03-28
and
09-01-28
Is there some way to make Oracle and Java behave the same? (e.g. some setting in oracle or some parameter in Java)?
When you do month arithmetic you need to decide, from the business point of view, what is the right way to deal with months with differing numbers of days. The flip side to the issue you raised is what happens when going from a longer month, like August, to a shorter one like February (and even from Feb to Feb if leap years are involved).
If you are happy for errors to be reported because the calculation cannot find 'Feb-30', then look at INTERVAL
You can create own DateService class like bellow and use "addMonthToDateLikeOracle" method.
package db;
import org.joda.time.LocalDate;
import java.util.Calendar;
import java.util.Date;
public class DateService {
public static Date addMonthToDateLikeOracle(Date date, int months) {
Calendar c = Calendar.getInstance();
c.setTime(date);
Date retval;
if (DateService.isLastDayOfMonth(date)) {
c.add(Calendar.MONTH, 1);
c.add(Calendar.MONTH, 1);
c.set(Calendar.DATE, 1);
c.add(Calendar.DATE, -1);
retval = c.getTime();
} else {
retval = LocalDate.fromDateFields(date).plusMonths(months).toDate();
}
return retval;
}
public static boolean isLastDayOfMonth(Date date) {
if (date == null) {
throw new RuntimeException("The date parameter cannot be null!");
}
Date endOfMonth = getEndOfMonth(date);
return endOfMonth.equals(date);
}
public static Date getEndOfMonth(Date date) {
Date startOfMonth = getStartOfMonth(date);
Date startOfNextMonth = LocalDate.fromDateFields(startOfMonth).plusMonths(1).toDate();
return LocalDate.fromDateFields(startOfNextMonth).plusDays(-1).toDate();
}
public static Date getStartOfMonth(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.DAY_OF_MONTH, 1);
return calendar.getTime();
}
}
From the Oracle reference on add_months (http://download-west.oracle.com/docs/cd/B19306_01/server.102/b14200/functions004.htm)
If date is the last day of the month or if the resulting month has fewer days than the day component of date, then the result is the last day of the resulting month. Otherwise, the result has the same day component as date.
That means you're going to have to write a method for Java that performs the same check to get the same results (or a function in PL/SQL that behaves the same as Java).
You could write your own add months function in java.
public Date functionAddMonth(Date d, int month)
{
Calendar c = Calendar.getInstance();
c.setTime(d);
c.add(Calendar.DAY, 1);
c.add(Calendar.MONTH, month);
c.add(Calendar.DAY, -1);
return c.getTime();
}
Related
We have a requirement to Round by next quarter minutes in Java code, for example:
if current date time is 2020-05-28T10:01:00
then, round up to next quarter to make it 2020-05-28T10:15:00
if current date time is 2020-05-28T10:15:01
then, round up to next quarter to make it 2020-05-28T10:30:00
if current date time is 2020-05-28T10:46:15
then, round up to next quarter to make it 2020-05-28T11:00:00
if current date time is 2020-12-31T23:47:00
then, round up to next quarter to make it 2021-01-01T00:00:00
Can someone please provide Java code to achieve this. Any help is appreciated.
Tried below code but unable to get the output which I'm looking for:
import java.util.Calendar;
import java.util.Date;
public class Main {
public static void main(String[] args) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
int round = calendar.get(Calendar.MINUTE) % 15;
calendar.add(Calendar.MINUTE, round < 8 ? -round : (15-round));
calendar.set( Calendar.SECOND, 0 );
System.out.println(calendar.getTime());
}
}
This can be easily done using the LocalDateTime class from the java.time package, which you should be using anyway1.
public static LocalDateTime roundUpToQuarter(LocalDateTime datetime) {
int minutesToAdd = 15 - (datetime.getMinute() % 15);
return datetime
.plusMinutes(minutesToAdd)
.truncatedTo(ChronoUnit.MINUTES);
}
This calculation involving the modulus operator makes sure that we get the number of minutes to add to go to the next full quarter. The truncatedTo(ChronoUnit.MINUTES) call makes in turn sure that all fields smaller than 'minutes' are set to zero.
Update: I'm not certain of your exact use case, but, as Basil pointed out, if you want to represent a moment on the timeline, then you also need a timezone or offset. This can be done by replacing LocalDateTime instances with ZonedDateTime in the abovementioned method.
1 The Date, Calendar and SimpleDateFormat classes are obsolete. They may work in simple cases, but they will cause trouble in more complex cases. See What's wrong with Java Date & Time API?.
You can try below code.
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date(1609438620000l));
int fraction = calendar.get(Calendar.MINUTE) / 15;
switch (fraction) {
case 0: {
calendar.set(Calendar.MINUTE, 0);
break;
}
case 1: {
calendar.set(Calendar.MINUTE, 15);
break;
}
case 2: {
calendar.set(Calendar.MINUTE, 30);
break;
}
default: {
calendar.set(Calendar.MINUTE, 45);
break;
}
}
calendar.add(Calendar.MINUTE, 15);
calendar.set(Calendar.SECOND, 0);
System.out.println(calendar.getTime());
I am using the below code to retrieve the last day in the previous month - Ex: May. But it is returning 30 days instead of 31.
The code given below
package net.vcmg.date;
import java.util.Calendar;
import java.util.Date;
import org.apache.commons.lang.time.DateUtils;
public class LastDayPreviousMonth {
public static void main(String[] args) {
Date lastDateOfPreviousMonth = addMonths(lastDayOfTheMonth(today()), -1);
System.out.println("lastDateOfPreviousMonth: "+lastDateOfPreviousMonth);
}
//the below method is from Utils.java
public static Date lastDayOfTheMonth(Date d) {
Calendar cal = Calendar.getInstance();
cal.setTime(d);
int actualMax = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
cal.set(Calendar.DAY_OF_MONTH, actualMax);
return cal.getTime();
}
public static Date addMonths(Date date, int numMonths)
{
return DateUtils.addMonths(date, numMonths);
}
public static Date today()
{
return truncDate(now());
}
public static Date now()
{
// will cut-off milliseconds
return new Date( (System.currentTimeMillis()/1000) * 1000);
}
public static Date truncDate (Date date) {
return DateUtils.truncate(date, Calendar.DATE);
}
}
Here, when i call the lastDateOfPreviousMonth in the main method, it is returning 30 days alone. Not the 31 , May contains 31 days actually. Please help.
Java 8
If you are not constraint to use the old Date it will be better to use the new java.time.LocalDate
LocalDate previousMonth = LocalDate.now().minusMonths(1);
LocalDate start = previousMonth.withDayOfMonth(1);
LocalDate end = previousMonth.withDayOfMonth(previousMonth.lengthOfMonth());
System.out.println(start);
System.out.println(end);
Output
2019-05-01
2019-05-31
Edit
For your implementation, change the order of methods
addMonths - get the current date and provide the previous month addMonths(new Date(), -1)
lastDayOfTheMonth - get the last day of the previous month lastDayOfTheMonth(addMonths(new Date(), -1))
Date lastDateOfPreviousMonth = lastDayOfTheMonth(addMonths(new Date(), -1));
Output
lastDateOfPreviousMonth: Fri May 31 10:46:13 EEST 2019
Try this:
public static void main(String[] args) {
Date lastDateOfPreviousMonth = lastDayOfTheMonth(addMonths(today(), -1));
System.out.println("lastDateOfPreviousMonth: " + lastDateOfPreviousMonth);
}
When you call lastDayOfTheMonth for today() day will be 30. And after minus one month result expected will be 30, not 31.
It’s a logical error in the way you have thought out your program/algorithm. You are first finding the last day of the month, in this case June 30. You are then subtracting 1 month. That gives May 30 regardless of the fact that there are 31 days in May (it’s not explicit from the documentation of DateUtils.addMonths that it works this way, but it uses the poorly designed and outdated Calendar class internally, so this is what we should expect).
Instead do things in the opposite order. First find the previous month:
YearMonth lastMonth = YearMonth.now(ZoneId.of("Asia/Kolkata")).minusMonths(1);
2019-05
Since the new month doesn’t begin at the same point in time in all time zones, I recommend that you state your desired time zone as shown.
Only then find the last day of the month:
LocalDate lastDayOfLastMonth = lastMonth.atEndOfMonth();
2019-05-31
Avoid Date and Calendar
I recommend you don’t use Date and Calendar. Those classes are poorly designed and long outdated. Instead use LocalDate and other classes from java.time, the modern Java date and time API. This will also save you from the external dependency on Apache DateUtils since its functionality is generally built into the modern classes.
Links
Documentation of org.apache.commons.lang3.time.DateUtils.addMonths
Oracle tutorial: Date Time explaining how to use java.time.
This question already has answers here:
Get first Monday after certain date?
(5 answers)
Closed 6 years ago.
Hello at first i would like to note that i have found several posts with the same question here, but NONE of them worked for me. i am creating alarm clock application for android and the last thing i need is: get the date of the nearest certain day in week.
I have found several algorithms here and i will also copy one here :
import java.util.Calendar;
public class NextWednesday {
public static Calendar nextDayOfWeek(int dow) {
Calendar date = Calendar.getInstance();
int diff = dow - date.get(Calendar.DAY_OF_WEEK);
if (!(diff > 0)) {
diff += 7;
}
date.add(Calendar.DAY_OF_MONTH, diff);
return date;
}
public static void main(String[] args) {
System.out.printf(
"%ta, %<tb %<te, %<tY",
nextDayOfWeek(Calendar.WEDNESDAY) // this can contain any of the 7 days in week
);
}
}
Today is tuesday in my country
If i put wednesday in the function it returns me the wednesday that is in the next week, but thats not correct.
This algorithm automatically looks at the following week no matter if its just monday and theres whole week before you, it jumps to the next week and does its job but thats not correct, i need to implement the same behaviour but it must start from today.
Example: Today is Monday, i am looking for wednesday
Correct output: Date of wednesday in this week.
Uncorrect output: Date of wednesday in the next week.
I hope its clear enough.
Okay, algorithm works correctly, i made a simple mistake, i was passing wrong day to the function, i passed the current day, not the one that was chosen by the user
If you wish to keep it simple.
public Date getNextDate(int dayOfWeek) {
Calendar c = Calendar.getInstance();
for ( int i = 0; i < 7; i++ ) {
if ( c.get(Calendar.DAY_OF_WEEK) == dayOfWeek ) {
return c.getTime();
} else {
c.add(Calendar.DAY_OF_WEEK, 1);
}
}
return c.getTime();
}
I am trying to subtract days from the current date using the java.util.Calendar object. My problem here is the days to subtract can be positive or negative. My code is as follows
public class Test {
public static void main(String[] args) {
int pastValidationDays=2;
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DAY_OF_MONTH, - pastValidationDays);
}
}
As per the above code if the date is 20/1/2015 it will give me 18/1/2015
Now say if the pastValidationDays= -2(negative value) then also it should subtract from the current date. As per the above code if i say
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DAY_OF_MONTH, - pastValidationDays);
then it is adding up the days instead of subtracting. Say if the current date 20/1/2015 it is giving me 22/1/2015. But in this case as well i need the date as 18/1/2015.
One of the way i am doing is as below
if (pastValidationDays < 0){
calendar.add(Calendar.DAY_OF_MONTH, pastValidationDays);
}else{
calendar.add(Calendar.DAY_OF_MONTH, -pastValidationDays);
}
Is this a good approach or can it be done this way
calendar.add(Calendar.DAY_OF_MONTH, - Math.abs(pastValidationDays));
I want to subtract the days using calendar object only. I do not want to use JODA time and other objects. Please suggest other approaches if any. Thanks in advance
This question already has answers here:
How do I check if a date is within a certain range?
(17 answers)
Closed 7 years ago.
One thing I want to know is how to calculate what date will it be 10 days from today.
Second thing is to check if one Date is between two other Dates.
For example, let's say I have an app that shows what events I need to do in the next 10 days (planner). Now how can I see if the date I assigned to an event is between today and the date that is 10 days from today?
Manipulating and comparing dates using java.util.Date and java.util.Calendar is pretty a pain, that's why JodaTime exist. None of the answers as far have covered the time in question. The comparisons may fail when the dates have a non-zero time. It's also unclear whether you want an inclusive or exclusive comparison. Most of the answers posted so far suggest exclusive comparision (i.e. May 24 is not between May 20 and May 24) while in real it would make more sense to make it inclusive (i.e. May 24 is between May 20 and May 24).
One thing I want to know is how to calculate what date will it be 10 days from today.
With the standard Java SE 6 API, you need java.util.Calendar for this.
Calendar plus10days = Calendar.getInstance();
plus10days.add(Calendar.DAY_OF_YEAR, 10);
With JodaTime you would do like this:
DateTime plus10days = new DateTime().plusDays(10);
Second thing is to check if one Date is between two other Dates. For example, let's say I have an app that shows what events I need to do in the next 10 days (planner). Now how can I see if the date I assigned to an event is between today and the date that is 10 days from today?
Now comes the terrible part with Calendar. Let's prepare first:
Calendar now = Calendar.getInstance();
Calendar plus10days = Calendar.getInstance();
plus10days.add(Calendar.DAY_OF_YEAR, 10);
Calendar event = Calendar.getInstance();
event.set(year, month - 1, day); // Or setTime(date);
To compare reliably using Calendar#before() and Calendar#after(), we need to get rid of the time first. Imagine it's currently 24 May 2010 at 9.00 AM and that the event's date is set to 24 May 2010 without time. When you want inclusive comparison, you would like to make it return true at the same day. I.e. both the (event.equals(now) || event.after(now)) or -shorter but equally- (!event.before(now)) should return true. But actually none does that due to the presence of the time in now. You need to clear the time in all calendar instances first like follows:
calendar.clear(Calendar.HOUR);
calendar.clear(Calendar.HOUR_OF_DAY);
calendar.clear(Calendar.MINUTE);
calendar.clear(Calendar.SECOND);
calendar.clear(Calendar.MILLISECOND);
Alternatively you can also compare on day/month/year only.
if (event.get(Calendar.YEAR) >= now.get(Calendar.YEAR)
&& event.get(Calendar.MONTH) >= now.get(Calendar.MONTH)
&& event.get(Calendar.DAY_OF_MONTH) >= now.get(Calendar.DAY_OF_MONTH)
{
// event is equal or after today.
}
Very verbose all.
With JodaTime you can just use DateTime#toLocalDate() to get the date part only:
LocalDate now = new DateTime().toLocalDate();
LocalDate plus10days = now.plusDays(10);
LocalDate event = new DateTime(year, month, day, 0, 0, 0, 0).toLocalDate();
if (!event.isBefore(now) && !event.isAfter(plus10days)) {
// Event is between now and 10 days (inclusive).
}
Yes, the above is really all you need to do.
public static boolean between(Date date, Date dateStart, Date dateEnd) {
if (date != null && dateStart != null && dateEnd != null) {
if (date.after(dateStart) && date.before(dateEnd)) {
return true;
}
else {
return false;
}
}
return false;
}
EDIT: Another suggested variant:
public Boolean checkDate(Date startDate, Date endDate, Date checkDate) {
Interval interval = new Interval(new DateTime(startDate),
new DateTime(endDate));
return interval.contains(new DateTime(checkDate));
}
Use JodaTime calendar replacement classes: http://joda-time.sourceforge.net/
You can use before, after and compareTo methods of Date class.
Here're some examples
http://www.roseindia.net/java/example/java/util/CompareDate.shtml
http://www.javafaq.nu/java-example-code-287.html
http://www.esus.com/javaindex/j2se/jdk1.2/javautil/dates/comparingdates.html
And here's API on Date class
http://java.sun.com/j2se/1.4.2/docs/api/java/util/Date.html
Good Luck!
To add ten days:
Date today = new Date();
Calendar cal = new GregorianCalendar();
cal.setTime(today);
cal.add(Calendar.DAY_OF_YEAR, 10);
To check if between two dates:
myDate.after(firstDate) && myDate.before(lastDate);
To check if date is between two dates, here is simple program:
public static void main(String[] args) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
String oeStartDateStr = "04/01/";
String oeEndDateStr = "11/14/";
Calendar cal = Calendar.getInstance();
Integer year = cal.get(Calendar.YEAR);
oeStartDateStr = oeStartDateStr.concat(year.toString());
oeEndDateStr = oeEndDateStr.concat(year.toString());
Date startDate = sdf.parse(oeStartDateStr);
Date endDate = sdf.parse(oeEndDateStr);
Date d = new Date();
String currDt = sdf.format(d);
if((d.after(startDate) && (d.before(endDate))) || (currDt.equals(sdf.format(startDate)) ||currDt.equals(sdf.format(endDate)))){
System.out.println("Date is between 1st april to 14th nov...");
}
else{
System.out.println("Date is not between 1st april to 14th nov...");
}
}
I took the initial answer and modified it a bit. I consider if the dates are equal to be "inside"..
private static boolean between(Date date, Date dateStart, Date dateEnd) {
if (date != null && dateStart != null && dateEnd != null) {
return (dateEqualOrAfter(date, dateStart) && dateEqualOrBefore(date, dateEnd));
}
return false;
}
private static boolean dateEqualOrAfter(Date dateInQuestion, Date date2)
{
if (dateInQuestion.equals(date2))
return true;
return (dateInQuestion.after(date2));
}
private static boolean dateEqualOrBefore(Date dateInQuestion, Date date2)
{
if (dateInQuestion.equals(date2))
return true;
return (dateInQuestion.before(date2));
}