2012年2月23日木曜日

[Java]月毎の日を得る

何月は何日まであるのかを得たい
絶対スマートな方法があるのに・・・
Calendarとかは使わずに


private int getDay(int year, int month){
  switch(month){
    case 1:
    case 3:
    case 5:
    case 7:
    case 8:
    case 10:
    case 12:
      return 31;
    case 4:
    case 6:
    case 9:
    case 11:
      return 30;
    case 2:
      if(checkLeapYear(year))
        return 29;
      else
        return 28;
    default:
      return -1;
  }
}

private boolean checkLeapYear(int year){
  if((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
    return true;
  else
    return false;
}


二四むく侍。。

2012年2月20日月曜日

[VBA]新しくXLSファイルを作成/出力を行う


新しくファイルを作成し、内容をコピーする必要があったので。


Sub outDataFile(ByVal fileName As String, ByVal range As Integer)
  Dim row As Integer
  Dim inWks, outWks As Object
  
  Set inWks = ActiveWorkbook
  Set outWks = CreateObject("Excel.Sheet")
  
  For shIdx = 1 To inWks.Worksheets.count
    If shIdx <> 1 Then outWks.Worksheets.Add after:=outWks.Worksheets(shIdx - 1)
    outWks.Worksheets(shIdx).Name = inWks.Worksheets(shIdx).Name
    
    row = 1
    Do
      For col = 1 To range
        outWks.Worksheets(shIdx).Cells(row, col) = inWks.Worksheets(shIdx).Cells(row, col)
      Next col
      
      row = row + 1
    Loop While inWks.Worksheets(shIdx).Cells(row, 1) <> ""
  Next shIdx
  
  outWks.SaveAs fileName
End Sub


ちょっとベンチマークとります

2012年2月14日火曜日

[VBA]文字列<-->数値

Cstr(Integer) : String

Cint(String) : Integer

はい。
バレンタインデー

2012年2月7日火曜日

[Mac]Controlキーのショートカット

control + p : previous
control + n : next
control + b : back
control + f : forward

control + h : Just like BackSpace

control + k : Erase from cursor to end of the line
control + y : ctrl+kで消した文字列を元に戻す

macのcaps lockキーをcontrolキーに置き換える(・ω・)

2012年1月31日火曜日

[JAVA]文字列の読み込みと分割

ファイルから文字列を一行ずつ読み込んで、TABで分割を行う。
例外等は略

FileReader fr;
BufferedReader br;

fr = new FileReader("Station_a");
br = new BufferedReader(fr);
String line;
while((line = br.readLine()) != null){
  //処理だよー
  String tmp[] = line.split("\t");
}


タブは\tで表す。

[JAVA]文字コードを指定したファイル書き込み

韓国語を扱うプロジェクトから、いつも通りにファイル書き出しを行ったら文字化けの嵐だったので。
EUC-8→Shift_JISで。
例外等は省略

PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(
                                  new FileOutputStream(file), "Shift_JIS")));

pw.write(String);


文字コード(Character Set)に指定する名前はここ参照
IANA Charset Registry

[JAVA]HTMLから表(table)だけ取り出したい

Webページから表状になっているデータを取ってくる必要があったので、Jericho氏のparserを。
例外等は省略


Source source = new Source(new URL(URL));
List tableList = source.getAllElements(HTMLElementName.TABLE);
   
for(Element table : tableList){
  if(tableList.indexOf(table) > 0){
    for(Element tbody : table.getChildElements()){
      if(table.getChildElements().indexOf(tbody) > 1){
        for(Element tr : tbody.getChildElements()){
          if(tbody.getChildElements().indexOf(tr) >= 0){//ここが問題
            for(Element td : tr.getChildElements()){
              if(tr.getChildElements().indexOf(td) > 0){
                //処理だよー
                System.out.println(td.getTextExtractor().toString());
              }
            }//td
          }
        }//tr
      }
    }//tbody
  }
}//table


何これ簡単
でも、最初から<td>辺りを取得すれば良かったかも?

sourceforge - Jericho HTML Parser