The method of judging the legal holidays of Spring Festival, Dragon Boat Festival and mid-Autumn festival based on Java code _java

Source: Internet
Author: User
Tags dateformat getdate

First, the preface

I've had a problem with my work recently. The back end has a timed task, need to use the Java daily judgment statutory holidays, weekends, work and so on, in fact, want to alone through the logic of what to judge the holiday of China's legal holidays, basic impossible, because the country each year's holiday may not be the same, is artificially set;

So only rely on other means, can think of the more reliable as follows:

1. Network interface: Some data Services Chamber of Commerce to provide, or to collect money, or the number of restrictions, and so on various issues, the effect is not ideal, controllability is poor, I have not tried, such as:

https://www.juhe.cn/docs/api/id/177/aid/601

Or

Http://apistore.baidu.com/apiworks/servicedetail/1116.html

2. Online analysis of web information, get the holiday situation: seriously rely on the Web page to be resolved, so when choosing a site, to find a little more reliable point;

3. According to the national statutory holiday holiday, the annual input system, this if the customer is not afraid of trouble. is still more reliable;

This demo will choose the second to achieve;

Second, the use of Htmlunit online analysis of web information, access to the holiday situation

The beginning is to use Jsoup to parse the Web page, the effect is not ideal, if the Web page is dynamically generated, with Jsoup encountered a variety of problems, so changed to Htmlunit, in total htmlunit or very powerful, can simulate the browser running, known as Java Browser Open source implementation;

First go to the website to download the relevant jar package, and read the relevant documents:

http://htmlunit.sourceforge.net/

I am here to parse the page is 360 calendar:

http://hao.360.cn/rili/

The calendar interface is as follows:


The parsed HTML format is as follows:


Implementation steps:

1, loading page;

2, the cycle waiting for the page load completed (there may be some dynamic pages, is generated with JavaScript);

3, according to the Web page format parsing HTML content, and extract the key information into the package of good objects;

Note the point:

1, the difficulty is to determine whether vacation and vacation types, because the original page does not indicate the type of holiday each day, so the logic here to implement their own, the details of the reference code;

2, the reason why there is a static latestvocationname variable, is to prevent the following conditions (the probability of a very low occurrence of this case; PS: The method is called once a day, the variable takes effect):

Code implementation:

Define a Chinese date class:

Package com.pichen.tools.getDate;
Import Java.util.Date; public class Chinadate {/** * Gregorian time/private Date solardate;/** * Lunar day/private String lunar;/** * Gregorian day/Private STR
ING Solar;
/** * Whether is sue * * Private Boolean isvacation = false;
/** * If the holiday is the name of the vacation/private String vacationname = "non-holiday";
/** * is a class * * Private Boolean isworkflag = false;
Private Boolean issaturday = false;
Private Boolean issunday = false; /** * @return The solardate/public Date getsolardate () {return solardate;}/** * @param solardate the solardate to SE t/public void Setsolardate (Date solardate) {this.solardate = solardate;}/** * @return the lunar/public String get
Lunar () {return lunar.}/** * @param lunar the Lunar to set */public void Setlunar (String lunar) {This.lunar = lunar;  /** * @return The solar/public String Getsolar () {return solar;}/** * @param solar "solar to set */public void Setsolar (String solar) {this.solar = solar;}/** * @return the isvacation/public boOlean Isvacation () {return isvacation.}/** * @param isvacation the isvacation to set */public void Setvacation (Boolean isvacation) {this.isvacation = isvacation}/** * @return the Vacationname/public String Getvacationname () {return
Vacationname; /** * @param vacationname the Vacationname to set */public void Setvacationname (String vacationname) {vacationname = V
Acationname; }/** * @return the Isworkflag/public boolean isworkflag () {return isworkflag;}/** * @param isworkflag the ISWORKFLA
G to set */public void Setworkflag (Boolean isworkflag) {this.isworkflag = Isworkflag;}/** * @return the Issaturday * * public Boolean issaturday () {return issaturday.}/** * @param issaturday the Issaturday to set */public void Setsaturda Y (Boolean issaturday) {this.issaturday = issaturday}/** * @return the Issunday/public boolean issunday () {return I
Ssunday; }/** * @param issunday the Issunday to set */public void Setsunday (Boolean issunday) {this.issunday = IssunDay }
}

Parse the Web page and invoke demo to print this month's details, and the day details:

Package com.pichen.tools.getDate;
Import java.io.IOException;
Import java.net.MalformedURLException;
Import Java.text.DateFormat;
Import java.text.ParseException;
Import Java.text.SimpleDateFormat;
Import java.util.ArrayList;
Import Java.util.Date;
Import java.util.List;
Import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
Import com.gargoylesoftware.htmlunit.WebClient;
Import com.gargoylesoftware.htmlunit.html.DomNodeList;
Import com.gargoylesoftware.htmlunit.html.HtmlElement;
Import Com.gargoylesoftware.htmlunit.html.HtmlPage; public class Main {private static string Latestvocationname= ' "; public string Getvocationname (domnodelist<
Htmlelement> htmlelements, String date) throws parseexception{string rst = "";
Boolean pasttimeflag = false;
DateFormat DateFormat = new SimpleDateFormat ("Yyyy/mm/dd");
Date paramdate = Dateformat.parse (date); if (new Date (). GetTime () >= paramdate.gettime ()) {Pasttimeflag = true;}//first step//jugde if can get Vocation name F RoM HTML page for (int i = 0; i < htmlelements.size (); i++) {htmlelement element = Htmlelements.get (i); if (element.getattr
Ibute ("Class"). IndexOf ("Vacation")!=-1) {Boolean hitflag = false;
String voationname = "";
for (; i < htmlelements.size (); i++) {HtmlElement elementtmp = Htmlelements.get (i);
String lidate = Elementtmp.getattribute ("date");
list 

Run the program with the correct result:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.