Skip to content

Instantly share code, notes, and snippets.

@noxi515
Created October 31, 2016 16:08
Show Gist options
  • Save noxi515/ad9dc42653573098cfd581b210779d1d to your computer and use it in GitHub Desktop.
Save noxi515/ad9dc42653573098cfd581b210779d1d to your computer and use it in GitHub Desktop.
WeatherNowの祝日CSV生成用コード
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