DatePickerCellEditor format changing when selected - java

I have created JTable having DatePickerCellEditor with DateFormat(dd/MM/yyyy).
The DateFormat is changing when date is selected.
like this :
here's my code :
DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
final DatePickerCellEditor dateCellPrev = new DatePickerCellEditor(formatter);
dateCellPrev.addCellEditorListener(new CellEditorListener() {
#Override
public void editingStopped(ChangeEvent arg0) {
dateCellPrev.setFormats(formatter);
}
#Override
public void editingCanceled(ChangeEvent arg0) {
dateCellPrev.setFormats(formatter);
}
});
table.getColumnModel().getColumn(19).setCellEditor(dateCellPrev);
I want to change format to "dd/MM/yyyy" not like format in last picture

Related

How to highlight dates inside JCalendar in JDateChooser?

I am using a JDateChooser component from an external library toedter(jcalendar1.4). I used a custom DateEvaluator by implementing IDateEvaluator which is later added to JDateChooser component:
public static class HighlightEvaluator implements IDateEvaluator {
private final List<Date> list = new ArrayList<>();
public void add(Date date) {
list.add(date);
}
public void remove(Date date) {
list.remove(date);
}
public void setDates(List<Date> dates) {
list.addAll(dates);
}
#Override
public Color getInvalidBackroundColor() {
return Color.BLACK;
}
#Override
public Color getInvalidForegroundColor() {
return null;
}
#Override
public String getInvalidTooltip() {
return null;
}
#Override
public Color getSpecialBackroundColor() {
return Color.GREEN;
}
#Override
public Color getSpecialForegroundColor() {
return Color.RED;
}
#Override
public String getSpecialTooltip() {
return "filled";
}
#Override
public boolean isInvalid(Date date) {
return false;
}
#Override
public boolean isSpecial(Date date) {
return list.contains(date);
}
}
and here is my main method:
public static void main(){
JDateChooser dateChooser = new JDateChooser();
List<Date> dates = new ArrayList<Date>();
dates.add(new Date());
HighlightEvaluator evaluator = new HighlightEvaluator();
evaluator.setDates(dates);
dateChooser.getJCalendar().getDayChooser().addDateEvaluator(evaluator);
}
Now the current date is supposed to be highlighted by the code but its not getting highlighted. Please tell a fix for this
You need to alter the Date variable a little bit. When you create a Date variable it grabs the current day month year along with time(hours, minutes, seconds, milliseconds)
However setting the same date to evaluator will not work because evaluator expects date to be devoid of the time details. So alternatively you can use Calendar object in your main function as illustrated below:
public static void main(){
JDateChooser dateChooser = new JDateChooser();
List<Date> dates = new ArrayList<Date>();
Calendar c = Calendar.getInstance();
c.set(Calendar.YEAR, desiredyear);
c.set(Calendar.MONTH, desiredmonth);
c.set(Calendar.DAY_OF_MONTH, desiredday);
c.set(Calendar.HOUR_OF_DAY, 0);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);
dates.add(c.getTime());
HighlightEvaluator evaluator = new HighlightEvaluator();
evaluator.setDates(dates);
dateChooser.getJCalendar().getDayChooser().addDateEvaluator(evaluator);
}

compare java fx date picker date to sql date inside a query

I want to know how I can compare a mysql date to a java fx date picker date. So if I have a datepicker date in MM/DD/YYYY format and a mysql date in YYYY/MM/DD format how do I compare those two inside a query?
I prepared for you small appliaction how to convert Date to LocalDate and set in DatePicker, how to take LocalDate from DatePicker and convert to Date. You can also check that date are the same.
public class Main extends Application {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
private Date dateUtil = sdf.parse("2016/09/25");
public Main() throws ParseException {
}
#Override public void start(Stage primaryStage) throws Exception {
Button button = new Button("Take date from DatePicker");
Label labelCompare = new Label();
Label labelCompare2 = new Label();
DatePicker datePicker = new DatePicker();
//convert Date to LocalDate
LocalDate localDate = dateUtil.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
//set in DatePicker
datePicker.setValue(localDate);
VBox hBox = new VBox();
hBox.getChildren().addAll(datePicker, button, labelCompare, labelCompare2);
Scene scene = new Scene(hBox, 400, 400);
primaryStage.setScene(scene);
primaryStage.show();
button.setOnAction(new EventHandler<ActionEvent>() {
#Override public void handle(ActionEvent e) {
//Take LocalDate from DatePicker
LocalDate localDate = datePicker.getValue();
//Convert LocalDate to Date
Date dateFromPicker = Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
//compare
labelCompare.setText("Compare date: " + dateUtil.compareTo(dateFromPicker));
}
});
}
public static void main(String[] args) {
launch();
}
}
Easiest way:
String searched_date = datePicker.getValue().toString();
Date dateFromPicker = Date.valueOf(searched_date);

Add days to a Java textfield through combobox

