可以从网络上同步法定节假日,双击或者右键改变节假日状态
package com.vito.holiday.view;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.Border;
import com.vito.holiday.service.HolidayService;
import com.vito.holiday.util.CalendarDO;
import com.vito.holiday.util.HolidayWebTool;
import com.vito.holiday.util.JResponseObject;
import com.vito.holiday.util.StringFunction;
/**
* @Description: 节假日管理-主界面
* @author wutp 2017年5月15日
* @version 1.0
*/
public class HolidayManageDlg extends JFrame implements ActionListener {
/**
*
*/
private static final long serialVersionUID = 1L;
Border border1;
JPanel northPanel = new JPanel();
JPanel centerPanel = new JPanel();
JPanel southPanel = new JPanel();
JPanel pNorth1 = new JPanel();
JPanel pNorth2 = new JPanel();
JPanel pNorth3 = new JPanel();
JComboBox
JComboBox
private boolean yearComboBoxIgnore = false;
private boolean monthComboBoxIgnore = false;
JButton previousMonthBtn, nextMonthBtn, backTodayBtn;
JLabel showMessage = new JLabel("", JLabel.CENTER);
JMonthPanel monthPanel;
private int year = 2017;
private int month = 5;
private CalendarDO calendarDO;
private Calendar calendar = Calendar.getInstance();
private String[] days;
public String SERVER_TYPE = "";
public HolidayManageDlg() {
try{
initDate(true);
jbInit("节假日管理");
pack();
openCalendar(true);
changeComboBox();
}catch (Exception e) {
e.printStackTrace();
}
}
private void jbInit(String title) throws Exception{
this.setTitle(title);
this.getContentPane().setLayout(new BorderLayout());
this.getContentPane().setPreferredSize(new Dimension(600, 600));
this.getContentPane().setSize(600, 600);
buildMenu();
this.getContentPane().add(northPanel, BorderLayout.NORTH);
this.getContentPane().add(centerPanel, BorderLayout.CENTER);
this.getContentPane().add(southPanel, BorderLayout.SOUTH);
border1 = BorderFactory.createLineBorder(new Color(172, 172, 172));
centerPanel.setBorder(border1);
centerPanel.setLayout(new BorderLayout());
yearComboBox = new JComboBox
yearComboBox.setPreferredSize(new Dimension(70, 22));
yearComboBox.setMinimumSize(new Dimension(70,23));//设置按钮大小最小值
for(int i=1901;i<2100;i++){
yearComboBox.addItem(String.valueOf(i));
}
monthComboBox = new JComboBox
monthComboBox.setPreferredSize(new Dimension(50, 22));
monthComboBox.setMinimumSize(new Dimension(50,23));//设置按钮大小最小值
String item = "";
for(int i=1;i<13;i++){
if(String.valueOf(i).length() == 1){
item = "0"+i;
}else{
item = ""+i;
}
if( !"".equals(item) ){
monthComboBox.addItem(item);
}
}
previousMonthBtn = new JButton("上月");
previousMonthBtn.setMinimumSize(new Dimension(40,23));//设置按钮大小最小值
previousMonthBtn.setPreferredSize(new Dimension(40,23));//设置按钮大小
previousMonthBtn.setMargin(new Insets(2,2,2,2));//设置按钮边框和标签之间的空白
previousMonthBtn.setText("上月");//设置文本
previousMonthBtn.setMnemonic('P');//设置快捷键
nextMonthBtn = new JButton();
nextMonthBtn.setMinimumSize(new Dimension(40,23));//设置按钮大小最小值
nextMonthBtn.setPreferredSize(new Dimension(40,23));//设置按钮大小
nextMonthBtn.setMargin(new Insets(2,2,2,2));//设置按钮边框和标签之间的空白
nextMonthBtn.setText("下月");//设置文本
nextMonthBtn.setMnemonic('N');//设置快捷键
backTodayBtn = new JButton();
backTodayBtn.setMinimumSize(new Dimension(90,23));//设置按钮大小最小值
backTodayBtn.setPreferredSize(new Dimension(90,23));//设置按钮大小
backTodayBtn.setMargin(new Insets(2,2,2,2));//设置按钮边框和标签之间的空白
backTodayBtn.setText("返回今天");//设置文本
backTodayBtn.setMnemonic('T');//设置快捷键
northPanel.setLayout(new GridLayout(1,3,1,1));
northPanel.add(pNorth1);
northPanel.add(pNorth2);
northPanel.add(pNorth3);
pNorth1.add(yearComboBox,BorderLayout.WEST);
pNorth2.setLayout(new FlowLayout(FlowLayout.CENTER));
pNorth2.add(previousMonthBtn);
pNorth2.add(monthComboBox);
pNorth2.add(nextMonthBtn);
pNorth3.add(backTodayBtn,BorderLayout.EAST);
southPanel.add(showMessage);
showMessage.setText("日历:" + getDate());
// JScrollPane scrollPane = new JScrollPane();
// scrollPane.add(pCenter);
backTodayBtn.addActionListener(this);
nextMonthBtn.addActionListener(this);
previousMonthBtn.addActionListener(this);
yearComboBox.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
if(e.getStateChange() == ItemEvent.SELECTED){
if(yearComboBoxIgnore){
yearComboBoxIgnore = false;
return;
}
String year = (String) e.getItem();
System.out.println("---ItemEvent performed:" + e.paramString() + "---");
boolean result = itemStateChangedAction("YEAR",year);
if(result){
initDate(false);
try{
openCalendar(false);
}catch(Exception ex){
ex.printStackTrace();
}
}
}
}
});
monthComboBox.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
if(e.getStateChange() == ItemEvent.SELECTED){
if(monthComboBoxIgnore){
monthComboBoxIgnore = false;
return;
}
String month = (String) e.getItem();
System.out.println("---ItemEvent performed:"+ e.paramString() + "---");
boolean result = itemStateChangedAction("MONTH", month);
if (result) {
initDate(false);
try {
openCalendar(false);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
}
});
}
private void initDate(boolean isFirst) {
if(isFirst){
this.year = calendar.get(Calendar.YEAR);
this.month = calendar.get(Calendar.MONTH) + 1;
this.calendarDO = new CalendarDO();
}
calendarDO.setYear(year);
calendarDO.setMonth(month);
days = calendarDO.getCalendar();
}
private void buildMenu() {
// 菜单设置
JMenuBar menuBar = new JMenuBar();
JMenu operateMenu = new JMenu("操作");
JMenuItem nowSyncMenuItem = new JMenuItem("立即同步");
JCheckBoxMenuItem autoAsyMenuItem = new JCheckBoxMenuItem("自动同步");
operateMenu.add(nowSyncMenuItem);
//operateMenu.add(autoAsyMenuItem);
menuBar.add(operateMenu);
JMenu settingMenu = new JMenu("设置");
JCheckBoxMenuItem menuItem1 = new JCheckBoxMenuItem("bitefu");
JCheckBoxMenuItem menuItem2 = new JCheckBoxMenuItem("easybots");
//settingMenu.add(menuItem1);
settingMenu.add(menuItem2);
menuBar.add(settingMenu);
this.setJMenuBar(menuBar);
nowSyncMenuItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent paramActionEvent) {
try {
nowSyncHoliday();
} catch (Exception e) {
e.printStackTrace();
}
}
});
autoAsyMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
/*JOptionPane.showMessageDialog(this, "暂时不支持自动同步!",
"节假日管理", JOptionPane.ERROR_MESSAGE);*/
}
});
menuItem1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
boolean b = ((JCheckBoxMenuItem) e.getSource()).getState();
if (b) {
SERVER_TYPE = HolidayWebTool.SERVER_BITTEFU;
} else {
SERVER_TYPE = "";
}
}
});
menuItem2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
boolean b = ((JCheckBoxMenuItem) e.getSource()).getState();
if (b) {
SERVER_TYPE = HolidayWebTool.SERVER_EASYBOTS;
} else {
SERVER_TYPE = "";
}
}
});
}
private void openCalendar(boolean isFirst) throws Exception{
monthPanel = new JMonthPanel(this,days,getDate(),getHolidays());
if(monthPanel != null){
this.centerPanel.removeAll();
this.centerPanel.add(monthPanel,BorderLayout.CENTER);
}
if(!isFirst){
centerPanel.setVisible(false);
centerPanel.setVisible(true);
}
}
private boolean itemStateChangedAction(String type,String value){
int number = 0;
if(value == null && "".equals(value)){
return false;
}
try{
number = Integer.valueOf(value);
}catch(Exception e){
e.printStackTrace();
return false;
}
if(type.equals("YEAR")){
this.year = number;
showMessage.setText("日历:" + getDate());
return true;
}else if(type.equals("MONTH")){
this.month = number;
showMessage.setText("日历:" + getDate());
return true;
}
return false;
}
private void changeComboBox(){
yearComboBoxIgnore = true;
yearComboBox.setSelectedItem(String.valueOf(year));
monthComboBoxIgnore = true;
monthComboBox.setSelectedItem(StringFunction.fillBeforeValue(String.valueOf(month)));
}
private void nowSyncHoliday() throws Exception{
if("".equals(SERVER_TYPE)){
JOptionPane.showMessageDialog(this, "请先选择网络源!");
return;
}
Map
String sql = "select count(*) from PSJJR where F_YEAR = ?";
boolean deleteDate = false;
String[] paramter = new String[1];
paramter[0] = String.valueOf(this.year);
PO.put("CMDTEXT", sql);
JResponseObject RO = (JResponseObject) HolidayService.getCountBySql(PO,paramter);
if (RO == null) {
JOptionPane.showMessageDialog(this, "读取错误!\r\n原因:RO为空", "提示",
JOptionPane.ERROR_MESSAGE);
return ;
} else if (RO.ErrorCode == 1) {
Integer count = (Integer) RO.ResponseObject;
if(count != 0){
int result = JOptionPane.showConfirmDialog(this,
"已存在 "+this.year+"年的数据"+count+" 条,是否删除数据在同步?", "系统提示", JOptionPane.YES_NO_CANCEL_OPTION);
if (result == JOptionPane.YES_OPTION) {
deleteDate = true;
} else if (result == JOptionPane.NO_OPTION) {
deleteDate = false;
} else if (result == JOptionPane.CANCEL_OPTION) {
return ;
}
}
} else {
JOptionPane.showMessageDialog(this, "读取错误!\r\n原因:"
+ RO.ErrorString, "提示", JOptionPane.ERROR_MESSAGE);
return ;
}
ArrayList
if(deleteDate){
sql = " delete from PSJJR where F_YEAR = ? ";
PO.put("F_YEAR",String.valueOf(this.year));
}else{
sql = "";
}
PO.put("DELETESQL", sql);
RO = (JResponseObject) HolidayService.syncHoliday(PO,jsonResult);
if (RO == null) {
JOptionPane.showMessageDialog(this, "同步错误!\r\n原因:RO为空", "提示",
JOptionPane.ERROR_MESSAGE);
} else if (RO.ErrorCode == 1) {
JOptionPane.showMessageDialog(this, "同步成功!");
openCalendar(false);
} else {
JOptionPane.showMessageDialog(this, "同步错误!\r\n原因:"
+ RO.ErrorString, "提示", JOptionPane.ERROR_MESSAGE);
}
}
public String getDate(){
return this.year + "年" + StringFunction.fillBeforeValue(String.valueOf(month)) + "月";
}
@SuppressWarnings("unchecked")
private HashMap
HashMap
Map
PO.put("F_YEAR", String.valueOf(this.year));
PO.put("F_MONTH", StringFunction.fillBeforeValue(String.valueOf(month)));
JResponseObject RO = null;
try {
RO = (JResponseObject) HolidayService.listHoliday(PO,null);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (RO == null) {
JOptionPane.showMessageDialog(this, "读取错误!\r\n原因:RO为空", "提示",
JOptionPane.ERROR_MESSAGE);
} else if (RO.ErrorCode == 1) {
holidays = (HashMap
} else {
JOptionPane.showMessageDialog(this, "读取错误!\r\n原因:"
+ RO.ErrorString, "提示", JOptionPane.ERROR_MESSAGE);
}
return holidays;
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == nextMonthBtn) {
month = month + 1;
if (month > 12){
month = 1;
year += 1;
}
initDate(false);
changeComboBox();
try {
openCalendar(false);
} catch (Exception e1) {
e1.printStackTrace();
}
} else if (e.getSource() == previousMonthBtn) {
month = month - 1;
if (month < 1){
month = 12;
year -= 1;
}
initDate(false);
changeComboBox();
try {
openCalendar(false);
} catch (Exception e1) {
e1.printStackTrace();
}
} else if (e.getSource() == backTodayBtn){
try {
initDate(true);
changeComboBox();
openCalendar(false);
} catch (Exception e1) {
e1.printStackTrace();
}
}
showMessage.setText("日历:" + getDate());
}
public int getYear() {
return year;
}
public int getMonth() {
return month;
}
public static void main(String args[]) {
HolidayManageDlg dlg = new HolidayManageDlg();
dlg.show(true);
}
}
package com.vito.holiday.view;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.util.HashMap;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.BevelBorder;
import javax.swing.border.EtchedBorder;
/**
* @Description: 节假日管理-月对象
* @author wutp 2017年5月15日
* @version 1.0
*/
public class JMonthPanel extends JPanel{
/**
*
*/
private static final long serialVersionUID = 1L;
public final static String week[] = { "日", "一", "二", "三", "四", "五", "六" };
GridLayout gridLayout1 ,gridLayout2;
JPanel northPanel = new JPanel();
JPanel centerPanel = new JPanel();
JLabel titleName[] = new JLabel[7]; // 周一到周天的按键
JDayPanel[] dayPanel = new JDayPanel[42];// 用来输出日历的天的数组
HolidayManageDlg holidayManageDlg;
private String[] days;
private String date;
/**
* 节假日集合
*/
private HashMap
public JMonthPanel(HolidayManageDlg holidayManageDlg, String[] days, String date,HashMap
super();
try{
this.holidayManageDlg = holidayManageDlg;
this.days = days;
this.date = date;
this.holidays = holidays;
jbInit();
}catch(Exception e){
e.printStackTrace();
throw e;
}
}
private void jbInit(){
this.setLayout(new BorderLayout());
this.setBorder(new BevelBorder(BevelBorder.LOWERED));
this.add(northPanel,BorderLayout.NORTH);
this.add(centerPanel,BorderLayout.CENTER);
gridLayout1 = new GridLayout(1, 7, 1, 1);
gridLayout2 = new GridLayout(6, 7, 1, 1);
northPanel.setLayout(gridLayout1);
centerPanel.setLayout(gridLayout2);
centerPanel.setBorder(new EtchedBorder());
for (int i = 0; i < 7; i++) {
titleName[i] = new JLabel(week[i]);
titleName[i].setFont(new Font("TimesRoman",Font.BOLD,20));
titleName[i].setHorizontalAlignment(JLabel.CENTER);
titleName[i].setBackground(Color.gray);
titleName[i].setForeground(Color.black);
northPanel.add(titleName[i]);
}
for (int i = 0; i < 42; i++) {
dayPanel[i] = new JDayPanel(this,i,getHolidayStatus(days[i]));
centerPanel.add(dayPanel[i]);
}
}
public String[] getDays() {
return days;
}
public String getDate() {
return date;
}
public String getHolidayStatus(String key) {
String value ;
if(key == null){
return "0";
}
if(key.length() == 1){
key = "0"+key;
}
try{
value = this.holidays.get(key);
}catch(Exception e){
e.printStackTrace();
value = "0";
}
value = value == null ? "0" : value;
return value ;
}
}
package com.vito.holiday.view;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EtchedBorder;
import com.vito.holiday.service.HolidayService;
import com.vito.holiday.util.JResponseObject;
/*import jfoundation.bridge.classes.JResponseObject;
import jfoundation.dataobject.classes.JParamObject;
import jframework.foundation.classes.JActiveDComDM;*/
import com.vito.holiday.util.LunarFunction;
import com.vito.holiday.util.StringFunction;
/**
* @Description: 节假日管理-日对象
* @author wutp 2017年5月15日
* @version 1.0
*/
public class JDayPanel extends JPanel {
/**
*
*/
private static final long serialVersionUID = 1L;
BorderLayout borderLayout = new BorderLayout();
JPanel northPanel;
JPanel centerPanel;
JPanel sourthPanel;
JPanel eastPanel;
JPanel westPanel;
private JLabel gregorianLabel;
private JLabel lunarLabel;
private JLabel statusLabel;
private JPanel statusPanel;
private MyMouseListener mouseListener = new MyMouseListener();
private JMonthPanel jMonthPanel;
/**
* 在月单元格的位置
*/
private int dayId;
/**
* 是周几
*/
private String weekId ;
/**
* 是否是周末 true:是;false:否。
*/
private boolean week;
/**
* 是否是节假日,0:上班,1:休 。
*/
private String holiday ;
/**
* 阳历
*/
private String gregorianDay;
/**
* 农历
*/
private String chinaDayString;
public JDayPanel(JMonthPanel jMonthPanel, int i,String holiday) {
super();
this.jMonthPanel = jMonthPanel;
this.dayId = i;
this.holiday = holiday;
calculationData();
initUI();
}
private void calculationData(){
this.weekId = weekId(this.dayId % 7);
this.week = isWeek();
this.gregorianDay = jMonthPanel.getDays()[dayId] ==null ? "" : jMonthPanel.getDays()[dayId];
if(!"".equals(gregorianDay)){
try {
this.chinaDayString = LunarFunction.getLunarDayString(jMonthPanel.getDate()+StringFunction.fillBeforeValue(gregorianDay)+"日");
} catch (ParseException e) {
e.printStackTrace();
}
}
}
private String weekId(int i){
return JMonthPanel.week[i];
}
private boolean isWeek(){
if("六".equals(weekId) || "日".equals(weekId)){
return true;
}
return false;
}
private void initUI() {
this.setLayout(borderLayout);
this.setBorder(new EtchedBorder(5));
northPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
northPanel.setBackground(Color.white);
centerPanel = new JPanel(new BorderLayout());
centerPanel.setBackground(Color.white);
sourthPanel = new JPanel();
sourthPanel.setBackground(Color.white);
eastPanel = new JPanel();
eastPanel.setBackground(Color.white);
westPanel = new JPanel();
westPanel.setBackground(Color.white);
this.add(northPanel,BorderLayout.NORTH);
this.add(centerPanel,BorderLayout.CENTER);
this.add(sourthPanel,BorderLayout.SOUTH);
this.add(eastPanel,BorderLayout.EAST);
this.add(westPanel,BorderLayout.WEST);
if( !"".equals(gregorianDay)){
statusLabel = new JLabel();
statusLabel.setFont(new Font("TimesRoman",Font.PLAIN,15));
statusLabel.setVerticalAlignment(JLabel.CENTER);
statusLabel.setHorizontalAlignment(JLabel.CENTER);
statusLabel.setForeground(Color.white);
statusPanel = new JPanel();
statusPanel.setPreferredSize(new Dimension(20,20));
statusPanel.setSize(new Dimension(20,20));
statusPanel.setBackground(Color.white);
if( this.holiday.equals("1") ){
statusLabel.setText("休");
statusPanel.setBackground(Color.red);
}else if( week && this.holiday.equals("0") ){
statusLabel.setText("班");
statusPanel.setBackground(Color.black);
}else if( !week && this.holiday.equals("0") ){
statusLabel.setText(" ");
statusPanel.setBackground(Color.white);
}
statusPanel.add(statusLabel,BorderLayout.CENTER);
northPanel.add(statusPanel);
gregorianLabel = new JLabel();
gregorianLabel.enableInputMethods(true);
gregorianLabel.setHorizontalAlignment(JLabel.CENTER);
if(week){
gregorianLabel.setForeground(Color.red);
}
gregorianLabel.setText(gregorianDay);
lunarLabel = new JLabel();
lunarLabel.enableInputMethods(true);
lunarLabel.setHorizontalAlignment(JLabel.CENTER);
lunarLabel.setText(chinaDayString);
centerPanel.add(gregorianLabel,BorderLayout.CENTER);
centerPanel.add(lunarLabel,BorderLayout.SOUTH);
}
this.addMouseListener(mouseListener);
this.setVisible(true);
}
private boolean updateHolidays(String holiday) {
Map
PO.put("F_YEAR", String.valueOf(jMonthPanel.holidayManageDlg.getYear()));
PO.put("F_MONTH", StringFunction.fillBeforeValue(
String.valueOf(jMonthPanel.holidayManageDlg.getMonth())));
PO.put("F_VDATE", String.valueOf(jMonthPanel.holidayManageDlg.getYear())
+StringFunction.fillBeforeValue(String.valueOf(jMonthPanel.holidayManageDlg.getMonth()))
+StringFunction.fillBeforeValue(this.gregorianDay));
PO.put("F_JJR", holiday);
JResponseObject RO = null;
try {
RO = (JResponseObject) HolidayService.updateHoliday(PO,null);
} catch (Exception e) {
e.printStackTrace();
}
if (RO == null) {
JOptionPane.showMessageDialog(this, "保存错误!\r\n原因:RO为空", "提示",
JOptionPane.ERROR_MESSAGE);
} else if (RO.ErrorCode == 1) {
JOptionPane.showMessageDialog(this, "保存成功!");
return true;
} else {
JOptionPane.showMessageDialog(this, "保存错误!\r\n原因:"
+ RO.ErrorString, "提示", JOptionPane.ERROR_MESSAGE);
}
return false;
}
class MyMouseListener extends MouseAdapter {
public MyMouseListener(){
super();
}
public void mouseClicked(MouseEvent evt) {
if(evt.isMetaDown() || evt.getClickCount() == 2){
System.out.println("changeHoliday()");
changeHoliday();
}
}
}
/* */
/**
*
* @Description:
holiday:0 1
week:y n
0y 班 1 休
0n "" 1 休
1y 休 0 班
1n 休 0 ""
* @auther: wutp 2017年5月19日
* @return void
*/
private void changeHoliday(){
String temHoliday ;
if( this.holiday.equals("0") ){
temHoliday = "1";
if(updateHolidays(temHoliday)){
statusLabel.setText("休");
statusPanel.setBackground(Color.red);
this.holiday = temHoliday;
}
}else if( this.holiday.equals("1") && week ){
temHoliday = "0";
if(updateHolidays(temHoliday)){
statusLabel.setText("班");
statusPanel.setBackground(Color.black);
this.holiday = temHoliday;
}
}else if( this.holiday.equals("1") && !week ){
temHoliday = "0";
if(updateHolidays(temHoliday)){
statusLabel.setText(" ");
statusPanel.setBackground(Color.white);
this.holiday = temHoliday;
}
}
}
}
package com.vito.holiday.util;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
* @Description: 农历工具类
* @author wutp 2017年5月16日
* @version 1.0
*/
public class LunarFunction {
private int year;
private int month;
private int day;
private boolean leap;
private final static String chineseNumber[] = { "一", "二", "三", "四", "五", "六", "七",
"八", "九", "十", "十一", "十二" };
private static SimpleDateFormat chineseDateFormat = new SimpleDateFormat(
"yyyy年MM月dd日");
private final static long[] lunarInfo = new long[] { 0x04bd8, 0x04ae0, 0x0a570,
0x054d5, 0x0d260, 0x0d950, 0x16554, 0x056a0, 0x09ad0, 0x055d2,
0x04ae0, 0x0a5b6, 0x0a4d0, 0x0d250, 0x1d255, 0x0b540, 0x0d6a0,
0x0ada2, 0x095b0, 0x14977, 0x04970, 0x0a4b0, 0x0b4b5, 0x06a50,
0x06d40, 0x1ab54, 0x02b60, 0x09570, 0x052f2, 0x04970, 0x06566,
0x0d4a0, 0x0ea50, 0x06e95, 0x05ad0, 0x02b60, 0x186e3, 0x092e0,
0x1c8d7, 0x0c950, 0x0d4a0, 0x1d8a6, 0x0b550, 0x056a0, 0x1a5b4,
0x025d0, 0x092d0, 0x0d2b2, 0x0a950, 0x0b557, 0x06ca0, 0x0b550,
0x15355, 0x04da0, 0x0a5d0, 0x14573, 0x052d0, 0x0a9a8, 0x0e950,
0x06aa0, 0x0aea6, 0x0ab50, 0x04b60, 0x0aae4, 0x0a570, 0x05260,
0x0f263, 0x0d950, 0x05b57, 0x056a0, 0x096d0, 0x04dd5, 0x04ad0,
0x0a4d0, 0x0d4d4, 0x0d250, 0x0d558, 0x0b540, 0x0b5a0, 0x195a6,
0x095b0, 0x049b0, 0x0a974, 0x0a4b0, 0x0b27a, 0x06a50, 0x06d40,
0x0af46, 0x0ab60, 0x09570, 0x04af5, 0x04970, 0x064b0, 0x074a3,
0x0ea50, 0x06b58, 0x055c0, 0x0ab60, 0x096d5, 0x092e0, 0x0c960,
0x0d954, 0x0d4a0, 0x0da50, 0x07552, 0x056a0, 0x0abb7, 0x025d0,
0x092d0, 0x0cab5, 0x0a950, 0x0b4a0, 0x0baa4, 0x0ad50, 0x055d9,
0x04ba0, 0x0a5b0, 0x15176, 0x052b0, 0x0a930, 0x07954, 0x06aa0,
0x0ad50, 0x05b52, 0x04b60, 0x0a6e6, 0x0a4e0, 0x0d260, 0x0ea65,
0x0d530, 0x05aa0, 0x076a3, 0x096d0, 0x04bd7, 0x04ad0, 0x0a4d0,
0x1d0b6, 0x0d250, 0x0d520, 0x0dd45, 0x0b5a0, 0x056d0, 0x055b2,
0x049b0, 0x0a577, 0x0a4b0, 0x0aa50, 0x1b255, 0x06d20, 0x0ada0 };
public LunarFunction(Calendar cal) {
initLunarFunction(cal);
}
/**
* @Description:传出y年m月d日对应的农历. yearCyl3:农历年与1864的相差数 ?
* monCyl4:从1900年1月31日以来,闰月数
* dayCyl5:与1900年1月31日相差的天数,再加40 ?
* @auther: wutp 2017年5月16日
* @return void
*/
private void initLunarFunction(Calendar cal){
int leapMonth = 0;
Date baseDate = null;
try {
baseDate = chineseDateFormat.parse("1900年1月31日");
} catch (ParseException e) {
e.printStackTrace(); // To change body of catch statement use
// Options | File Templates.
}
// 求出和1900年1月31日相差的天数
int offset = (int) ((cal.getTime().getTime() - baseDate.getTime()) / 86400000L);
// 用offset减去每农历年的天数
// 计算当天是农历第几天
// i最终结果是农历的年份
// offset是当年的第几天
int iYear, daysOfYear = 0;
for (iYear = 1900; iYear < 2050 && offset > 0; iYear++) {
daysOfYear = yearDays(iYear);
offset -= daysOfYear;
}
if (offset < 0) {
offset += daysOfYear;
iYear--;
}
// 农历年份
year = iYear;
leapMonth = leapMonth(iYear); // 闰哪个月,1-12
leap = false;
// 用当年的天数offset,逐个减去每月(农历)的天数,求出当天是本月的第几天
int iMonth, daysOfMonth = 0;
for (iMonth = 1; iMonth < 13 && offset > 0; iMonth++) {
// 闰月
if (leapMonth > 0 && iMonth == (leapMonth + 1) && !leap) {
--iMonth;
leap = true;
daysOfMonth = leapDays(year);
} else
daysOfMonth = monthDays(year, iMonth);
offset -= daysOfMonth;
// 解除闰月
if (leap && iMonth == (leapMonth + 1))
leap = false;
if (!leap) {
}
}
// offset为0时,并且刚才计算的月份是闰月,要校正
if (offset == 0 && leapMonth > 0 && iMonth == leapMonth + 1) {
if (leap) {
leap = false;
} else {
leap = true;
--iMonth;
}
}
// offset小于0时,也要校正
if (offset < 0) {
offset += daysOfMonth;
--iMonth;
}
month = iMonth;
day = offset + 1;
}
/**
* @Description: 获取农历
* @auther: wutp 2017年5月16日
* @param calendar
* @param k
* @return
* @return String
* @throws ParseException
*/
public static String getLunarDayString(String gregorianDay) throws ParseException {
Calendar today = Calendar.getInstance();
today.setTime(chineseDateFormat.parse(gregorianDay));
LunarFunction lunar = new LunarFunction(today);
/*System.out.println("北京时间:" + chineseDateFormat.format(today.getTime())
+ " 农历" + lunar);*/
return getChinaDayString(lunar.day);
}
/**
* @Description:装换成汉字
* @auther: wutp 2017年5月16日
* @param day
* @return
* @return String
*/
private static String getChinaDayString(int day) {
String chineseTen[] = { "初", "十", "廿", "卅" };
int n = day % 10 == 0 ? 9 : day % 10 - 1;
if (day > 30)
return "";
if (day == 10)
return "初十";
else
return chineseTen[day / 10] + chineseNumber[n];
}
/**
* @Description:传回农历 y年的总天数
* @auther: wutp 2017年5月16日
* @param y
* @return
* @return int
*/
private final static int yearDays(int y) {
int i, sum = 348;
for (i = 0x8000; i > 0x8; i >>= 1) {
if ((lunarInfo[y - 1900] & i) != 0)
sum += 1;
}
return (sum + leapDays(y));
}
/**
* @Description:传回农历 y年闰月的天数
* @auther: wutp 2017年5月16日
* @param y
* @return
* @return int
*/
private final static int leapDays(int y) {
if (leapMonth(y) != 0) {
if ((lunarInfo[y - 1900] & 0x10000) != 0){
return 30;
} else{
return 29;
}
} else {
return 0;
}
}
/**
* @Description:传回农历 y年闰哪个月 1-12 , 没闰传回 0
* @auther: wutp 2017年5月16日
* @param y
* @return
* @return int
*/
private final static int leapMonth(int y) {
return (int) (lunarInfo[y - 1900] & 0xf);
}
/**
* @Description:传回农历 y年m月的总天数
* @auther: wutp 2017年5月16日
* @param y
* @param m
* @return
* @return int
*/
private final static int monthDays(int y, int m) {
if ((lunarInfo[y - 1900] & (0x10000 >> m)) == 0)
return 29;
else
return 30;
}
/**
* @Description:传回农历 y年的生肖
* @auther: wutp 2017年5月16日
* @return
* @return String
*/
@Deprecated
public final String animalsYear() {
final String[] Animals = new String[] { "鼠", "牛", "虎", "兔", "龙", "蛇",
"马", "羊", "猴", "鸡", "狗", "猪" };
return Animals[(year - 4) % 12];
}
/**
* @Description:传入 offset 传回干支, 0=甲子
* @auther: wutp 2017年5月16日
* @return
* @return String
*/
@Deprecated
public final String cyclical() {
int num = year - 1900 + 36;
return (cyclicalm(num));
}
/**
* @Description:传入 月日的offset 传回干支, 0=甲子
* @auther: wutp 2017年5月16日
* @param num
* @return
* @return String
*/
@Deprecated
private final static String cyclicalm(int num) {
final String[] Gan = new String[] { "甲", "乙", "丙", "丁", "戊", "己", "庚",
"辛", "壬", "癸" };
final String[] Zhi = new String[] { "子", "丑", "寅", "卯", "辰", "巳", "午",
"未", "申", "酉", "戌", "亥" };
return (Gan[num % 10] + Zhi[num % 12]);
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
public boolean isLeap() {
return leap;
}
public void setLeap(boolean leap) {
this.leap = leap;
}
@Override
public String toString() {
return year + "年" + (leap ? "闰" : "") + chineseNumber[month - 1] + "月"
+ getChinaDayString(day);
}
/* public static void main(String[] args) {
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
cal.setTimeZone(TimeZone.getDefault());
System.out.println("公历日期:" + sdf.format(cal.getTime()));
LunarFunction lunar = new LunarFunction(cal);
System.out.print("农历日期:");
System.out.print(lunar.year + "年 ");
System.out.print(lunar.month + "月 ");
System.out.println(getChinaDayString(lunar.day));
System.out.println("*************");
System.out.println(lunar);
}*/
/* public static void main(String[] args) throws ParseException {
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
cal.setTimeZone(TimeZone.getDefault());
System.out.println("公历日期:" + sdf.format(cal.getTime()));
LunarFunction lunar = new LunarFunction(cal);
System.out.println(getChinaDayString(lunar.day));
}*/
}
package com.vito.holiday.util;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Map;
import com.alibaba.fastjson.JSON;
/**
* @Description: 在线获取节假日
* 节假日API bitefu对于本应用场景有一个无法解决的问题 所以只支持easybots
* 1、bitefu: http://tool.bitefu.net/jiari/
* 2、easybots: http://www.easybots.cn/holiday_api.net
* @author wutp 2017年5月17日
* @version 1.0
*/
public class HolidayWebTool {
public final static String SERVER_EASYBOTS = "EASYBOTS";
public final static String SERVER_BITTEFU = "BITTEFU";
public final static String URL_EASYBOTS_CHECK = "http://www.easybots.cn/api/holiday.php?d=";
public final static String URL_EASYBOTS_GET = "http://www.easybots.cn/api/holiday.php?m=";
public final static String URL_BITTEFU = "http://tool.bitefu.net/jiari/?d=";
/**
* @Description:
* @auther: wutp 2017年5月16日
* @param serverType
* @param httpArg
* @return
* @return Map
*/
@SuppressWarnings("unchecked")
public static Map
Map
BufferedReader reader = null;
String result = null;
StringBuffer sbf = new StringBuffer();
try {
URL url = new URL(httpUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.connect();
InputStream is = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
String strRead = null;
while ((strRead = reader.readLine()) != null) {
sbf.append(strRead);
sbf.append("\r\n");
}
reader.close();
result = sbf.toString();
map = (Map
} catch (Exception e) {
e.printStackTrace();
}
return map;
}
/**
* @Description:参数
* @auther: wutp 2017年5月17日
* @param httpArg
* @return
* @return String
*/
public static String requestCheck(String serverType,String httpArg) {
String result = null;
if(serverType.equals(SERVER_EASYBOTS)){
result = requestCheckByEasybots(httpArg);
}else if(serverType.equals(SERVER_BITTEFU)){
result = requestCheckBybitefu(httpArg);
}
return result;
}
private static String requestCheckByEasybots(String httpArg) {
return null;
}
private static String requestCheckBybitefu(String httpArg) {
return null;
}
/**
* @Description:参数
* @auther: wutp 2017年5月17日
* @param httpArg
* @return
* @return String
*/
public static ArrayList
ArrayList
if(serverType.equals(SERVER_EASYBOTS)){
list = requestGetMonthByEasybots(httpArg);
}else if(serverType.equals(SERVER_BITTEFU)){
list = requestGetMonthByBitefu(httpArg);
}
return list;
}
private static ArrayList
try {
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private static ArrayList
try {
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* @Description:参数
* @auther: wutp 2017年5月17日
* @param httpArg
* @return
* @return String
*/
public static ArrayList
ArrayList
if(serverType.equals(SERVER_EASYBOTS)){
list = requestGetYearByEasybots(year);
}else if(serverType.equals(SERVER_BITTEFU)){
list = requestGetYearByBitefu(year);
}
return list;
}
private static ArrayList
ArrayList
String httpArg = "";
String month ;
for(int i=1;i<13;i++){
month = String.valueOf(i).length() == 1 ? "0"+i : ""+i ;
httpArg += year+month+",";
}
if(httpArg.length() > 0){
httpArg = httpArg.substring(0, httpArg.length()-1);
}
String httpUrl = URL_EASYBOTS_GET+httpArg;
try {
Map
list = map2ListByEasybots(map);
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
private static ArrayList
ArrayList
String httpUrl=URL_BITTEFU+year;
try {
Map
list = map2ListByBitefu(map);
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
private static ArrayList
ArrayList
for (Map.Entry
if(entry.getValue() != null && entry.getValue().toString().length() > 1){
String[] jjr = entry.getValue().toString().split(",");
for(int i=0;i
}
}
}
return list;
}
/**
* @Description:装换成日期格式
* @auther: wutp 2017年5月18日
* @param key
* @param value
* @return
* @return String[]
*/
private static String[] string2Array(String key,String value){
String[] date = new String[2];
if(value.length() ==9){
if(value.startsWith("{")){
value = value.replace("{", "");
}else{
value = value.replace("}", "");
}
}
value = value.replaceAll("\"", "");
date = value.split(":");
date[0] = key+date[0];
date[1] = "2".equals(date[1]) ? "1" : date[1] ;
return date;
}
private static ArrayList
ArrayList
for (Object v : map.values()) {
System.out.println("value= " + v.toString());
String[] jjr = v.toString().split(",");
for(int i=0;i
jjr[i],"2"
});
}
}
return list;
}
public static void main(String[] args) {
// String[] date = string2Array("201611","{\"19\":\"1\"");
// System.out.println(date[0]+"-"+date[1]);
//判断今天是否是工作日 周末 还是节假日
// SimpleDateFormat f=new SimpleDateFormat("yyyyMMdd");
// String httpArg=f.format(new Date());
// System.out.println(httpArg);
ArrayList
String[] date = null;
System.out.println("共有"+jsonResult.size()+"天节假日!");
for(int i=0;i
System.out.println(date[0]+":"+date[1]);
}
//System.out.println(jsonResult);
}
}
CREATE TABLE PSJJR(
F_YEAR VARCHAR(4) DEFAULT ' ' NOT NULL,
F_MONTH VARCHAR(2) DEFAULT ' ' NOT NULL,
F_VDATE VARCHAR(8) DEFAULT ' ' NOT NULL,
F_JJR VARCHAR(1) DEFAULT ' ' NOT NULL
);
CREATE UNIQUE INDEX PSJJR ON PSJJR (F_VDATE);
主要代码就这些,一下是详细代码,欢迎交流。
http://download.csdn.net/detail/jiluoji/9853240