使用Java将日历信息保存到数据库中

使用Java将日历信息保存到数据库中

直接上代码吧

建表语句

CREATE TABLE
    calendar_info
    (
        date_info DATE NOT NULL COMMENT '当前日期',
        week_of_year VARCHAR(10) COMMENT '第几周',
        day_of_week VARCHAR(20) COMMENT '周几',
        date_flag VARCHAR(1) COMMENT '0 上班  1周末 2节假日 ',
        date_remark VARCHAR(255) COMMENT '日期备注',
        PRIMARY KEY (date_info)
    )
    ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='日历表';

主要实现逻辑类

	{
Calendar c_begin = new GregorianCalendar();
		Calendar c_end = new GregorianCalendar();
		DateFormatSymbols dfs = new DateFormatSymbols();
		String[] weeks = dfs.getWeekdays();
		c_begin.set(2019, 0, 1); // Calendar的月从0-11,所以4月是3.
		c_end.set(2020, 0, 1); // Calendar的月从0-11,所以5月是4.
		int count = 1;
		/*
		 * cal1.add(Calendar.DAY_OF_MONTH,1); cal1.add(Calendar.DAY_OF_YEAR,1);
		 * cal1.add(Calendar.DATE,1); 就单纯的add操作结果都一样,因为都是将日期+1,区别就是在月的日期中加1还是年的日期中加1
		 * 但是Calendar设置DAY_OF_MONTH和DAY_OF_YEAR的目的不是用来+1
		 * 将日期加1,这通过cal1.add(Calendar.DATE,1)就可以实现
		 * DAY_OF_MONTH的主要作用是cal.get(DAY_OF_MONTH),用来获得这一天在是这个月的第多少天
		 * Calendar.DAY_OF_YEAR的主要作用是cal.get(DAY_OF_YEAR),用来获得这一天在是这个年的第多少天。
		 * DAY_OF_WEEK,用来获得当前日期是一周的第几天
		 */
		c_end.add(Calendar.DAY_OF_YEAR, 1); // 结束日期下滚一天是为了包含最后一天

		while (c_begin.before(c_end)) {
			CalendarInfo c = new CalendarInfo();
			System.out.println("第" + count + "周  日期:" + new java.sql.Date(c_begin.getTime().getTime()) + ","
					+ weeks[c_begin.get(Calendar.DAY_OF_WEEK)]);
			c.setDateInfo(new java.sql.Date(c_begin.getTime().getTime()));
			c.setDayOfWeek(weeks[c_begin.get(Calendar.DAY_OF_WEEK)]);
			c.setWeekOfYear("第" + count + "周");
			SimpleDateFormat f = new SimpleDateFormat("yyyyMMdd");
			String httpArg = f.format(c.getDateInfo());
			String jsonResult = holiday.request(httpArg);
			Map stringToMap =  JSONObject.parseObject(jsonResult); 
	        String res=(String)stringToMap.get(httpArg);
	        c.setDateFlag(res);
			if (c_begin.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
				count++;
			}
			c_begin.add(Calendar.DAY_OF_YEAR, 1);
			calendarInfoMapper.insert(c);
		}
	}
    public static String request( String httpArg) {
        String httpUrl="http://www.easybots.cn/api/holiday.php";
        BufferedReader reader = null;
        String result = null;
        StringBuffer sbf = new StringBuffer();
        httpUrl = httpUrl + "?d=" + httpArg;

        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<String,Object> stringToMap =  JSONObject.parseObject(result);
            String res=(String)stringToMap.get(httpArg);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
	

实体类

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@ApiModel(description = "日历表数据实体")
public class CalendarInfo implements Serializable  {
	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = 1L;
	/**
	 * 当前日期
	 */
	@ApiModelProperty(value = "当前日期")
	private Date dateInfo;
	/**
	 * 第几周
	 */
	@ApiModelProperty(value = "第几周")
	private String weekOfYear;
	/**
	 * 周几
	 */
	@ApiModelProperty(value = "周几")
	private String dayOfWeek;
	/**
	 * 0 上班  1周末 2节假日 
	 */
	@ApiModelProperty(value = "0 上班  1周末 2节假日 ")
	private String dateFlag;
	/**
	 * 日期备注
	 */
	@ApiModelProperty(value = "日期备注")
	private String dateRemark;
	}

dao类

@Mapper
public interface CalendarInfoMapper{

	
	 /**
     *  根据ID查找对应的记录
     */
	CalendarInfo queryInfoById(String id);
	
	 /**
     *  查询总条数
     */
	int selectCount();
	
	
	/**
     *  按照条件查询
     */
	List<CalendarInfo> selectByCondition(CalendarInfo entity);
	
	/**
	 *  按照条件查询 一条数据
	 */
	 CalendarInfo  selectOneByCondition(CalendarInfo entity);
	
	/**
     *  按照条件查询记录对应的条数
     */
	int selectCountByCondition(CalendarInfo entity);
	
	/**
     *  按照id更新
     */
	int updateById(CalendarInfo entity);

	/**
     *  按照id删除
     */
	int deleteById(String id);

	/**
     *  新增
     */
	int insert(CalendarInfo entity);
	
	/**
     *  按照集合新增
     */
	int insertList(List<CalendarInfo> entityList);
	
	
	/**
     *  获取全部的数据
     */
	List<CalendarInfo>  getList();

	/**
     *  批量更新
     */
	int updateList(List<CalendarInfo> entityList);


}

mapperXml

<insert id="insertList">
		insert into
		calendar_info
		(date_info,week_of_year,day_of_week,date_flag,date_remark)
		values
		<foreach collection="list" item="item" index="index" separator="," >
		(#{item.dateInfo},#{item.weekOfYear},#{item.dayOfWeek},#{item.dateFlag},#{item.dateRemark})
		</foreach>
	</insert>
已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页