LINK TO GUI IMAGE HERE -------> http://imgur.com/uPD0K5S
public class MainMenu extends javax.swing.JFrame {
public MainMenu() {
initComponents();
cmbRoomNumber.setEnabled(false);
jPanel1.setVisible(false);
btnBook.setEnabled(false);
//SETTING COMBOBOXES TO NONE
cmbPhotoId.setSelectedIndex(-1);
cmbStayDuration.setSelectedIndex(-1);
//LABELS VALIDATION
jlblNameVer.setVisible(false);
//SETTING DATE TODAY
Date now = new Date();
//Set date format as you want
SimpleDateFormat sf = new SimpleDateFormat("dd/MM/yyyy");
this.ftxtCheckinDate.setText(sf.format(now));
}
As you can see i want to add days to Check-out Date(ftxtCheckOutDate) depending on how many days selected in the combobox(cmbStayDuration)
Im using netbeans JFrame
Thanks :)
private void cmbStayDurationActionPerformed(java.awt.event.ActionEvent evt) {
}
Calendar c = Calendar.getInstance();
c.setTime(new Date());
c.add(Calendar.DATE, combobox number);
Basically Calendar class has a function to add days.
Get the date now, get the combo box day, then add it.
For example:
public static void main(String[] args) {
// TODO code application logic here
Calendar c = Calendar.getInstance();
Date d = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
c.setTime(d);
System.out.println(sdf.format(c.getTime()));
c.setTime(d);
c.add(Calendar.DATE, 10);
System.out.println(sdf.format(c.getTime()));
}
Output:
05/11/2015
15/11/2015
As for changing the value of Check-out Date form as the ComboBox changes, you can add either an ActionListener to listen to it change.
Example

LWUIT Calendar date format

I have a LWUIT code that supposed to print today date .
The problem with me is the date printed in "Mon dd hh:mm:ss GMT+...... yyyy" format
e.g Thu Nov 28 01:00:00 GMT+03:00 2013
So I have a couple of questions
How to get the format in "yyyy-mon-dd" format.
how to add a day to the today date after conversion to "yyyy-mon-dd" .
Observe that some classes wouldn't work in J2ME like Simpledateformat class.
import javax.microedition.midlet.*;
import com.sun.lwuit.*;
import com.sun.lwuit.events.*;
public class myLibrary extends MIDlet {
Form f;
com.sun.lwuit.Calendar cal;
Button b;
public void startApp() {
com.sun.lwuit.Display.init(this);
f = new com.sun.lwuit.Form();
cal = new com.sun.lwuit.Calendar();
b = new Button("Enter");
f.addComponent(cal);
f.addComponent(b);
b.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent acv) {
System.out.println(""+cal.getDate());
}
});
f.show();
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
}
In order to use the java.lwuit.Calendar class, to get your date in that format you will need to substring the data from the cal.getDate().
for example
System.out.println("DAY " + cal.getDate().toString().substring(0,3));
Doing that, you will get your data and after that reorder them in a String.
To change the Date from the Calendar view you will need to use Calendar.setDate(Date d);
I suggest you to use java.util.Calendar
java.util.Calendar c = Calendar.getInstnace();
c.set(Calendar.DAY_OF_THE_MONTH, day_that_you want);
c.set(Calendar.MONTH, month_that_you want);
c.set(Calendar.YEAR, year_that_you want);
java.lwuit.Calendar cal = new java.lwuit.Calendar();
cal.setDate(c.getDate().getTime());
If you still want to use the Date class, try this code, it will print the tomorrow day
private static final int DAY = 24 * 60 * 60 * 1000;
Date d = new Date(); d.setTime(d.getTime() + DAY);
import javax.microedition.midlet.*;
import com.sun.lwuit.*;
import com.sun.lwuit.events.*;
public class myLibrary extends MIDlet {
Form f;
Button b;
public void startApp() {
com.sun.lwuit.Display.init(this);
private static final int DAY =86400000;
f = new com.sun.lwuit.Form();
b = new Button("Enter");
f.addComponent(b);
b.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent acv) {
java.util.Date d = new java.util.Date();
d.setTime(d.getTime() + DAY);
System.out.println(""+ d.toString());
}
});
f.show();
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
}

Adding actionListener to jCalendar

How would I add an actionListener to the jDayChooser component of an existing jCalendar placed using netbeans?
I would like to only trigger an event only when the day buttons are clicked. as the propertyChange in jCalendar listens to even the jMonthChooser and jYearChooser
P.S. using toedter's jCalendar
Alternatively, you can listen for the specific propertyName, "day".
JDayChooser jdc = new JDayChooser();
jdc.addPropertyChangeListener("day", new PropertyChangeListener() {
#Override
public void propertyChange(PropertyChangeEvent e) {
System.out.println(e.getPropertyName()+ ": " + e.getNewValue());
}
});
Addendum: How do I get it to work on a JCalendar?
Similarly, the propertyName, "calendar" represents a Calendar from which you can get() the DAY_OF_MONTH.
JCalendar jc = new JCalendar();
jc.addPropertyChangeListener("calendar", new PropertyChangeListener() {
#Override
public void propertyChange(PropertyChangeEvent e) {
final Calendar c = (Calendar) e.getNewValue();
System.out.println(c.get(Calendar.DAY_OF_MONTH));
}
});
In case somebody misses reading the comments. Here's a sample working code.
JCalendar jCalendar = new JCalendar();
jCalendar.getDayChooser().addPropertyChangeListener("day", new PropertyChangeListener() {
#Override
public void propertyChange(PropertyChangeEvent e) {
System.out.println(e.getPropertyName()+ ": " + e.getNewValue());
}
});

Categories