java DatePicker

 package com.xinxin;
 import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 * @author [email protected]
 * @version 1.0
 * @date 2011-05-05
 * @description I am very bored tonight. I am trying to make a notebook for
 *              myself.But I am very bored with the silly coding. And I thought
 *              datepicker may be part of a notebook. So I did it. It is under
 *              Apache License. You can use it for any purpose you like. But if
 *              somebody can help me find a good notebook under ubuntu, I will
 *              express my appreciate to them very much.By the way, you can use
 *              your own favourite icons.
 * */
public class JQDatePicker extends JPanel implements ActionListener {
	private static final long serialVersionUID = 7576197202344588176L;
	private JPanel northPanel;
	private JButton nextYear;
	private JButton nextMonth;
	private JButton lastYear;
	private JButton lastMonth;
	private JLabel centerLabel;
	private JPanel mainPanel;
	private JButton[] dates;
	private Date curDate;
	private static String NEXT_MONTH = "NextMonth";
	private static String NEXT_YEAR = "NextYear";
	private static String LAST_MONTH = "LastMonth";
	private static String LAST_YEAR = "LastYear";
	private static String DATE_SELECT = "DateSelect";
	private static byte[] NEXT_YEAR_ICON_DATA = new byte[] { -119, 80, 78, 71,
			13, 10, 26, 10, 0, 0, 0, 13, 73, 72, 68, 82, 0, 0, 0, 16, 0, 0, 0,
			16, 8, 6, 0, 0, 0, 31, -13, -1, 97, 0, 0, 0, 4, 115, 66, 73, 84, 8,
			8, 8, 8, 124, 8, 100, -120, 0, 0, 0, 9, 112, 72, 89, 115, 0, 0, 1,
			-69, 0, 0, 1, -69, 1, 58, -20, -29, -30, 0, 0, 0, 25, 116, 69, 88,
			116, 83, 111, 102, 116, 119, 97, 114, 101, 0, 119, 119, 119, 46,
			105, 110, 107, 115, 99, 97, 112, 101, 46, 111, 114, 103, -101, -18,
			60, 26, 0, 0, 2, 23, 73, 68, 65, 84, 120, -38, -91, 83, -49, 107,
			19, 65, 24, 125, -77, 59, 59, -37, 77, 55, -110, 31, -43, -86, -95,
			80, -120, -32, 33, 21, -44, -93, -74, 42, -76, 107, -15, 100, 60,
			120, -48, -117, -34, -4, 7, -68, 74, -95, 23, 61, 21, -76, 61,
			-118, -32, -59, -109, 122, -11, -32, -95, 39, 45, 40, -83, 10,
			-107, -30, 65, 15, -110, 96, 106, 107, -78, -119, -55, -26, -57,
			-18, -26, -13, 115, 9, 45, -95, -90, 30, -6, -122, -57, 55, 15,
			-26, 123, -13, 6, -66, 17, 68, -124, -125, 64, 3, 67, -4, -59, 85,
			113, 95, 56, 34, -43, -89, -89, 89, -1, 7, 81, 2, 113, 77, 88,
			-109, -39, 73, -49, 109, -72, -107, -11, -62, -6, 101, 72, 124,
			118, -50, 56, 94, -39, 45, -105, 87, 55, 86, 29, 122, 69, 107,
			-125, 19, -12, 16, 106, 33, 102, 46, -52, 36, -90, 78, 79, -67, 1,
			-31, -106, 47, 124, -28, -13, -7, -92, 115, -34, 89, -31, 52, -73,
			49, 0, 18, 61, -44, 121, -107, -36, -110, 24, -49, -114, 43, 123,
			-60, 94, -30, 52, 40, 85, 75, 34, 119, 54, -89, -30, -55, -8, 99,
			-21, -70, 117, -82, 53, -47, -70, 67, 115, 68, -1, 124, 66, -10,
			100, -42, 27, 75, -114, 65, -38, 18, -58, -112, 1, -61, 52, 32, 37,
			-17, 53, -82, -68, 26, 110, 35, 92, 126, -69, -4, -95, 90, -84,
			-50, -46, 107, 42, -17, 49, -56, -28, 50, -98, 45, 109, -56, 67,
			-36, 20, -29, 38, 83, 66, 73, 5, 41, 88, -121, -84, 3, 9, -14, 40,
			-4, -72, -15, -87, -14, 115, 115, 115, -106, 94, -48, 90, -33, 19,
			90, 90, 11, 94, -57, -117, 14, -22, -92, 71, -108, -60, 38, -92,
			-96, 2, 5, -93, -51, 38, 109, -87, -97, 56, -98, 77, 91, -58, -48,
			-118, -72, 41, 110, -48, 51, 122, -71, 99, -32, -23, 77, 52, -69,
			30, -96, -9, 108, 21, 16, -95, -61, -84, 3, -62, 21, 64, 5, -48,
			126, 105, -120, 89, -61, 4, 67, -41, -5, 18, -24, 38, 107, 3, 64,
			-100, -103, 100, -38, 76, -63, -12, 16, -127, 58, 4, -53, -73, 2,
			-61, 52, 107, -75, -83, -58, 21, 122, 26, -68, -21, 55, -112, -68,
			-19, 2, 48, -103, -61, 92, -46, 38, -38, 90, 59, -70, 21, 77, 32,
			17, 75, 6, 97, -93, -5, -91, -26, -71, -105, -24, 9, 109, -17,
			-103, 3, -87, -11, 12, -104, -88, 104, 97, 98, 59, 21, -39, 11, 18,
			56, 22, 102, -62, 118, -39, 127, -2, 91, 84, 79, -47, -62, 110,
			115, 127, -126, -82, 14, -27, 43, -22, -44, -125, 0, 63, 112, -113,
			12, 122, 96, -106, 76, 26, -35, 58, 26, 22, -66, 21, -17, -122,
			115, -2, -61, 125, 39, 49, 101, -91, -111, 26, 57, -62, -13, -92,
			77, -29, 125, -9, 81, -86, -109, 70, -70, 48, 90, -1, -2, -75, 120,
			49, 106, 30, -124, -34, 111, 20, -6, -110, 90, -60, 60, 14, -17,
			-24, -123, 93, -67, 31, 15, -4, -99, -1, 0, 49, -107, -8, 5, 33,
			-92, -102, 89, 0, 0, 0, 0, 73, 69, 78, 68, -82, 66, 96, -126 };
	private static byte[] NEXT_MONTH_ICON_DATA = new byte[] { -119, 80, 78, 71,
			13, 10, 26, 10, 0, 0, 0, 13, 73, 72, 68, 82, 0, 0, 0, 16, 0, 0, 0,
			16, 8, 6, 0, 0, 0, 31, -13, -1, 97, 0, 0, 0, 4, 115, 66, 73, 84, 8,
			8, 8, 8, 124, 8, 100, -120, 0, 0, 0, 9, 112, 72, 89, 115, 0, 0, 1,
			-69, 0, 0, 1, -69, 1, 58, -20, -29, -30, 0, 0, 0, 25, 116, 69, 88,
			116, 83, 111, 102, 116, 119, 97, 114, 101, 0, 119, 119, 119, 46,
			105, 110, 107, 115, 99, 97, 112, 101, 46, 111, 114, 103, -101, -18,
			60, 26, 0, 0, 1, -116, 73, 68, 65, 84, 120, -38, -91, -109, 63, 75,
			-101, 81, 20, -121, 127, -25, -2, 121, -109, 92, -45, -108, 26,
			-83, 40, 46, -102, 33, 56, 4, 106, -95, -123, 64, 29, 5, -55, -92,
			17, -95, 84, 39, 51, -76, 115, -91, 115, -23, 38, 1, -63, -59, 69,
			23, -23, -46, 81, -4, 22, 14, -3, 2, -115, 88, 17, 65, 19, -109,
			-102, 40, -58, 68, -33, 123, -33, -93, 89, 45, -119, -47, 60, 112,
			-90, 3, 15, -100, -33, 57, -121, -104, 25, -67, 32, -16, 0, -102,
			-91, 76, 100, 62, -78, -12, 108, 1, 24, 75, 43, -117, 43, 63, 99,
			31, 99, 91, -12, -125, -24, -55, -126, 22, 102, -64, 80, 110, 33,
			-73, 60, 124, 56, -4, -101, -90, -87, -1, 105, -126, 8, 80, -68,
			44, -62, -123, -99, -56, -50, 102, -33, 36, -110, -119, 63, -108,
			-95, 73, -76, 65, -31, 33, 125, 64, -79, 94, 4, -88, -43, 84, 34,
			-3, 33, 29, 55, 47, -52, 30, -51, -47, 103, -34, -31, -19, 71, 5,
			-62, 72, 89, -78, 37, 4, 65, -48, 18, 64, 91, 77, -93, -29, -93,
			-98, 14, -23, 45, -17, -109, -105, -10, -109, -2, 23, -2, -50, -36,
			94, 16, 22, -78, 66, 21, -8, -28, 67, 59, 13, -27, 43, -56, -122,
			-124, -114, 104, -107, 26, 75, -27, 10, -57, -5, 111, -119, -24,
			61, -33, -45, 86, 80, -96, 2, 108, 96, -127, 6, 64, 23, 4, 81, 19,
			112, 101, 7, 115, 99, 56, 48, 1, 117, 12, 81, 122, 90, -120, 65, 1,
			-60, 1, -68, 4, 56, -54, 112, 33, -121, 87, 67, -3, -42, -58, -125,
			95, -51, -111, -26, 59, -26, 78, 35, 120, 66, 113, 75, 112, 5, -64,
			2, -70, -82, -39, 103, -25, 87, -101, -75, -81, -68, -18, 54, 30,
			13, -111, 52, -55, -5, 2, 8, -120, 34, -22, -62, 13, 115, 85, 105,
			84, 103, 120, -19, 118, -81, -85, 53, 74, -83, 36, -97, 90, -68,
			-82, 15, 89, 123, 20, 28, 84, 78, -50, -90, 56, -49, -27, -82, 15,
			73, 40, 33, -57, -2, 37, 112, -3, -9, 118, -9, -68, 90, -98, -32,
			85, 46, -93, 3, -1, 125, -93, -54, -121, -26, 64, 52, 104, -65, 53,
			55, -47, 5, 61, -65, -13, 29, 127, 75, -107, 51, 75, -95, -30, -59,
			0, 0, 0, 0, 73, 69, 78, 68, -82, 66, 96, -126 };
	private static byte[] LAST_YEAR_ICON_DATA = new byte[] { -119, 80, 78, 71,
			13, 10, 26, 10, 0, 0, 0, 13, 73, 72, 68, 82, 0, 0, 0, 16, 0, 0, 0,
			16, 8, 6, 0, 0, 0, 31, -13, -1, 97, 0, 0, 0, 4, 115, 66, 73, 84, 8,
			8, 8, 8, 124, 8, 100, -120, 0, 0, 0, 9, 112, 72, 89, 115, 0, 0, 1,
			-69, 0, 0, 1, -69, 1, 58, -20, -29, -30, 0, 0, 0, 25, 116, 69, 88,
			116, 83, 111, 102, 116, 119, 97, 114, 101, 0, 119, 119, 119, 46,
			105, 110, 107, 115, 99, 97, 112, 101, 46, 111, 114, 103, -101, -18,
			60, 26, 0, 0, 2, 10, 73, 68, 65, 84, 120, -38, -91, 83, -49, 107,
			19, 65, 20, -2, 102, 103, -109, -35, -92, -124, -18, 110, 118, 91,
			37, -104, -96, 30, -92, 88, 81, -45, -101, 80, 107, 127, -28, -112,
			83, 75, 60, -23, 69, -15, 95, -16, 40, 82, -12, -30, -83, -96, -10,
			-30, 69, -16, -28, 65, -63, 115, -51, -75, 34, -24, 65, 16, 47,
			-126, 7, -47, -106, 52, 90, 83, -37, 77, -109, 108, 102, 54, -49,
			-23, 6, -37, -122, 104, 123, -24, 123, 60, 120, -33, -52, -5, -66,
			-9, 6, -34, 48, 34, -62, 81, 76, -61, 33, -58, -90, -103, -61, 102,
			-39, 3, -90, 44, -62, -123, 94, 124, -32, 4, -84, -56, -14, 99, 35,
			99, 101, -57, 114, -100, -14, -121, 114, 18, 18, 103, 71, -77, -93,
			-81, -83, -92, 101, 47, 127, 89, 78, -46, 43, 106, -2, 119, 2, -43,
			-27, 102, -31, 82, -31, -19, -36, -20, -100, 45, -104, 0, 8, 55,
			-58, -13, -29, 111, 102, -82, -52, 88, 97, 44, -36, -83, -45, -5,
			-120, -9, 24, 51, 63, -103, 79, 74, 83, -91, 91, -39, -45, 89, -66,
			-74, -71, -122, -128, 7, 40, 78, 21, 23, 93, -57, -115, 112, 93,
			121, -97, -64, -33, -9, 13, 30, 27, 92, -102, -100, -104, -68,
			-104, -80, 19, -68, -78, 85, -127, 80, -18, -27, 60, 8, 33, 120,
			-91, 81, -127, 108, 74, 52, -12, 70, -65, 0, -69, -54, -14, 67, 39,
			-121, -105, 46, -100, 58, 111, 55, 89, -109, -5, -37, 62, -124, 46,
			32, 53, -119, -74, 114, 41, -91, 18, 81, 56, -112, 104, -23, -83,
			94, 1, 118, -99, -107, 114, 94, -18, 121, -58, -54, -60, 106, -83,
			26, -109, 76, 21, 39, 5, -38, -92, -88, 49, 69, 86, 56, -36, 113,
			10, 33, 67, 9, 77, -41, 122, 5, 16, -29, -68, -74, -67, 65, 43, 95,
			87, -48, 113, 59, 64, 26, 32, -101, 0, 123, -25, 110, 95, -112,
			-118, 0, 72, -124, -55, -34, 61, -96, 103, -14, -123, 31, 107, 94,
			30, 24, 73, 109, -104, 41, 83, -110, 65, -64, 0, -128, 20, -70, 34,
			-98, 10, 55, -54, -93, 51, -98, -32, -3, -117, 68, 79, -37, -17,
			-74, -40, -17, 51, -70, 23, -1, 108, -91, 108, 25, 117, 52, -69, 4,
			-61, 51, 0, 7, 93, 81, -107, 114, 93, -1, -9, 38, -46, 2, -83, -5,
			124, -13, 92, 0, -15, -14, -72, -103, 9, 25, -79, 104, 108, -21,
			-89, 98, -1, -48, 66, 16, -128, 14, -96, 107, -3, 2, 123, 34, -13,
			68, -115, -69, -2, -75, 106, -75, 122, -5, 68, -112, -107, -122,
			111, 16, -7, 4, 124, -60, 29, -84, 106, 34, 30, -60, -119, -121,
			-4, -16, -65, 16, -50, -117, -121, -33, -66, -81, 78, -92, 127, 13,
			-41, -45, 109, 23, 120, -33, 121, -124, 117, 109, -38, -47, -121,
			-22, 105, -61, -35, -41, -111, -24, -64, -64, 125, 120, 124, 33,
			-2, 24, 0, -37, -59, -117, 123, -8, -56, -33, -7, 15, 68, 110, -23,
			-43, 83, -91, -64, 90, 0, 0, 0, 0, 73, 69, 78, 68, -82, 66, 96,
			-126 };
	private static byte[] LAST_MONTH_ICON_DATA = new byte[] { -119, 80, 78, 71,
			13, 10, 26, 10, 0, 0, 0, 13, 73, 72, 68, 82, 0, 0, 0, 16, 0, 0, 0,
			16, 8, 6, 0, 0, 0, 31, -13, -1, 97, 0, 0, 0, 4, 115, 66, 73, 84, 8,
			8, 8, 8, 124, 8, 100, -120, 0, 0, 0, 9, 112, 72, 89, 115, 0, 0, 1,
			-69, 0, 0, 1, -69, 1, 58, -20, -29, -30, 0, 0, 0, 25, 116, 69, 88,
			116, 83, 111, 102, 116, 119, 97, 114, 101, 0, 119, 119, 119, 46,
			105, 110, 107, 115, 99, 97, 112, 101, 46, 111, 114, 103, -101, -18,
			60, 26, 0, 0, 1, 125, 73, 68, 65, 84, 120, -38, -91, 83, -65, 75,
			66, 81, 20, 62, -57, 123, -33, -45, 82, -45, -25, 15, 122, 26, 37,
			6, 17, 13, 10, -71, -75, -44, 3, 105, 104, 50, 107, -86, 41, -24,
			15, 17, 93, 90, 66, -120, -106, -90, -96, -87, -95, 102, 65, -6, 3,
			-118, -42, -90, -74, 40, 67, -125, -52, -12, 41, 79, 125, -41, 119,
			-69, -24, -30, 16, -6, -94, -13, 113, -106, -113, 115, -66, -13,
			13, -33, 65, -50, 57, -4, -89, 28, 118, 7, 49, -115, 1, -52, -32,
			9, -118, 26, -25, -87, -83, -27, 29, 76, -91, -110, -87, 59, 53,
			-92, 6, 74, -114, 82, 65, 80, -122, 109, 7, -30, -22, 81, 122, 35,
			125, -97, -35, -51, 42, 38, -102, 67, -50, -106, 3, -52, 35, 58,
			-97, -100, 23, 25, 45, 115, 28, 95, -119, -109, 90, -77, 6, 29, 1,
			91, 2, -72, -115, 1, -97, -22, 43, 107, -102, -74, -18, 81, 60,
			-92, -38, -86, -126, 41, -96, 83, 125, -70, 0, -18, 99, 74, 93,
			-114, -108, -109, -15, -124, -46, -59, 46, 105, 119, -38, 96, 82,
			19, -104, -125, -127, 65, -115, -55, 2, 120, -120, 123, -79, 112,
			-20, 58, -22, -117, 74, 117, -93, -114, 12, 24, 88, 110, 11, -6, 2,
			76, 98, -48, -109, 123, 83, 28, 72, -124, 124, 117, 26, -68, -14,
			82, 1, 43, 100, 1, -122, 16, 44, -65, 5, -96, -116, 38, 103, -56,
			-20, -28, 28, -16, 43, 118, -93, 75, -58, -90, 123, -51, -37, 112,
			121, 93, -52, -110, -59, -78, 27, 0, -68, 48, 20, 33, 115, 100,
			122, -112, -8, 101, -1, -79, -123, -33, -85, 52, 44, 63, -5, -67,
			10, 3, 73, -112, -82, -111, 8, -15, 80, 123, 73, -28, 69, -2, -87,
			-109, 102, -94, 7, -26, 109, -60, -75, 48, 64, -114, -126, 4, -96,
			-108, -38, -49, 1, -49, 13, -97, -28, -128, -28, -91, -121, 69,
			121, -23, -12, 67, -81, 17, 98, 17, -4, -13, 47, 12, 114, -26, -39,
			-21, -37, -5, 86, -80, 62, -33, 14, -10, 67, -65, 92, -30, -36, 86,
			67, 1, -62, -92, 40, -97, 3, 0, -114, -13, -1, 126, -25, 31, -107,
			2, -95, -24, 93, -109, 56, 106, 0, 0, 0, 0, 73, 69, 78, 68, -82,
			66, 96, -126 };
	private String[] days = new String[] { "Sat", "Mon", "Tue", "Wed", "Thu",
			"Fri", "Sat" };
	private JLabel[] daysLabel = new JLabel[7];

