In Swing use an InputVerifier to check dates and time - java

I'm currently working on a Swing app and I've got a few JTextAreas that are going to be parsed, turned into dates and then added to a MySQL database. One is a Date field, the others are DateTime, what I'm trying to do is use InputVerifier to make sure they're entered correctly.
I've created an InputVerifier that tries to turn the text into a date useing DateFormat.parse() and that mostly works, however there are two flaws:
Firstly it can't check if I only have a Date, rather than a date and time
Secondly it can't check if I have both a date and time, rather than just one of them.
Is there any way around this? Or a better way to validate date fields in Swing I'm not aware off?

Swing has support for entering dates into text fields using a JFormattedTextField with a DateFormatter. With a DateFormatter you can specifiy a DateFormat object so can have either a date only field or a date and time field.
Have a look at How To Use Formatted Text Fields in the Swing tutorial as an introduction.

Just a thought: you could consider using a SpinnerDateModel with a JSpinner, instead of a text field. Maybe you could handle the date-only field by calling setCalendarField(Calendar.DAY) - never done it myself, though, so I'm not sure.

Related

How do i apply custom date formatting from excel in java?

I have an 2007 excel file called Test10. The first row has 10 headers, test1 test2 etc to test10. The second row, column 3 has a date, 10-10-2020 on the cell with the 10-10-2020 date there is a custom date format -> dd-mm-yyyy
The date in the cell is written as 2020-10-10.
I have the format from the cell in Java, however when I get it it's dd/-mm/-yyyy;# and the date is 2020-10-10.
I need to somehow apply that format to the date at hand.
I need something more flexible, is there a class that does that format? All I have managed to find so far is how to apply formatting to the excel in java when creating the excel, but not the other way around, when getting it from the Excel file.
I can manually serialize the format and remove the special characters that are not needed but that is a workaround.
I need a class that will apply the strange looking format to the date, in Java. Is there anything like that?
Also the example I provided above is a very simple example.
We're using a custom handler, the date comes as a double and we currently use DateUtil.getJavaDate to get the date as a string.
I have not created the handler so it's quite hard to understand the whole of it, but somewhere in the handler, the custom formatting for the cell is extracted from the excel and put into a variable, at the point where the DateUtil.getJavaDate is called I have access to the formatting variable.
Previously it was using SimpleDateFormat(CONSTANT_FORMAT).format(DateUtil.getJavaDate(d)) to format it, but I need to apply the custom format. Is there a library that parses xlsx date formats. As the dd/-mm/-yyyy;# format will not work with SimpleDateFormat.
Also I cannot serialize the format hardcoded as if I replace mm with MM how do I know if the user wanted minutes or months?
DataFormatter from org.apache.poi handles the weird excel formats

How and where to change date format of microba date picker

I am a beginner in Java programming and I am using Netbeans. I want to change the Date format of the Microba date picker to "dd-MMM-yy" and also remove the remaining fields. How to do these things?
Thank you.
Since Microba DataPicker is open source you could just clone the repo and have a look at the source code for your self.
If you have a look at the source code for the DatePicker class you will notice a number constructors which you can use to customise the picker, several take a DateFormat class which you can use to modify the format of the date value.
If you don't know how to use DateFormat, then I recommend you take a look at SimpleDateFormat, there are countless examples available to show you how to use it
As to remove elements, you'd have to look closer at the source code and make determinations on how best that might be achieved, since I'm not sure what fields you're talking about.

How can I use a global DateFormat-String with elements that interpret it differently? (JQuery datePicker, JSTL, Java)

I have a java web app, where one of the functionalities I need, is to fetch an existing date, and have the option of editing the date, by selecting another. This works really good with a JQuery DatePicker that I found, in combination with a regular input field, type text, that triggers the datepicker.
My existing date (GregorianCalendar) objects are loaded from a database, and parsed into JavaBeans, and further loaded into the jsp page, and the specific input field.
However, Java's SimpleDateFormat, JSTL functions-tag library, and DatePicker's formatDate function! does not interpret formatting styles the same way. MM in java and JSTL gives "03", while in the DatePicker, it gives "March" as output. My desired "03" is achieved by the pattern mm in DatePicker, but then it shows the objects minutes, if any, in JSTL.
I figure there are two options. Either I create two patterns which can format the different outputs to the same desired String. Or I could use datePickers formatDate-function to format my java date object upon loading of the data.
I much desire to have only one global string pattern for my datepicker, because I want to be able to change the pattern without breaking the code, or having to many dependencies. Therefor solution number 2 is what I want.
Right now, this is my code for getting the formatted date String, using tag libraries:
<fmt:formatDate type="date"
pattern="${appdata.dateFormat}"
value="${myBeanObject.gregorianCalendarObject.time}">
</fmt:formatDate>
I want something like:
$.datepicker.formatDate( ${appdata.dateFormat},
${myBeanObject.gregorianCalendarObject.time});
However, with my limited JQuery/Javascript experience, I do not know how or where to put this code, and how to invoke the function correctly. Everything I seem to find about invoking scripts seem to involve an onclick, or "ready"-functions. I merely want to get the string, while building the page.
I realise that my problem is more basic than my title, but if anyone has another idea on how to get java's and the DatePicker's formatting patterns to play along, I would be happy to hear them. If not, how can I invoke the script, and get that String I want?
Thanks in advance.
EDIT
So far I have added a small parser in my singleton applicationwide data object. When setting the dateFormat-string, it automatically parses the String to one that is matching JQuery DatePicker's mismatching interpretation. But to make this really work, I would need to add a whole lot of possible translations, other than month and year-patterns.
public void setDateFormat(String dateFormat) {
this.dateFormat = dateFormat;
datePickerFormat = dateFormat.replaceAll("yyyy", "yy");
datePickerFormat = datePickerFormat.replaceAll("M", "m");
}

How to input a date with a specific date format in struts 2.2.3.1?

I want to know if there is a way to force the input format for a date in struts, something like dd/MM/yyyy .
The date is written in a text field. I read that this is a locale sensitive subject, but it seems a too much to switch locales for a simple changing of format.
I know I could call addFieldError(fieldName, errorMessage) inside the method of my Action class but to me it feels like a date format should have been thought of to be configured in an Action-validation.xml file. I searched to configure this with properties file but found no working example.
Is using a custom StrutsTypeConverter and addFieldError(fieldName, errorMessage) the only way to do it? Or is there another version of struts in which the date format inputed in a text field can be specified?
UPDATE:
I assumed I made myself clear enough but some answers demostrate otherwise.
The use case is: User enters manually a date in an arbitrary format in a textfield, afterwards struts2 validates the date format as corresponding to the format set by the programmer or not, displaying a message accordingly.
A jQuery date picker with a <s:date> tag can be an alternative to what I want to achieve, but it it NOT what I asked. Furthermore, Javascript can be disabled, rendering the date picker useless, jQuery being a Javascript library as far as I understand it.
My action implemented ModelDriven but the date format can't be supplied anywhere to be forced in the text field, I did use my own validator, but I changed the mapping of the form element to be a String named in the action class, then used a validator with a SimpleDateFormat with the format retrieved from a resource bundle to convert it to a date, then formated the date again, to see if the representation is the same as the one inputed in the text field. Hardly after all that the format is clear. If there are other suggestions I welcome them, this seems like the only choice.
I find this drawback at the very least annoying.
If you want to have a specific format of date on your page, i will advise you to use struts 2 jquery datepicker. You can find a demo of this following this url. Go to widgets tab, select datepicker.

Problem with DateTime in Java

i have used the Swt DateTime. But I get the problem that we cannot send null value through it. it is always declared. So i used the nebulla dateChooserCombo. it's a good one and i can send the null value too. But I faced the problem that unlike the Swt DateTime It is really difficult to switch form one year to another.we have to move everymonth in it. We can explicitly enter the date through the DateChoosercombo but user may not know the date Pattern so it may be difficult for them. Is there any solution for this Problem.Please Do suggest me? is There any way we can enter Null value through swt DateTime?
Then you can add checkbox which will enable or disable the DateTime widget, and let user to decide if he/she wants to define the date or not..
but user may not know the date Pattern so it may be difficult for them
So tell them the pattern (e.g. in a Label or in the tooltip for DateChooserCombo).
is There any way we can enter Null value through swt DateTime?
No.

Categories

Resources