Javaベースの一括生成スクリプト


最近、业务は表の上にいくつかのフィールドを追加する必要があります.月ごとに表を分けるために、3年间36各表の修正スクリプトを提供する予定です.私は怠け者になりたいです.简単なJavaを书きました.一度に生成します.ソースコードは以下のように、私は一つ一つ説明しません.主な考え方は文字列を交换することです.简単です.
    
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class        {
	public static void main(String[] args) throws Exception {
		String baseStr = readFileContent("f:/saveFile/  .sql", "utf-8");
		String tmpStr=null;
		List<String> monthsList = getEveryMonth("2011-01-01", "2015-01-01", "");
		StringBuffer result=new StringBuffer(5120);
		for (String str : monthsList) {
			tmpStr=baseStr.replaceAll("#yyyymm#", str);
			result.append(tmpStr).append("\r
"); } writeStrToFile(result.toString(),"f:/saveFile/ .sql","utf-8"); } public static void writeStrToFile(String str, String filePath, String charsetName) throws Exception { if (charsetName == null) { charsetName = "utf-8"; } File file1 = new File(filePath); OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream( file1), charsetName); out.write(str); out.close(); } public static String readFileContent(String fileName, String charsetName) throws Exception { if (charsetName == null) { charsetName = "utf-8"; } File file = new File(fileName); if (!file.exists() || file.isDirectory()) { return null; } InputStreamReader read = new InputStreamReader( new FileInputStream(file), charsetName);// StringBuffer result = new StringBuffer((int) file.length()); BufferedReader bufferedReader = new BufferedReader(read); String lineTxt = null; while ((lineTxt = bufferedReader.readLine()) != null) { result.append(lineTxt).append("\r
"); } return result.toString(); } public static List<String> getEveryMonth(String beginDateStr, String endDateStr, String split) { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); Date beginDate = null, endDate = null; try { beginDate = format.parse(beginDateStr); endDate = format.parse(endDateStr); } catch (ParseException e) { e.printStackTrace(); } Calendar c = Calendar.getInstance(); c.setTime(endDate); int endYear = c.get(Calendar.YEAR); int endMonth = c.get(Calendar.MONTH) + 1; c.setTime(beginDate); int startYear = c.get(Calendar.YEAR); int startMonth = c.get(Calendar.MONTH) + 1; int totalM = 12 * (endYear - startYear) + endMonth - startMonth; List<String> everyMonths = new ArrayList<String>(); String tmpStr = formatYear(startYear) + split + formatMonthDay(startMonth); everyMonths.add(tmpStr); for (int i = 0; i < totalM; i++) { startMonth = startMonth + 1; if (startMonth > 12) { startMonth = 1; startYear += 1; } tmpStr = formatYear(startYear) + split + formatMonthDay(startMonth); everyMonths.add(tmpStr); } return everyMonths; } public static String formatMonthDay(int decimal) { DecimalFormat df = new DecimalFormat("00"); return df.format(decimal); } public static String formatYear(int decimal) { DecimalFormat df = new DecimalFormat("0000"); return df.format(decimal); } }

    2日以内の毎日を手に入れたい場合は、次のようにします.
    http://bbs.ibeifeng.com/simple/index.php?t14118.html
    コードも持ってきました.以下のようにします.
   
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * From:http://bbs.ibeifeng.com/simple/index.php?t14118.html
 * 
 * @author Administrator
 * 
 */
public class GetEveryDayHelper {
	public static void main(String[] args) {
		List<String> list = GetEveryDayHelper.getEveryday("2014-01-01",
				"2014-03-02", "_");
		for (String result : list) {
			System.out.println(result);
		}
	}

	/**         */
	private static final int[] DAYS_P_MONTH_LY = { 31, 29, 31, 30, 31, 30, 31,
			31, 30, 31, 30, 31 };

	/**          */
	private static final int[] DAYS_P_MONTH_CY = { 31, 28, 31, 30, 31, 30, 31,
			31, 30, 31, 30, 31 };

	/**        、 、  */
	private static final int Y = 0, M = 1, D = 2;

	/**
	 *                       
	 * 
	 * @param date
	 * @return
	 */
	public static int[] splitYMD(String date) {
		date = date.replace("-", "");
		int[] ymd = { 0, 0, 0 };
		ymd[Y] = Integer.parseInt(date.substring(0, 4));
		ymd[M] = Integer.parseInt(date.substring(4, 6));
		ymd[D] = Integer.parseInt(date.substring(6, 8));
		return ymd;
	}

	/**
	 *                  
	 * 
	 * @param year
	 * @return
	 */
	public static boolean isLeapYear(int year) {
		return ((year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0)));
	}

	/**
	 *    1 
	 * 
	 * @param year
	 * @param month
	 * @param day
	 * @return
	 */
	private static int[] addOneDay(int year, int month, int day) {
		if (isLeapYear(year)) {
			day++;
			if (day > DAYS_P_MONTH_LY[month - 1]) {
				month++;
				if (month > 12) {
					year++;
					month = 1;
				}
				day = 1;
			}
		} else {
			day++;
			if (day > DAYS_P_MONTH_CY[month - 1]) {
				month++;
				if (month > 12) {
					year++;
					month = 1;
				}
				day = 1;
			}
		}
		int[] ymd = { year, month, day };
		return ymd;
	}

	/**
	 *                 
	 * 
	 * @param decimal
	 * @return
	 */
	public static String formatMonthDay(int decimal) {
		DecimalFormat df = new DecimalFormat("00");
		return df.format(decimal);
	}

	/**
	 *              
	 * 
	 * @param decimal
	 * @return
	 */
	public static String formatYear(int decimal) {
		DecimalFormat df = new DecimalFormat("0000");
		return df.format(decimal);
	}

	/**
	 *              
	 * 
	 * @param begin
	 * @param end
	 * @return
	 * @throws ParseException
	 */
	public static long countDay(String begin, String end) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Date beginDate, endDate;
		long day = 0;
		try {
			beginDate = format.parse(begin);
			endDate = format.parse(end);
			day = (endDate.getTime() - beginDate.getTime())
					/ (24 * 60 * 60 * 1000);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return day;
	}

	/**
	 *           
	 * 
	 * @param beginDate
	 *            endDate
	 * @param days
	 * @return
	 */
	public static List<String> getEveryday(String beginDate, String endDate,
			String split) {
		long days = countDay(beginDate, endDate);
		int[] ymd = splitYMD(beginDate);
		List<String> everyDays = new ArrayList<String>();
		everyDays.add(beginDate);
		for (int i = 0; i < days; i++) {
			ymd = addOneDay(ymd[Y], ymd[M], ymd[D]);
			everyDays.add(formatYear(ymd[Y]) + split + formatMonthDay(ymd[M])
					+ split + formatMonthDay(ymd[D]));
		}
		return everyDays;
	}

}

    全文が終わる.