Codename One PickerComponent setStartDate and setEndDate usage - codenameone

I tried the following code to set the startDate and endDate of a PickerComponent:
CSS:
#Constants {
includeNativeBool: true;
lightweightPickerBool: true; /* It's necessary to use the "setStartDate" and "setEndDate" of the Picker */
}
MainClass:
Form f = new Form(new TextModeLayout(1, 1));
int MINAGE = 13;
Calendar minAge = Calendar.getInstance(); // it's not a singleton, https://stackoverflow.com/questions/6055112/why-can-i-have-only-one-instance-of-calendar-object
minAge.setTime(new Date()); // current time
int year = minAge.get(Calendar.YEAR) - MINAGE;
minAge.set(Calendar.YEAR, year);
Date endDate = minAge.getTime();
Calendar maxAge = Calendar.getInstance();
maxAge.setTime(new Date());
maxAge.set(Calendar.YEAR, 1930);
maxAge.set(Calendar.MONTH, 1);
maxAge.set(Calendar.DAY_OF_MONTH, 1);
Date startDate = maxAge.getTime();
PickerComponent inputCmp = PickerComponent.createDate(endDate).label("Birthday");
Log.p("startDate: " + startDate.toString());
Log.p("endDate: " + endDate.toString());
inputCmp.getPicker().setStartDate(startDate);
inputCmp.getPicker().setEndDate(endDate);
f.add(inputCmp);
f.show();
but when I try to select the date in the picker, I get:
[EDT] 0:0:0,293 - startDate: Sat Feb 01 12:30:58 CET 1930
[EDT] 0:0:0,307 - endDate: Fri Dec 02 12:30:58 CET 2005
[EDT] 0:0:2,227 - Exception: java.lang.ArrayIndexOutOfBoundsException - Index out of bounds
java.lang.ArrayIndexOutOfBoundsException: Index out of bounds
at com.codename1.ui.spinner.SpinnerNode.setSelectedIndex(SpinnerNode.java:284)
at com.codename1.ui.spinner.SpinnerNode.rebuildChildren(SpinnerNode.java:213)
at com.codename1.ui.spinner.SpinnerNode.setListModel(SpinnerNode.java:227)
at com.codename1.ui.spinner.Spinner3D.setModel(Spinner3D.java:207)
at com.codename1.ui.spinner.DateSpinner3D.setEndYear(DateSpinner3D.java:340)
at com.codename1.ui.spinner.DateSpinner3D.setDateRange(DateSpinner3D.java:216)
at com.codename1.ui.spinner.Picker$1.createDatePicker3D(Picker.java:490)
at com.codename1.ui.spinner.Picker$1.showInteractionDialog(Picker.java:605)
at com.codename1.ui.spinner.Picker$1.actionPerformed(Picker.java:303)
at com.codename1.ui.util.EventDispatcher.fireActionEvent(EventDispatcher.java:349)
at com.codename1.ui.Button.fireActionEvent(Button.java:570)
at com.codename1.ui.Button.released(Button.java:604)
at com.codename1.ui.Button.pointerReleased(Button.java:708)
at com.codename1.ui.Form.pointerReleased(Form.java:3339)
at com.codename1.ui.Component.pointerReleased(Component.java:4528)
at com.codename1.ui.Display.handleEvent(Display.java:2079)
at com.codename1.ui.Display.edtLoopImpl(Display.java:1051)
at com.codename1.ui.Display.mainEDTLoop(Display.java:969)
at com.codename1.ui.RunnableWrapper.run(RunnableWrapper.java:120)
at com.codename1.impl.CodenameOneThread.run(CodenameOneThread.java:176)
Tested in the Simulator.

This is a bug in Picker I asked a user in the past to submit an issue on this but it seems that he didn't. It seems the default behavior of picker is to show 1970 as the oldest date. This is obviously problematic and should be addressed.

Related

get Dates In Range in rescript and Daylight Saving Time

i have a calendar in my site which take a start date and end date and pass them into a function who calculates the dates between .
lets sat we have the start date Mon Mar 29 2021 03:00:00 GMT+0300 (Eastern European Summer Time) and the end date is Mon Apr 05 2021 03:00:00 GMT+0300 (Eastern European Summer Time) ; this function should return ["30/3/2021","31/3/2021","1/4/2020","2/4/2020","3/4/2020","4/4/2020"]
let getDatesInRange = (start, end) => {
let dates = ref([])
let current = ref(start)
while current.contents <= end {
dates := dates.contents->Js.Array2.concat([current.contents->toUTCDateString])
current := {
let date = current.contents->toUTCDateString->Js.Date.fromString
date->Js.Date.setDate(date->Js.Date.getDate +. 1.0)->ignore
date
}
}
dates.contents
}
and this is toUTCDateString function which take a date and give the string version of it
let toUTCDateString = date => {
let date = date->External.unSafeCastToJsObject
date["toISOString"]()["split"]("T")[0]
}
These functions where working fine until The time has changed for Daylight Saving Time; we gain an hour so the day stuck there in for some reason
Any body face this issue before and who to deal with such time issues ?

Apache Flink -TumblingProcessingTimeWindows - Incorrect calculation Start-End

it's very simple example:env.keyBy(value -> (...)) .window(TumblingProcessingTimeWindows.of(Time.hours(24))).addSink();
................
public Collection<TimeWindow> assignWindows(){
final long now = context.getCurrentProcessingTime();
long start = TimeWindow.getWindowStartWithOffset(now, offset, size);
// the value "now" is correct = 1603379120043 (Date in your timezone*: 10/22/2020, 12:05:20 PM GMT-0300 (-03))
// the value "start" is 1603324800000 (Date in your timezone*: 10/21/2020, 9:00:00 PM GMT-0300 (-03) : ???!!!!!
// I should started yesterday ???
// As the result:
public TimeWindow(long start, long end) {
this.start = start; //1603324800000 - Date in your timezone*: 10/21/2020, 9:00:00 PM GMT-0300 (-03)
this.end = end; //1603411200000 - Date in your timezone*: 10/22/2020, 9:00:00 PM GMT-0300 (-03)}
So, my job with 24h TumblingProcessingTimeWindows starting now at 10/22/2020, 12:05:20 PM will be finished today at 9:00:00 PM === 9 hours instead of 24 hours
Some solutions, please ?
By default, Flink's windows are aligned to the epoch, not to the time when they are created. So a 24 hour window will end at midnight UTC.
You can use the optional offset parameter to shift the window boundaries.

Convert first day of month to Last day of month in Angular JS

I am very new to angular JS but since morning struggling with this.
I have a datepicker with "MM/yyyy" format, the date value returned here is first day of month.
i.e. February 1, 2017 but i want the date as February 28, 2017 i.e last day of month.
Just to update i am using moment function.
Please suggest some work around for the same!
I infer from your question that you are using momentjs.
This lib provides you with a built in function endof
const date = new Date(2017, 1) // 1st Feb
moment(date).endOf('month');
This should handle most cases directly including leap years
If you have a JavaScript Date instance d, you can simply use
const d = new Date(2017, 1) // 1st Feb
d.setMonth(d.getMonth() + 1)
d.setDate(d.getDate() - 1)
console.info(d.toLocaleString())
Now d will be the last day of the month.
Note: this easily handles year boundaries without any extra code. For example
const d = new Date(2017, 11) // 1st Dec
console.info('Before', d.toLocaleString())
d.setMonth(d.getMonth() + 1)
d.setDate(d.getDate() - 1)
console.info('After', d.toLocaleString())
You just need add 1 month to the date, and then substract 1 day. Here's an example:
// Let's suppose the date selected from the picker was February 1st, 2017
// Remember months in JS are zero-index based, so 0=Jan, 1=Feb, etc.
var selectedDate = new Date(2017, 1, 1);
var month = selectedDate.getMonth();
var year = selectedDate.getFullYear();
month ++;
if(month > 11){ // Last month number is 11 (December)
month = 0; // January
year ++;
}
var oneMonthAheadDate = new Date(year, month, 1);
var lastDayOfSelectedMonthDate = new Date(oneMonthAheadDate.getTime() - (1000 * 3600 * 24)); // We substract 1 day (1000 ms x 3600 secs in an hour x 24 hours in a day)
Your needed date will be in lastDayOfSelectedMonthDate

Angularjs Django compare dates

I am fetching the date from my Django backend which comes like this: 2016-03-31
In my angular controller I want to compare it with today's date and if they are similar I want to deactivate a button.
I tried new Date() which gives me something like Thu Mar 31 2016 08:59:01 GMT+0200 (W. Europe Daylight Time)
How can these two dates be compared to achieve my goal?
ehhhhh... i think currently we could only do a manual formatting
see this one: How to format a JavaScript date
For your reference, below is what i did though:
$scope.formatDate = function(date){
var newDate = new Date(date);
var year = newDate.getFullYear();
var month = (newDate.getMonth() + 1).toString(); //add 1 as Jan is '0'
var day = newDate.getDate().toString();
month = month.length > 1? month: '0'+month;
day = day.length > 1? day: '0'+day;
$scope.date = day + '/' + month + '/' + year;
}
If you want to compare the date with now you can do the following:
var now = new Date().getTime();
var compareDate = new Date('2016-03-31').getTime();
if(now > compareDate){
//greater
}else if(now < compareDate){
//less
}else{
//equal
}
Just add what you need to the scope and then you could do something like:
ng-disabled="compareDate === today"

Jasmine date expect always returning false

I have a test written in Jasmine test runner:
it("Expect 'due date' to be 14 days from today", function () {
var dateNow = new Date();
scope.dateOfService(dateNow);
expect(scope.DueDate == new Date(dateNow.setDate(dateNow.getDate() + 14))).toBeTruthy();
});
However this always returns false? Is there something I'm doing incorrect?
To 'debug' this I also ran:
expect(scope.DueDate).toBe(new Date(dateNow.setDate(dateNow.getDate() + 14)));
And this returns:
Expected Date(Tue Nov 11 2014 08:20:23 GMT+0000 (GMT Standard Time)) to be Date(Tue Nov 11 2014 08:20:23 GMT+0000 (GMT Standard Time))
var dateNow = new Date();
scope.dateOfService(dateNow);
var dateReturned = new Date(scope.DueDate);
var dateAdd = new Date(dateNow.setDate(dateNow.getDate() + 14));
expect(dateReturned).toEqual(dateAdd);
The main point was to ensure it was .toEqual rather than .toBe.

Resources