Java-based Time calculation program

Source: Internet
Author: User

On the way to school by train, I suddenly thought about the weekly distribution of the date of nearly 30 years.

Write code in Java when you go to school

Very low code.

First of all, my idea is to use the time format to combine the month with the date, such as "0904"

It is then used as the primary key for the data table, and then by setting the column with the most table from Sunday to Saturday, the initial value is 0,

And then through a sub-function to implement the insertion

The idea of inserting is read first, judging whether the current date is empty

If blank, the item is inserted if it is not empty, and the current item is added 1 days in the specified week;

The idea is clear, and the code is simple.

The first is the class of database operation;

Calendardatabase.java

ImportJava.sql.DriverManager;ImportJava.sql.ResultSet;Importjava.sql.SQLException;Importjava.sql.Statement;Importjava.sql.Connection; Public classcalendardatabase {String Createtb= "CREATE Table Calendarb (" + "mydate varchar (4) Primary key," + "Sunday int (4)," + "Monday int (4)," + "Tuesday Int (4), "+" Wednesday int (4), "+" Thusday int (4), "+" Friday int (4), "+" Saturday int (4)) "; String Drivers= "Com.mysql.jdbc.Driver"; String URL= "jdbc:mysql://localhost:3306/"; String User= ""; String Password= ""; String DatabaseName= "Calendardb"; String myweek[]= {"Sunday", "Monday", "Tuesday", "Wednesday", "Thusday", "Friday", "Saturday" };    Connection Conn;  Publiccalendardatabase () {Statement St=NULL; Try{class.forname (drivers); Conn=drivermanager.getconnection (URL, user, password); St=conn.createstatement (); } Catch(ClassNotFoundException e) {e.printstacktrace (); } Catch(SQLException e) {e.printstacktrace (); } Catch(Exception e) {e.printstacktrace (); }        if(CreateDatabase (ST)) {System.out.println ("Database Create Succeed"); } Else{System.out.println ("Database Exited"); }        if(CreateTable (St, Createtb)) {System.out.println ("Table Create Succeed"); } Else{System.out.println ("Table Create Failed"); }        Try{st.close (); } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); }    }     Public voidInsertorquit (String datestr,intWeekin) {String Tempsql= "Select" + Myweek[weekin] + "from Calendarb where mydate= '" + datestr + "'"; Try{Statement St=conn.createstatement (); ResultSet RS=st.executequery (Tempsql); if(!Rs.next ()) {St.execute ("INSERT into Calendarb values ('" + datestr + "', 1,1,1,1,1,1,1)"); } Else {                if(Rs.getint (Myweek[weekin]) = = 1) St.execute ("Update Calendarb Set" + Myweek[weekin] + "=" + Myweek[weekin] + "-1 where mydate= '" + datestr + "'"); }        } Catch(Exception e) {//Todo:handle ExceptionE.printstacktrace (); }    }     Public voidInsert (String datestr,intWeekin) {String Tempsql= "Select" + Myweek[weekin] + "from Calendarb where mydate= '" + datestr + "'"; Try{Statement St=conn.createstatement (); ResultSet RS=st.executequery (Tempsql); if(!Rs.next ()) {St.execute ("INSERT into Calendarb values ('" + datestr + "', 0,0,0,0,0,0,0)"); } Else{St.execute ("Update Calendarb Set" + Myweek[weekin] + "=" + Myweek[weekin] + "+1 where mydate= '" + Datestr + " ‘"); }        } Catch(Exception e) {//Todo:handle ExceptionE.printstacktrace (); }    }     Public BooleanCreateDatabase (Statement St) {Booleantemp =false; String Createsql= "CREATE DATABASE" +DatabaseName; Try{st.execute (createsql); Temp=true; } Catch(Exception e) {//Todo:handle Exceptiontemp =false; }        Try{St.execute ("Use" +databaseName); } Catch(Exception e) {//Todo:handle Exception        }        returntemp; }     Public BooleanCreateTable (Statement St, String mSql) {Booleantemp =false; Try{st.execute (MSQL); Temp=true; } Catch(Exception e) {//Todo:handle Exceptiontemp =false; }        returntemp; }}


Next is the main program

ImportJava.text.SimpleDateFormat;ImportJava.util.Calendar;Importjava.util.Date;ImportJava.util.GregorianCalendar; Public classCalendarsearch { Public Static voidMain (string[] args) {//TODO auto-generated Method StubCalendardatabase calendardatabase=Newcalendardatabase (); Date Date=NewDate (); Calendar Calendar=NewGregorianCalendar ();        Calendar.settime (date); SimpleDateFormat SF=NewSimpleDateFormat ("MMdd"); SimpleDateFormat SDF=NewSimpleDateFormat ("YyyyMMdd"); LongStartTime =System.currenttimemillis ();  while(true) {            if(Sdf.format (date). Equals ("19900101")) {                 Break; } Calendardatabase.insert (Sf.format (date), Calendar.get (Calendar.day_of_week)-1); Calendar.add (Calendar. DATE,-1); Date=Calendar.gettime (); }        LongEndTime =System.currenttimemillis (); System.out.println ("We have spent" + (Endtime-starttime) + "MS"); }}

It takes about 66181ms to finish running a program;

It's interesting to run out of results.

0229 of the week is evenly distributed, all day

The other days of the week distribution is also very uniform, the vast majority of 4 days, a small number of the 3-day;

Think carefully, 365 days a year for more than 52 weeks, so if this year's 0904 is Monday, then last year's 0904 is Sunday

A leap year is a recursive two, that is, +1+1+1+2+1+1+1+2, because the week number is only from one to seven, the cycle period is 7, and the leap year cycle is 4

One 1 per cycle, 3+2+3=8,8-7=1 in the cycle of adjacent leap years; According to this method, it is 7 cycles, so the period distribution of the date is equal.

Above, is a very boring program.

Java-based Time calculation program

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.