-1

Recently I am trying to populate a Google query via a properties file and I have written these lines of code:

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;
}

to grab the data from properties

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();
}

This is the query that I am working with, so how do I make it so that I can enter the date into the properties file so that it will link it to the fields on my query?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
zomdar
  • 263
  • 2
  • 6
  • 22

1 Answers1

1

You have to read the date as a String and then convert it to Date.

There are 2 ways you can it (more than 2 but these 2 are the most common ones):

  • Save the number of milliseconds in the properties file, then read the property as a Long and convert it to Date: Date date = new Date(long_value);

  • Save the date formatted: 2014/05/21 and then parse it in your file: How to parse a date?

For example:

    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);
Community
  • 1
  • 1
Alexandre Santos
  • 8,170
  • 10
  • 42
  • 64
  • ok im just trying to replace the start dates and end dates in the query with the dates that i can input into the properties file. is that the best steps to do it? – zomdar May 21 '14 at 18:17
  • There's is no "best". If it works for you then great! – Alexandre Santos May 21 '14 at 18:20
  • let me rephrase the question....how do i get String company1 = prop.getProperty("startDate"); to populate on the start date field on my query. Or do i not wanna do that? – zomdar May 21 '14 at 18:27
  • I'll put an example on my post – Alexandre Santos May 21 '14 at 18:28
  • i guess what im not understanding is why i need to parse the date and stuff. i only want the string to go into the queries so that the queries can call the strings mentioned in my properties file – zomdar May 21 '14 at 18:41
  • In this case you can just use company1 as it is. – Alexandre Santos May 21 '14 at 18:42
  • so if i wanted to replace this part of my query "2014-05-19", // Start date. with company1 can i just put String company1 in place of the actual date...or would i need to refer to the different class when i do it. sorry i am very new to java – zomdar May 21 '14 at 18:47
  • You can either make company1 and company2 public and moving them to the class or you can pass them as parameters. In the second case you'd need to modify the signature of the method executeDataQuery to receive the 2 strings. – Alexandre Santos May 21 '14 at 18:50
  • cool i think ill go with option one...any links/examples to guide me through this? thanks a lot – zomdar May 21 '14 at 18:57