	public JQDatePicker() {
		Calendar c = Calendar.getInstance();
		curDate = c.getTime();
		this.buildComponents();
	}

	public JQDatePicker(Date curDate) {
		this.curDate = curDate;
		this.buildComponents();
	}

	private void buildComponents() {
		try {
			this.initComponents();
			this.setDatePanel(curDate);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private void initComponents() throws IOException {
		nextYear = new JQButton(getIcon(NEXT_YEAR_ICON_DATA));
		nextYear.addActionListener(this);
		nextYear.setActionCommand(NEXT_YEAR);
		nextMonth = new JQButton(getIcon(NEXT_MONTH_ICON_DATA));
		nextMonth.addActionListener(this);
		nextMonth.setActionCommand(NEXT_MONTH);
		lastYear = new JQButton(getIcon(LAST_YEAR_ICON_DATA));
		lastYear.addActionListener(this);
		lastYear.setActionCommand(LAST_YEAR);
		lastMonth = new JQButton(getIcon(LAST_MONTH_ICON_DATA));
		lastMonth.addActionListener(this);
		lastMonth.setActionCommand(LAST_MONTH);
		centerLabel = new JLabel();

		northPanel = new JPanel();
		JPanel left = new JPanel(new FlowLayout(FlowLayout.LEFT));
		left.add(lastYear);
		left.add(lastMonth);

		JPanel right = new JPanel(new FlowLayout(FlowLayout.RIGHT));
		right.add(nextMonth);
		right.add(nextYear);

		JPanel center = new JPanel(new FlowLayout(FlowLayout.CENTER));
		center.add(centerLabel);

		northPanel.add(left);
		northPanel.add(center);
		northPanel.add(right);
		mainPanel = new JPanel();

		dates = new JButton[42];
		mainPanel.setLayout(new GridLayout(7, 7));
		for (int i = 0; i < 7; i++) {
			daysLabel[i] = new JLabel(days[i]);
			daysLabel[i].setHorizontalAlignment(JLabel.CENTER);
			daysLabel[i].setOpaque(true);
			daysLabel[i].setBackground(Color.CYAN);
			mainPanel.add(daysLabel[i]);
		}

		for (int i = 0; i < 42; i++) {
			dates[i] = new JQButton("");
			dates[i].setBackground(Color.WHITE);
			dates[i].setActionCommand(DATE_SELECT);
			dates[i].addActionListener(this);
			mainPanel.add(dates[i]);
		}
		this.setLayout(new BorderLayout());
		mainPanel.setPreferredSize(new Dimension(224, 192));
		this.add(northPanel, BorderLayout.NORTH);
		this.add(mainPanel, BorderLayout.CENTER);
		this.setBorder(BorderFactory.createLineBorder(Color.red));

	}

	public ImageIcon getIcon(String name) throws IOException {
		InputStream iconStream = this.getClass().getClassLoader()
				.getResourceAsStream(name);
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		byte buffer[] = new byte[1024];
		int c = 0;
		while ((c = iconStream.read(buffer)) != -1) {
			out.write(buffer, 0, c);
		}
		iconStream.close();
		return getIcon(out.toByteArray());
	}

	public ImageIcon getIcon(byte[] iconData) {
		return new ImageIcon(iconData);
	}

	public void setDatePanel(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		int year = c.get(Calendar.YEAR);
		int month = c.get(Calendar.MONTH);
		centerLabel.setText(c.getDisplayName(Calendar.MONTH, Calendar.SHORT,
				Locale.UK)
				+ " " + year);

		int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
		int dayOfMonth = c.get(Calendar.DAY_OF_MONTH);
		int daysInMonth = this.getDayInMonth(year, month + 1);
		int padding = dayOfWeek + (dayOfMonth / 7) * 7;
		if (padding < dayOfMonth) {
			padding += 7;
		}
		padding -= 1;
		int right = padding;

		// reset
		for (int i = 0; i < dates.length; i++) {
			dates[i].setBackground(Color.WHITE);
			dates[i].setEnabled(true);
		}
		for (int i = dayOfMonth; i <= daysInMonth; i++) {
			dates[right].setText(i + "");
			right++;
		}
		int left = padding;
		for (int i = dayOfMonth; i > 0; i--) {
			dates[left--].setText(i + "");
		}
		for (int i = 0; i <= left; i++) {
			dates[i].setText("");
			dates[i].setEnabled(false);
			dates[i].setBackground(new Color(0xE9,0xD4,0xB5));
		}
		for (int i = right; i < dates.length; i++) {
			dates[i].setText("");
			dates[i].setEnabled(false);
			dates[i].setBackground(new Color(0xE9,0xD4,0xB5));
		}

		dates[padding].setBackground(Color.GRAY);

	}

	public int getDayInMonth(int year, int month) {
		int days = 30;
		switch (month) {
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12:
			days = 31;
			break;
		case 2:
			if (isLeapYear(year))
				days = 29;
			else
				days = 28;
			break;

		}
		return days;
	}

	public boolean isLeapYear(int year) {
		boolean isLeap = false;
		if ((year % 4 == 0 && year % 100 == 0) || (year % 1000 == 0)) {
			isLeap = true;
		}
		return isLeap;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		String cmd = e.getActionCommand();
		if (cmd.equals(NEXT_MONTH)) {
			Calendar c = Calendar.getInstance();
			c.setTime(curDate);
			c.add(Calendar.MONTH, 1);
			curDate = c.getTime();
			this.setDatePanel(curDate);
		} else if (cmd.equals(NEXT_YEAR)) {
			Calendar c = Calendar.getInstance();
			c.setTime(curDate);
			c.add(Calendar.YEAR, 1);
			curDate = c.getTime();
			this.setDatePanel(curDate);
		} else if (cmd.equals(LAST_MONTH)) {
			Calendar c = Calendar.getInstance();
			c.setTime(curDate);
			c.add(Calendar.MONTH, -1);
			curDate = c.getTime();
			this.setDatePanel(curDate);
		} else if (cmd.equals(LAST_YEAR)) {
			Calendar c = Calendar.getInstance();
			c.setTime(curDate);
			c.add(Calendar.YEAR, -1);
			curDate = c.getTime();
			this.setDatePanel(curDate);
		} else if (cmd.equals(DATE_SELECT)) {
			JButton s = (JButton) e.getSource();
			int day = Integer.parseInt(s.getText());
			Calendar c = Calendar.getInstance();
			c.setTime(curDate);
			c.set(Calendar.DAY_OF_MONTH, day);
			curDate = c.getTime();
			for (JButton each : dates) {
				if(each.isEnabled()) each.setBackground(Color.WHITE);
			}
			s.setBackground(Color.GRAY);
			SimpleDateFormat f = new SimpleDateFormat("dd/MM/yyyy");
			String selectDate = f.format(curDate);
			System.err.println(selectDate);

		}
	}

	public static void main(String args[]) {
		JQDatePicker dp = new JQDatePicker();
		JFrame f = new JFrame();
		f.setTitle("JQDatePicker - A simple datepicker in java by [email protected]");
		f.getContentPane().add(dp, BorderLayout.NORTH);
		f.setSize(400, 400);
		f.setVisible(true);
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

}

class JQButton extends JButton {
	private static final long serialVersionUID = 1L;

	public JQButton(String text) {
		super(text);
		this.setPreferredSize(new Dimension(16, 16));
		this.setBounds(0, 0, 16, 16);
	}

	public JQButton(ImageIcon icon) {
		super(icon);
		this.setPreferredSize(new Dimension(32, 32));
		this.setContentAreaFilled(false);
	}java DatePicker} 

你可能感兴趣的:(java,Datepicker)