Skip to content

Instantly share code, notes, and snippets.

@tonetheman
Created July 11, 2013 20:29
Show Gist options
  • Select an option

  • Save tonetheman/5978953 to your computer and use it in GitHub Desktop.

Select an option

Save tonetheman/5978953 to your computer and use it in GitHub Desktop.
shows date interval effects in mysql, done in java, mainly how things bunch up when you choose INTERVAL MONTH
import java.sql.*;
public class Mysqldates {
Connection db =null;
public void run() throws Exception {
db = DriverManager.getConnection("jdbc:mysql://localhost/test");
PreparedStatement st = null;
PreparedStatement st2 = null;
try {
String base = "2013-01-01";
int base_interval = 1;
String sql = "select date_add(?, interval ? day)";
String sql2 = "select date_add(?, interval 1 month)";
st2 = db.prepareStatement(sql2);
st = db.prepareStatement(sql);
for(int i=0;i<365;i++) {
st.setString(1, base);
st.setInt(2, base_interval);
ResultSet rs = st.executeQuery();
rs.next();
String newDay = rs.getString(1);
st2.setString(1, newDay);
ResultSet rs2 = st2.executeQuery();
rs2.next();
String newMonth = rs2.getString(1);
base_interval++;
System.out.println(newDay + " " + newMonth);
}
} finally {
if (st!=null) {
st.close();
}
}
}
public void close() throws Exception {
if (db!=null) {
db.close();
db = null;
}
}
public static void main(String[] args) throws Exception {
Mysqldates me = new Mysqldates();
try {
me.run();
} finally {
me.close();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment