-1

I'm working on an android app with gae as backend. I have two queries around DateTime object com.google.gdata.data.DateTime

  1. While inserting data, I only want to to insert date and not datetime. Here's current object creation and it saves both date and time.

    stock.setDate(new DateTime(new Date()));
    
  2. Also while retrieving stock object, where I have aggregated data, I want to group by date and not datetime. But as the data got stored as date and time, I'm getting group by datetime, where as I want date. What to do in such scenarios (yes, if we solve 1st problem this wont be required, but otherwise what should be done). My retrieval code is

    List<AggregatedStock> execute = null;
    
    PersistenceManager pm = getPersistenceManager();
    Query query = pm
            .newQuery(" select date, vehicleCode, vehicleSubCode, colorCode, sum(count) from Stock as AggregatedStock ");
    
    query.setFilter(" updatedByDealer == " + dealer);
    query.setGrouping(" date, vehicleCode, vehicleSubCode, colorCode ");
    query.setOrdering(" vehicleCode desc ");
    
    query.declareImports("import com.sandeepapplabs.dms.Stock");
    
    try {
        List<Object[]> results = (List<Object[]>) query.execute();
    
        execute = new ArrayList<AggregatedStock>();
    
        for (Object[] result : results) {
            execute.add(new AggregatedStock((Date) result[0],
                    (String) result[1], (String) result[2],
                    (String) result[3], ((Long) result[4]).intValue()));
        }
    
    } finally {
        pm.close();
    }
    
    return execute;
    

2 Answers2

1

A Date is a single moment in time. It's a not a "day" that lasts 24 hours. When you call new Date(), you get a different moment - down to a millisecond - each time. Note that there is no difference here between Date and DateTime - the DateTime is simply a Date with a chronology.

You have three options if you want to use the "date as a day" to group your entities:

(a) Always set the date as a midnight (how to create a Java Date object of midnight today and midnight tomorrow?).

(b) Save it as new Date(), but extract the "day" portion or convert it into a String formatted to represent a calendar day before using it to group entities.

(c) Store and use dates as String values (i.e. "2014-12-24").

Community
  • 1
  • 1
Andrei Volgin
  • 40,755
  • 6
  • 49
  • 58
0

App Engine supports java.util.Date as a property. Complete list of supported value types can be found here

  • Yes it does, but when I generate cloud endpoint libraries, it creates model classes with Date as DateTime. Sample from generated model `@SuppressWarnings("javadoc") public final class Stock extends com.google.api.client.json.GenericJson { /** * The value may be {@code null}. */ @com.google.api.client.util.Key private com.google.api.client.util.DateTime date;` – Sandeep Choudhary Dec 24 '14 at 05:15
  • Any why a downvote?? I'm new to this so please let me know so that I improve things at my end – Sandeep Choudhary Dec 24 '14 at 05:17