Skip to content

Instantly share code, notes, and snippets.

@ayapi
Last active December 29, 2015 01:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ayapi/7592669 to your computer and use it in GitHub Desktop.
Save ayapi/7592669 to your computer and use it in GitHub Desktop.
JXDatePickerで選択できる日付の範囲を限定する
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.jdesktop.swingx.JXDatePicker;
import javax.swing.*;
import java.awt.*;
import java.util.Date;
import java.util.TimeZone;
public class Calendar extends JPanel {
private JXDatePicker datePicker;
public Calendar(){
initDatePicker();
}
private void initDatePicker(){
Date tomorrow = DateUtils.addDays(new Date(), 1);
Date oneWeekAfter = DateUtils.addDays(new Date(), 7);
datePicker = new JXDatePicker(tomorrow);
datePicker.getMonthView().setLowerBound(tomorrow);
datePicker.getMonthView().setUpperBound(oneWeekAfter);
UIManager.put("JXMonthView.unselectableDayForeground", Color.GRAY);
add(datePicker);
}
public String getDate() {
return DateFormatUtils.format(
datePicker.getDate(),
"yyyy-MM-dd",
TimeZone.getTimeZone("GMT")
);
}
}
class Window {
public static void main(String[] args) throws Exception {
JFrame frame = new JFrame();
frame.setSize(200, 200);
Calendar calendar = new Calendar();
frame.add(calendar);
SwingUtilities.updateComponentTreeUI(frame);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment