Last active
June 23, 2016 02:56
-
-
Save hidamariman/cca87876fc19f03fbec9713a512ec7a4 to your computer and use it in GitHub Desktop.
いちおう動くようにはなった。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.BufferedReader; | |
import java.io.FileInputStream; | |
import java.io.FileNotFoundException; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.io.UnsupportedEncodingException; | |
import java.time.Duration; | |
import java.time.LocalDateTime; | |
import java.time.format.DateTimeFormatter; | |
import java.util.logging.Level; | |
import java.util.logging.Logger; | |
public class CalcWorkingTimeWithNewAPI { | |
/** | |
* @param args the command line arguments | |
*/ | |
public static void main(String[] args) { | |
String user = "hoge"; | |
String line; | |
DateTimeFormatter dtf1 = DateTimeFormatter.ofPattern("yyyyMMdd"); | |
DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("yyyy/MM/dd"); | |
DateTimeFormatter csvFormat = DateTimeFormatter.ofPattern("yyyy/MM/dd []H:mm:ss"); | |
DateTimeFormatter csvFormat2 = DateTimeFormatter.ofPattern("yyyy/MM/dd []H:[]m"); | |
DateTimeFormatter hour = DateTimeFormatter.ofPattern("HH"); | |
DateTimeFormatter min = DateTimeFormatter.ofPattern("mm"); | |
String today = dtf1.format(LocalDateTime.now()); | |
String filename = "C:\\Users\\hoge\\Desktop\\" + today + ".csv"; | |
try { | |
//ファイルを取得して文字コード調整 | |
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(filename), "SJIS")); | |
try { | |
//読み込み開始 | |
while ((line = br.readLine()) != null) { | |
String[] csvarr = line.split(","); | |
//自分の行かつ出退勤が両方記載されている行のみ対象とする | |
if(csvarr[0].equals(user) && csvarr.length == 3){ | |
String shukkin = csvarr[1]; | |
String taikin = csvarr[2]; | |
//LocalDateTimeに変換する | |
LocalDateTime ldt1 = LocalDateTime.parse(shukkin, csvFormat); | |
LocalDateTime ldt2 = LocalDateTime.parse(taikin, csvFormat); | |
//日付・時・分それぞれに分割 | |
int shukkinhour = Integer.parseInt(hour.format(ldt1)); | |
int taikinhour = Integer.parseInt(hour.format(ldt2)); | |
int shukkinmin = Integer.parseInt(min.format(ldt1)); | |
int taikinmin = Integer.parseInt(min.format(ldt2)); | |
String Strworkday = dtf2.format(ldt1); | |
//15分刻みで計算 45分以降出勤の場合出勤のMMを1繰り上げる | |
switch(shukkinmin / 15){ | |
case 0: | |
shukkinmin = 15; | |
break; | |
case 1: | |
shukkinmin = 30; | |
break; | |
case 2: | |
shukkinmin = 45; | |
break; | |
case 3: | |
shukkinmin = 0; | |
shukkinhour ++; | |
break; | |
} | |
switch(taikinmin / 15){ | |
case 0: | |
taikinmin = 0; | |
break; | |
case 1: | |
taikinmin = 15; | |
break; | |
case 2: | |
taikinmin = 30; | |
break; | |
case 3: | |
taikinmin = 45; | |
break; | |
} | |
//diffを求めるため再度LocalDateTimeに変換 | |
String time1 = Strworkday+" "+shukkinhour+":"+shukkinmin; | |
String time2 = Strworkday+" "+taikinhour+":"+taikinmin; | |
LocalDateTime shukkintime = LocalDateTime.parse(time1, csvFormat2); | |
LocalDateTime taikintime = LocalDateTime.parse(time2, csvFormat2); | |
//差分を計算 | |
Duration difftime = Duration.between(shukkintime, taikintime); | |
int work = (int)difftime.toMinutes(); | |
//60で割った商とあまりのMM変換をdoubleでたす | |
int workhour = work/60; | |
double workmin = 0; | |
switch( work % 60 ){ | |
case 0: | |
workmin += 0; | |
break; | |
case 15: | |
workmin += 0.25; | |
break; | |
case 30: | |
workmin += 0.5; | |
break; | |
case 45: | |
workmin += 0.75; | |
break; | |
} | |
//休憩時間をマイナス | |
double worktime = workhour -1 + workmin; | |
//出力 | |
System.out.println(Strworkday+": "+worktime); | |
} | |
} | |
} catch (IOException ex) { | |
ex.printStackTrace(); | |
} | |
} catch (UnsupportedEncodingException | FileNotFoundException ex) { | |
System.out.println("csvの文字コードがおかしいか、ファイルがないのでよみこめません"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment