Re: [問題] 請問如何將code不要寫死,不需要重新編쐠…
(更新說明方式)請問大家如何將code不要寫死,不需要重新編譯code就有擴充性
對不起,之前描述太簡略,讓大家都聽不懂。
如果聽不懂我說的問題,麻煩請大家跟我說。
1.1 寫入google calendar 網頁
寫一個HTML網頁,讓使用者可以選"兩個title選項"選擇一POST到servlet,
透過servlet(使用gdate)把選好title data 寫入 Google 日曆
PS:google calendar 目的是 記錄使用者資料
-----------------------------------------------------------------------------
1.2 抓取google calendar 程式
已知寫入google日曆有兩個事件選項。
自己用gdata for java 抓取 google calendar 每一天所有 title ,
之後並區分(分類)title再寫入DB.
PS:一天中會做"看書"、"吃飯" 、"看書"...等 很多title,
從 google calendar 全部抓出title 並計算每個title花費時間...等。
再區分(分類)title寫入自己DB"
以上Code已經寫好了
PS:抓出google calendar資料目的是要寫入自己DB,日後要做資料上研究分析
--------------------------------------------------------------------------------------------------------------------------------
現在要把程式變成活(不能重新編譯)的改成
2.1 寫入google calendar 網頁
如果要將原本html網頁兩個選項"擴充,新增" 第多個 title選項 (三個四個以上)
2.2 抓取google calendar 程式
再抓取google calendar時,必須先得知(html網頁)有多少title並將title區分分類並寫
入自己的DB
PS:如果 寫入google calendar 網頁 有3個title ,那 抓取google calendar 程式 必
須要區分分類 3個title 寫入自己DB
PS:如果 寫入google calendar 網頁 有4個title ,那 抓取google calendar 程式 必
須要區分分類 4個title 寫入自己DB
--------------------------------------------------------------------------------------------------------------------------------
目前我知道重新改寫解決這問題,如果
不能改寫code重新編譯,要如何 設計 撰寫 呢?
==========================================================================================
public class test {
private static String id = "id@gmail.com";
private static String pw = "pw";
private static String StartYear = "2009";
private static String StartMonths = "08";
private static String StartDay = "01";
private static String EndYear = "2009";
private static String EndMonths = "10";
private static String EndDay = "01";
private static float titelnum011 = 0;
private static float titelnum012 = 0;
private static String titel011 = "日曆";
private static String titel012 = "meeting";
private static int num01 = 0;
private static int daynum011 = 0;
private static int daynum012 = 0;
public static void main(String args[]) throws ServiceException,
IOException, ParseException {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy" + "-" + "MM"
+ "-" + "dd");
Date date1 = formatter.parse(StartYear + "-" + StartMonths + "-" +
StartDay);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date1);
Date date2 = formatter.parse(EndYear + "-" + EndMonths + "-" +
EndDay);
long total = (date2.getTime() - date1.getTime()) / (1000 * 60 * 60 *
24) + 1;
System.out.println("總共 " + total + " 天數");
//=============================================================================
CalendarService myService = new
CalendarService("exampleCo-exampleApp-1");
myService.setUserCredentials(id, pw);
URL feedUrl = new
URL("http://www.google.com/calendar/feeds/default/private/full");
CalendarQuery myQuery = new CalendarQuery(feedUrl);
int day = 0;
//=============================================================================
while (calendar.getTime().compareTo(date2) <= 0) {//一天一天抓取,解決
google一次只能抓取25DATA
int y = calendar.get(Calendar.YEAR);
int m = calendar.get(Calendar.MONTH) + 1;
int d = calendar.get(Calendar.DATE);
day = day + 1;
String yyy = String.valueOf(y);
String mmm;
if (m < 10) {
mmm = String.valueOf(m);
mmm = "0" + mmm;//符合google api 輸入日期格式
} else {
mmm = String.valueOf(m);
}
String ddd;
if (d < 10) {
ddd = String.valueOf(d);
ddd = "0" + ddd;//符合google api 輸入日期格式
} else {
ddd = String.valueOf(d);
}
//=============================================================================
myQuery.setMinimumStartTime(DateTime.parseDateTime(yyy + "-" +
mmm + "-" + ddd + "T00:00:00"));
myQuery.setMaximumStartTime(DateTime.parseDateTime(yyy + "-" +
mmm + "-" + ddd + "T23:59:59"));
// Send the request and receive the response:
CalendarEventFeed resultFeed = myService.query(myQuery,
CalendarEventFeed.class);
int size = resultFeed.getTotalResults();//取得日曆當天是否有資料筆
數
if (size != 0) {
for (int i = 0; i < resultFeed.getEntries().size(); i++) {
CalendarEventEntry entry = resultFeed.getEntries().get(i);
String title = entry.getTitle().getPlainText();//日曆事件
//=============================================================================
When t = entry.getTimes().get(0);
DateTime start = t.getStartTime();
String s = start.toUiString();
int aa = (int) start.getValue();//日曆title開始時間
DateTime end = t.getEndTime();
String e = end.toUiString();
int bb = (int) end.getValue();//日曆title結束時間
int cc = (bb - aa) / 1000 / 60;//計算日曆title花費時間
float dd = (float) (cc / 60f);
//==========================================================
// 當 輸入(post)google日曆事件選項 新增第三筆"
// 再 抓取google資料時必須要改寫並"新增第三筆"並區分事件
(titel)分別寫入DB
// 如何把下程式碼寫活的, 具備擴充性,
if (title.equals(titel011) || title.equals(titel012)) {
System.out.println();
System.out.println("開始 " + s);
System.out.println("事件 " + (i + 1) + " " + title);
System.out.println("結束 " + e);
System.out.println("花費時間 " + dd + "小時");
System.out.println("~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~");
if (title.equals(titel011)) {
daynum011 = daynum011 + 1;
titelnum011 = titelnum011 + dd;
}
if (title.equals(titel012)) {
daynum012 = daynum012 + 1;
titelnum012 = titelnum012 + dd;
}
//==========================================================
}//for
}//if
}
calendar.add(Calendar.DAY_OF_YEAR, 1);//while//日期加一天
}//while
//統計各項事件 總次數 總時數
System.out.println("事件一 " + titel011 + " 總次數 " + daynum011 + "
總時數 " + titelnum011);
System.out.println("事件二 " + titel012 + " 總次數 " + daynum012 + "
總時數 " + titelnum012);
}
}
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 210.240.197.117
→
10/19 20:14, , 1F
10/19 20:14, 1F
推
10/19 20:33, , 2F
10/19 20:33, 2F
→
10/19 20:44, , 3F
10/19 20:44, 3F
推
10/20 01:15, , 4F
10/20 01:15, 4F
→
10/20 02:07, , 5F
10/20 02:07, 5F
推
10/20 05:57, , 6F
10/20 05:57, 6F
→
10/20 13:26, , 7F
10/20 13:26, 7F
→
10/20 13:28, , 8F
10/20 13:28, 8F
→
10/20 13:31, , 9F
10/20 13:31, 9F
討論串 (同標題文章)
以下文章回應了本文:
完整討論串 (本文為第 1 之 10 篇):