How can i set the min and max value in a JDateChooser? - java

I saw there's another question like this but the answer isn't useful for me since I added the JDateChooser with the design option in NetBeans, so I don't know how to set the min and max date for the pick.

Assuming JDateChooser uses an extends of the SpinnerDateModel then you just need to get the model and set the start and end date.
SpinnerDateModel model = (SpinnerDateModel) chooser.getModel();
model.setStart(startDate)
model.setEnd(endDate);

Related

Reset counter after everyday Java GUI

I am trying to make a billing menu for a software. What i am trying to do is, I want to assign a bill id to each bill using the date, say 24/11/2022 will be converted as 24112022. This will be followed by a count as: 24112022-01. I want this counter to reset whenever the date changes. I can't figure it out. Any solutions?
Here is the code I am using to get the date values and use them as my bill id
LocalDate date=LocalDate.now();
int d=date.getDayOfMonth();
int m=date.getMonthValue();
int y=date.getYear();
billId=String.valueOf(d)+String.valueOf(m)+String.valueOf(y)+counter++;
"I want this counter to reset whenever the date changes."
Think analytically (re-framing the question for this context):
a) You want a counter to reset when the date changes
b) When the date changes, you want the counter reset
c) Upon a data change, reset the counter.
So, you may want to occasionally check the time and see if it has changed (having a stored value to compare against) or overwrite an existing method that changes the day so that it would also reset the value.

Java: addToDay function

I'm trying to setup my addToDay function. I'm currently stuck on how to proceed with this or even write it correctly. The function itself will take a variable that ranges from -100 to 100. So you would basically add that variable to the current and if it was below the 0 then subtract a month or if it was above the months max day then add a month. Which i have that function setup so all i would have to do is call addToMonth with the correct amount. My problem lies within the amount of days each month has. For example, October has 31 days while November has 30. I have a function that will return the number of days in the current set month so i can call that to get how many max days should be in the current month. I'm thinking maybe a while loop would work but i just wanted to get anyone's thoughts on the best way to set it up.
I have 3 private ints: month, day, year. These are what need to be changed. I have both addTo functions for month and year setup already.
Here are some other functions i have created that can be used in this:
1. addToMonth(int delta) - changes the current month depending on the given parameter
2. getDaysInMonth() - will return the days in a month depending on the month itself
3. validateDay() - Will return true or false if the days fall outside the wanted requirements.
I don't want to use the calendar utility
I also don't want to use any other utilities. Just the base code with Junit for testing
Joda's plusDays() function and Java 8 LocalDate already has the logic that you are trying to achieve
Alright so i ended up just copying my original addToMonth function and modifying it abit to fit with days. So far it works but i do think it'll fail in the cases of different amounth of days not lining up.

Struts: Highlighting Date Range in Calendar (JQuery/Javascript)

Thank you very much in advance for reading.
I'm programming a web application in Struts.
I'm using a JQuery datepicker 1.7 as a calendar on the sidebar on the user's view. (It's the best option I could find)
I would like to highlight in the calendar a group of days starting from initial date (startDate) until the end (endDate) of several reminders I have in an array. This way, the user will be able to see on the calendar of the application, all the available days he has left in order to take action for each of his reminders.
I already have my array of reminders which I can access from the view. I was able to implement it properly thanks to guidance from a great fellow around here.
This is my javascript function for the datepicker:
$(function(){
$('#datepicker').datepicker({
flat: true,
numberOfMonths: [1,1],
dateFormat: 'dd/mm/yy',
beforeShowDay: highlightDays
});
There is another function, namely, highlightDays which is the one that is causing me trouble:
It's parameter is the array "reminders" such that reminders[i].start is the startDate, and reminders[i].end is the endDate of reminder i, with i = number of reminders in the array.
function highlightDays(reminders) {
for (var i = 0; i < reminders.length; i++) {
/** Below:
*If startDate is smaller than endDate,
*then highlight the days inbetween.
*/
if (new Date(reminders[i].start).toString() <=
new Date(reminders[i].end).toString() ){
return [true, 'ui-state-highlight'];
}
} //Otherwise do not highlight
return [true, ''];
}
The problem is the calendar won't even show up when I open the application and log in. And the Apache Log or output do not show any errors. I'd like to know, where do you think I might be wrong? I will keep investigating, but I'd really appreciate your input!
P.D: I can access the elements of the array of reminders (endDate, startDate) and print these dates on screen, so that means the reminders array is not empty.
Thank you once again for taking the time to read.
From http://jqueryui.com/demos/datepicker/#event-beforeShowDay
The function takes a date as a parameter and must return an array with [0] equal to true/false indicating whether or not this date is selectable, [1] equal to a CSS class name(s) or '' for the default presentation, and [2] an optional popup tooltip for this date. It is called for each day in the datepicker before it is displayed.
Your callback function is not accepting a single date as a parameter, it's taking an array of date ranges.
You might want to figure out how to call the highlightdays function inside the loop of remdinder values.

JSpinner SpinnerDateModel question

I'm trying to create a JSpinner to enable the user to pick a Date. I want there to be a lower date limit and an upper date limit. I also want the initial value to be the lower date limit. Unfortunately, My problem is that it won't let me use the lower limit as the initial value (the JSpinner simply becomes unresponsive). Here is my code:
SpinnerDateModel model = new SpinnerDateModel();
model.setStart(minTime); //lower limit
model.setEnd(maxTime); //upper limit
model.setValue(minTime); //doesn't like this!
model.setCalendarField(Calendar.MINUTE);
JSpinner timePicker = new JSpinner(model);
timePicker.setEditor(new JSpinner.DateEditor(timePicker, "HH:mm dd/MM/yy"));
If I set the initial value to be one minute before or after the lower limit, it works fine. But for my requirements, I do not want that.
Help?
Looks like a bug to me. I messed around with some code and it appears that whatever you pass to setValue, I'll call it value, must be at least one calendarField unit greater than minTime.
i.e. if you had used model.setCalendarField(Calendar.YEAR), value would have to be any date in 2011, assuming you used a date in 2010 for minTime.
According to Sun, the invariant enforced by the SpinnerDateModel constructors is minimum <= value <= maximum, so this problem shouldn't be happening.
The first workaround that comes to mind is creating a custom SpinnerDateModel which overrides the getPreviousValue() and setValue() methods to manually check against your desired minTime.

Zero-padding a spinner in Java

How do you add zero padding to a JSpinner?
Since the spinner creates the JFormattedTextField itself, I can't just pass the format into the JFormattedTextField constructor.
Isn't there a way to set the formatting on an existing JFormattedTextField?
What I want: value = 37, editor = "0037"
UPDATE:
I have tried this as suggested:
JSpinner mySpinner = new JSpinner();
mySpinner.setEditor(
new JSpinner.NumberEditor(mySpinner, "####"));
and the result is no change at all to the presentation of the spinner's data. It seems like a reasonable solution; has anyone tried this successfully so I can be sure it's just something flaky in my own application?
You can set the editor yourself, like this:
// minimum of four digits
mySpinner.setEditor(new JSpinner.NumberEditor(mySpinner, "0000"));
"0000" is a DecimalFormat string specifying four digits, zero-padded as necessary; "####" specifies four digits but does not zero-pad.
The DecimalFormat API documentation covers formatting strings in more detail.
Referring to the javadocs, JSpinner has a setEditor(JComponent) method. Use that to set your custom JFormattedTextField, with its custom Format.

Categories

Resources