2014-05-21 22 views
-1

最近我試圖填充谷歌查詢通過屬性文件,我已經寫了幾行代碼:獲得一個字符串中另一個類填充上查詢

public String getPropValues() throws IOException { 

    String result = ""; 
    Properties prop = new Properties(); 
    String propFileName = "config.properties"; 

    InputStream inputStream = getClass().getClassLoader().getResourceAsStream(propFileName); 
    prop.load(inputStream); 
    if (inputStream == null) { 
     throw new FileNotFoundException("property file '" + propFileName + "' not found in the classpath"); 
    } 

    Date time = new Date(System.currentTimeMillis()); 

    // Get the property value and print it out 
    String user = prop.getProperty("user"); 
    String company1 = prop.getProperty("startDate"); 
    String company2 = prop.getProperty("endDate"); 
    String company3 = prop.getProperty("company3"); 

    result = "Company List = " + company1 + ", " + company2 + ", " + company3; 
    System.out.println(result + "\nProgram Ran on " + time + " by user=" + user); 
    return result; 
} 

抓住從性能

數據
private static GaData executeDataQuery(Analytics analytics, String profileId) throws IOException { 
    return analytics.data().ga().get("ga:" + profileId, // Table Id. ga: + profile id. 
     "2014-05-19", // Start date. 
     "2014-05-20", // End date. 
     "ga:pageviews,ga:sessions,ga:uniquePageviews") // Metrics. 
     .setDimensions("ga:date") 
     .setSort("-ga:date") 
     .setMaxResults(25) 
     .execute(); 
} 

這是我正在使用的查詢,所以如何使它能夠將日期輸入到屬性文件中,以便將其鏈接到我的查詢上的字段?

回答

1

您必須將日期讀取爲字符串,然後將其轉換爲日期。

有2種方式,你可以把它(大於2,但這些2是最常見的):

  • 保存在屬性的毫秒數文件,然後讀取屬性作爲一個長期而將其轉換爲日期:Date date = new Date(long_value);

  • 格式保存日期:2014年5月21日,然後分析它在你的文件:How to parse a date?

對於例如:

String company1 = prop.getProperty("startDate"); 
    // suppose that compan1 has the value 2014-05-21 

    DateFormat df = new SimpleDateFormat("yyyy-MM-dd"); 
    Date result = df.parse(company1); 
    System.out.println(result); 
+0

確定即時只是試圖用我可以輸入到屬性文件中的日期替換查詢中的開始日期和結束日期。這是最好的步驟嗎? – zomdar

+0

沒有「最好的」。如果它適合你,那麼太棒了! –

+0

讓我改述這個問題....我如何獲得String company1 = prop.getProperty(「startDate」);填充我的查詢的開始日期字段。 或者我不想那麼做? – zomdar

相關問題