With today's date, I should get the 16th date of next month.
For example, on passing 13-12-2021, I should get 16-01-2022.
I need to get the next month 16th day from current date (input date). Examples:
On passing 13-11-2021 should get 16-12-2021.
On passing 14-11-2021 should get 16-12-2021.
On passing 15-11-2021 should get 16-12-2021.
On passing 02-12-2021 should get 16-01-2022.
On passing 03-12-2021 should get 16-01-2022.
On passing 03-01-2022 should get 16-02-2022.
On passing 04-01-2022 should get 16-02-2022.
Any help will be much appreciated. Thanks.
java.time
One of the many strong points of java.time, the modern Java date and time API, is date arithmetic like this.
public static LocalDate nthDayOfFollowingMonth(
int desiredDayOfMonth, LocalDate currentDate) {
return YearMonth.from(currentDate)
.plusMonths(1)
.atDay(desiredDayOfMonth);
}
Try it out with your example date:
System.out.println(nthDayOfFollowingMonth(
16, LocalDate.of(2021, Month.DECEMBER, 13)));
Output:
2022-01-16
We might not have needed to convert to YearMonth and back to LocalDate. Doing so relieves both me and the reader of considering what happens if today’s day of month doesn’t exist in next month — for example if current date is 30 January (there is no 30 February). What one still wants to consider is what happens if you request a day of month tht doesn’t exist next month. For example on 13 January asking for the 30th of next month. We can try that out too:
System.out.println(nthDayOfFollowingMonth(
30, LocalDate.of(2022, Month.JANUARY, 13)));
I find the result very reasonable:
java.time.DateTimeException: Invalid date 'FEBRUARY 30'
Related
I am trying to get a date from its integer components: I have day, month and year as variables (that can change, I don't want to hard code them), and I want to reunite them in a date variable.
For example, something like that;
myDay: 15
myMonth: 4
myYear: 2016
`date$(myYear,myMonth,myDay) --> should return 2016.4.15 (formatted as a date).
Any way to do that?
Thank you
q)d:3
q)m:8
q)y:2016
q)"D"$"." sv string (y;m;d)
2016.08.03
See cast vs tok - need to use different arguments depending on if what you're casting from is a string or not
I'm trying to find a a pre-defined method for objects of Class Date or Class DateAndTime, that allows me to create an new Date (or a new DateAndTime) by supplying three integers: one integer for the day of the month (1-31); one for the month (1 - 12); and a four-digit integer for the year.
( The closest I've found so far is Integer>>asYear )
Is there a method that can set all three parameters at once?
If I understand you correctly, you are trying to create an instance of Date from three integers representing the day, month and year of said date.
When facing a question like this you can browse the class, Date in this case, and check its protocol for instance creation methods. In Pharo there are several methods in this category, but it is easy so see (I think) that #year:month:day looks like a good candidate. So, you can try it. Just evaluate the expression
Date year: 2015 month: 12 day: 31
and see what happens (you can inspect or print it to see the result).
You will also find #newDay:month:year as another good candidate. You could try it too. Or you could just see that it sends our previous message and thus it is just a synonymous (which is present for compatibility with other dialects that support the Smalltalk-80/ANSI specification).
String>>asDate
allows for date-formatted Strings to be converted into Dates
e.g. (in a Workspace)
aDateString := String new . " prints as '' "
aDateString := '1984-11-03' . " prints as '1984-11-03' "
aDate := Date new . " prints as 1 January 1901 "
aDate := aDateString asDate . "prints as 3 November 1984"
The comment in the String>>asDate method states that "Many allowed forms, see Date>>#readFrom:"
Date>>readFrom: says
"Read a Date from the stream in any of the forms:
<day> <month> <year> (15 April 1982; 15-APR-82; 15.4.82; 15APR82)
<month> <day> <year> (April 15, 1982; 4/15/82)
<year>-<month>-<day> (1982-04-15) (ISO8601)"
Also relevant: String>>asDateAndTime
n.b. a DateAndTime object is an exact instant in time.
Date, Year, Month, Week and Schedule are all subclasses of Class Timespan, so they all have a start moment, a duration, and an end moment.
I am new to Noda Time and I basically want to compare if a date has expired or not. In my case I have an object with the date it was created, represented by a LocalDate and the amount of months it's valid as an int, so I wanted to do a simple:
if ( Now > (dateCreated + validMonths) ) expired = true;
But I can't find in the Noda Time documentation the proper way to get the Now Date (they only show how to get the Now Time as SystemClock.Instance.Now) and the proper way to handle time comparisons.
For example if today is January 1st 2015 and the document was created in December 1st 2014, and it was valid for one month, today it expires its one month validity.
I miss methods such as isBefore() and isAfter() to compare dates and times. Simple overloads of the < > operators could also be very helpful.
EDIT:
1 - Sorry, there are < > operators to compare dates.
2 - I solve my problem using this code (not tested yet!):
...
LocalDate dateNow = this.clock.Now.InZone(DateTimeZoneProviders.Tzdb.GetSystemDefault()).LocalDateTime.Date;
LocalDate dateExpiration = DataASO.PlusMonths(validity);
return (dateNow < dateExpiration);
To get the current date, you need to specify which time zone you're in. So given a clock and a time zone, you'd use:
LocalDate today = clock.Now.InZone(zone).Date;
While you can use SystemClock.Instance, it's generally better to inject an IClock into your code, so you can test it easily.
Note that in Noda Time 2.0 this will be simpler, using ZonedClock, where it will just be:
LocalDate today = zonedClock.GetCurrentDate();
... but of course you'll need to create a ZonedClock by combining an IClock and a DateTimeZone. The fundamentals are still the same, it's just a bit more convenient if you're using the same zone in multiple places. For example:
// These are IClock extension methods...
ZonedClock zonedClock = SystemClock.Instance.InTzdbSystemDefaultZone();
// Or...
ZonedClock zonedClock = SystemClock.Instance.InZone(specificZone);
With DateJS, you'd add e.g. six months to the current date like this:
Date.today().addMonths(6);
However, I need to add 24 months not to today's date, but to a date which a user has typed into a date field. So the today() should in principle be replaced by something like this.getField('begin_date').value.
The result shall be written into another data form field.
I tried hard, but couldn't make it. Can anyone help me out?
Providing the input value is a textual representation of a date, you need to convert it into a Date object at the first place. Then you can work with it as you want.
DateJS has a pretty smart parse() function which does exactly that, so you'd achieve it like this:
Date.parse(this.getField('begin_date').value).addMonths(24)
When a specific date format is needed, like DD.MM.YYYY commonly used in Europe, you can use parseExact() and specify the format. Like this:
Date.parseExact(dateToParse, 'dd.MM.yyyy') // leading zeroes required; parses 01.04.2014 but not 1.4.2014
Date.parseExact(dateToParse, 'd.M.yyyy') // leading zeroes not required; parses both 01.04.2014 and 1.4.2014
Here is a solution that I found for my problem, using DateJS as well:
start = this.getField('begin_date').value;
var d1 = util.scand("dd.mm.yyyy", start);
var myDate = new Date(d1);
result = myDate.addMonths(24);
This works pretty fine, also spanning leap years, except for the 28th of February, 2014/2018/2022 ... ; the result then will be the 28th of February, 2016/2020/2024 ... and not the 29th of February 2016/2020/2024... In these cases it's up to the user to accept the 28th or to manually change the date to the 29th.
My problem can be easily created by the scenario below:
//create a gregorian calendar object that set the date and time as 4th June 2012 at 10:30PM
Calendar calendar = new GregorianCalendar(2012, 6, 4, 22, 30);
//when I print out these:
System.out.println(calendar.get(Calendar.DAY_OF_WEEK));
System.out.println(calendar.get(Calendar.MINUTE));
System.out.println(calendar.get(Calendar.HOUR));
System.out.println(calendar.get(Calendar.DATE));
System.out.println(calendar.get(Calendar.MONTH));
System.out.println(calendar.get(Calendar.YEAR));
//output reads as:
4
30
10
4
6
2012
//so does calendar.get(Calendar.DAY_OF_WEEK) == calendar.get(Calendar.DATE) ???
Just so that everyone is clear the 4th of June 2012 is a Monday, so shouldn't calendar.get(Calendar.DAY_OF_WEEK) return 0 as part of the first day of the week?
Thank for your all your help and concerns, please also verify the source that you are referring to.
user1442080
The month in java Calendar classes is 0-based. So June is month number 5.
You actually created an object representing July 4th, which happens to be a Wednesday, which is the fourth day of that week.
One should always look for values returned by Calendar.{field} e.g. like Calendar.SUNDAY, Calendar.MONDAY, Calendar.JANUARY, Calendar.MARCH etc. and so on.
This is because, the values returned by Calendar.{field} depends upon the TimeZone specified while creating Calendar instance.
You can try this by creating two calendar instances with different timezones:
Calendar.getInstance("BST")
and
Calendar.getInstance() // default timezone
and now try getting calendar.get(Calendar.DAY_OF_WEEK) which will return different integer values for these two instances.