Created
January 17, 2019 07:48
-
-
Save christoforosl/3b454ff4bbb7cb5babb5762c76d07f68 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
package org.dtph.model; | |
import java.io.PrintStream; | |
import java.text.SimpleDateFormat; | |
import java.util.Calendar; | |
import java.util.Date; | |
/** | |
* | |
* @author christoforosl | |
*/ | |
public class GoogleCalendarMaker { | |
public static void main(String[] args) throws Exception { | |
final GoogleCalendarMaker googleCalendarMaker = new GoogleCalendarMaker(); | |
Calendar c = Calendar.getInstance(); | |
c.set( 2019, 1, 10, 5 , 30 ,0 ); | |
googleCalendarMaker.neededItems = 200; | |
googleCalendarMaker.startFrom = c.getTime(); | |
//googleCalendarMaker.direction = 1; | |
googleCalendarMaker.make(); | |
googleCalendarMaker.print( System.err ); | |
} | |
final SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); | |
private static final String EVENT_TEMPLATE = "BEGIN:VEVENT\nDTSTART:%s\nDTEND:%s\nSUMMARY:%s\nX-GOOGLE-CALENDAR-CONTENT-TITLE:%s\nX-GOOGLE-CALENDAR-CONTENT-TYPE:text/html\nEND:VEVENT\n\n"; | |
private final StringBuilder calendarCode = new StringBuilder("BEGIN:VCALENDAR\nVERSION:2.0\nX-WR-CALNAME:MARATHONTRAINING\nX-WR-TIMEZONE:Europe/London\nCALSCALE:GREGORIAN\n\n"); | |
//private int direction = 0 ; | |
private int itemCounter = 0; | |
private int neededItems = 100; | |
private String itemType = "w"; | |
private Date startFrom; | |
public void make() { | |
final Calendar c = Calendar.getInstance(); | |
c.setTime( this.startFrom ); | |
int weeknum = 8; | |
while(itemCounter < neededItems) { | |
String title = ""; | |
if ( c.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) { | |
weeknum++; | |
title = "W"+weeknum+" 5 Miles Easy Run (8km)"; //itemCounter > 0 ? "Athens Marathon " + itemCounter + " week(s) Left!!" : "Athens Marathon Date" ; | |
} else if ( c.get(Calendar.DAY_OF_WEEK) == Calendar.MONDAY) { | |
title = "W"+weeknum+" Strength Training"; | |
} else if ( c.get(Calendar.DAY_OF_WEEK) == Calendar.FRIDAY) { | |
title = "W"+weeknum+" Strength Training"; | |
} else if ( c.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY) { | |
title = "W"+weeknum+" Long Run"; //itemCounter > 0 ? "Athens Marathon " + itemCounter + " week(s) Left!!" : "Athens Marathon Date" ; | |
} else { | |
title = "W"+weeknum+" Run"; //itemCounter > 0 ? "Athens Marathon " + itemCounter + " week(s) Left!!" : "Athens Marathon Date" ; | |
} | |
if (weeknum > 16) { | |
break; | |
} | |
final String summary=title; //itemCounter > 0 ? itemCounter + " weeks left until the Athens Marathon." : "Athens Marathon Date" ; | |
final String formattedEntry = String.format(EVENT_TEMPLATE, sdf.format(c.getTime())+"T033000", sdf.format(c.getTime())+"T043000", summary, title ); | |
calendarCode.append(formattedEntry); | |
//int numToAdd = direction ==0 ? 1 : -1; | |
c.add(Calendar.DAY_OF_MONTH, 1); | |
itemCounter++; | |
} | |
calendarCode.append("END:VCALENDAR"); | |
} | |
private void print( PrintStream pstr ) { | |
pstr.print( calendarCode ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment