Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ### これは何?
- 指定日時のX営業日前を計算するJavaのサンプルコード
- * 仕様
- * 計算対象はコードに埋め込み m(_ _)m
- * 土日は何もしなくても、営業日外としてる
- * その他に営業日外としたい日は `holiday.txt` に書いておく
- ```
- [tomohiro.koike@ java]$ cat holiday.txt
- 2018-11-3
- 2018-11-15
- ```
- ```eigyoubi_keisan.java
- import java.util.Calendar;
- import java.io.*;
- class EigyoubiKeisan {
- public static void main(String[] args) {
- String target = "20181115";
- int num = 9; // 営業日
- int year = Integer.parseInt(target.substring(0,4));
- int month = Integer.parseInt(target.substring(4,6))-1; // これで正しい
- int day = Integer.parseInt(target.substring(6,8));
- Calendar cal = Calendar.getInstance();
- //calにコマンドライン引数1で指定された年、月、日を設定する
- cal.set(year, month, day);
- // 休日ファイルを読み込んで配列に入れとく
- String str[] = new String[100]; //とりあえず祝日は100件分
- int cnt=0;
- try {
- FileReader in = new FileReader("holiday.txt");
- BufferedReader br = new BufferedReader(in);
- String line;
- while ((line = br.readLine()) != null) {
- str[cnt] = line;
- cnt++;
- }
- br.close();
- in.close();
- } catch (IOException e) {
- System.out.println(e);
- }
- for (;0<num;) {
- cal.add(Calendar.DATE, -1);
- String date = cal.get(Calendar.YEAR) + "-" + (cal.get(Calendar.MONTH)+1) + "-" + cal.get(Calendar.DATE);
- System.out.println(date);
- // 土日だったら飛ばす
- if ( cal.get(Calendar.DAY_OF_WEEK)==Calendar.SUNDAY ||
- cal.get(Calendar.DAY_OF_WEEK)==Calendar.SATURDAY ) {
- continue;
- }
- // 配列に入れた休日だったら飛ばす
- int flg = 0;
- for (int i=0;i<cnt;i++) {
- if (date.equals(str[i])) {
- flg = 1;
- break;
- }
- }
- if (flg==1) {
- continue;
- }
- // そうでなければデクリメント
- num--;
- }
- System.out.println( String.format(
- "%1$tY年%1$tB%1$te日%1$ta曜日です。", cal));
- }
- }
- ```
Add Comment
Please, Sign In to add comment