How and where to change date format of microba date picker - java

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.

Related

How to modify text style in java (or command prompt) through code

I have made a program which takes month number and year as input and displays the calendar of that particular month of the entered year. But I want to show the dates in any colour (or bold or anything which makes it look different) which are on Sundays. How do I do that in java. I have been searching for any library or code for this but cannot get it. This is the output which I get.
I don't think that my code is required here so I am not putting it. Any help or suggestion would be appreciable.
There is a custom library named "JColor" that works on Linux, macOS, and Windows 10. It uses the ANSI codes and abstracts them using words instead of codes which is more intuitive.
Download Link: https://github.com/dialex/JColor
CODE
Choose your format, colorize it, and print it:
System.out.println(colorize("Green text on blue", GREEN_TEXT(), BLUE_BACK()));
You can also define a format once, and reuse it several times:
AnsiFormat customFormat = new AnsiFormat(RED_TEXT(), YELLOW_BACK(), BOLD());
System.out.println(colorize("Something bad happened!", customFormat));

Format date in Vaadin date picker

I'm using Vaadin Date picker 3.3.0
In Java, I set locale as the below code.
datePicker.setLocale(Locale.CANADA_FRENCH);
But the date format display is YYYY-MM-dd.
How to format it to: YYYY/MM/dd ??
Thank you!
The only way right now to influence the date format of the date picker by using the Java API is to use the locale (like you do).
There is a feature request for setting a date format manually (like datePicker.setDateFormat("YYYY/MM/dd");), maybe you want to give a thumb up on that feature request so that they will implement this soon: https://github.com/vaadin/vaadin-date-picker-flow/issues/156
Also in the comments of that feature request, there is described an alternative approach on how to set a date format manually by not using the Java API, but using JavaScript. Maybe you want to give it a try: https://github.com/vaadin/vaadin-date-picker-flow/issues/156#issuecomment-603904954

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.

In Swing use an InputVerifier to check dates and time

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.

Categories

Resources