Created
October 31, 2016 16:08
-
-
Save noxi515/ad9dc42653573098cfd581b210779d1d to your computer and use it in GitHub Desktop.
WeatherNowの祝日CSV生成用コード
This file contains hidden or 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
package jp.noxi.weathernow; | |
import java.io.*; | |
import java.net.HttpURLConnection; | |
import java.net.URL; | |
import java.nio.file.Files; | |
import java.time.LocalDate; | |
import java.time.format.DateTimeFormatter; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class HolidayConverter { | |
public static void main(String... args) throws Exception { | |
String exportName = args.length != 0 ? args[0] : "holiday.csv"; | |
new HolidayConverter(exportName).execute(); | |
} | |
private static final String HOLIDAY_ICAL_URL = "https://calendar.google.com/calendar/ical/ja.japanese%23holiday%40group.v.calendar.google.com/public/basic.ics"; | |
private static final DateTimeFormatter PARSE_FORMATTER = DateTimeFormatter.ofPattern("yyyyMMdd"); | |
private final String exportName; | |
public HolidayConverter(String exportName) { | |
this.exportName = exportName; | |
} | |
public void execute() throws Exception { | |
URL url = new URL(HOLIDAY_ICAL_URL); | |
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); | |
try (InputStream in = conn.getInputStream(); | |
BufferedReader reader = new BufferedReader(new InputStreamReader(in))) { | |
List<Holiday> holidays = new ArrayList<>(); | |
Holiday holiday = null; | |
String s; | |
while ((s = reader.readLine()) != null) { | |
if (s.startsWith("DTSTART")) { | |
s = s.replace("DTSTART;VALUE=DATE:", ""); | |
holiday = new Holiday(); | |
holiday.date = LocalDate.parse(s, PARSE_FORMATTER); | |
continue; | |
} | |
if (s.startsWith("SUMMARY") && holiday != null) { | |
s = s.replace("SUMMARY:", ""); | |
holiday.name = s; | |
holidays.add(holiday); | |
holiday = null; | |
continue; | |
} | |
} | |
holidays.sort((s1, s2) -> s1.date.compareTo(s2.date)); | |
try (BufferedWriter writer = Files.newBufferedWriter(new File(exportName).toPath())) { | |
holidays.forEach(h -> { | |
try { | |
writer.write(h.toString()); | |
writer.newLine(); | |
} catch (IOException e) { | |
throw new UncheckedIOException(e); | |
} | |
}); | |
} | |
} finally { | |
conn.disconnect(); | |
} | |
} | |
} | |
class Holiday { | |
private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy/MM/dd"); | |
String name; | |
LocalDate date; | |
@Override | |
public String toString() { | |
return date.format(FORMATTER) + "," + name; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment