0

I need the get the current date like "yyyy-mm-dd" so not the hours and minutes. And I need the current day 24hours futher so 1 day futher.

I need it in my fql query this is my query:

String FQL + = .... AND start_time<'" + startTime + "' AND start_time>'" + endTime + "'  LIMIT 25"

I'm doing this with this code but it doesn't work:

Date myDate = new Date();
            Date endTime = new Date(myDate.getTime() + 86400000L);
            Date startTime = new Date(myDate.getTime());
Laurenswuyts
  • 2,144
  • 4
  • 21
  • 39

4 Answers4

1

When startTime < endTime

because

Date startTime = new Date(myDate.getTime());
Date endTime = new Date(myDate.getTime() + 86400000L);

how can the expression

 X<'" + startTime + "' AND X>'" + endTime + "'

ever by true?

 X < startTime                                      X > endTime 
---------------------------+---------------------+--------------------------
                       startTime               endTime

I replaced it with X to better distinguish between start_time and startTime

René Link
  • 48,224
  • 13
  • 108
  • 140
0

Use a Calendar instance.

Calendar calendar = Calendar.getInstance();
SimpleDateFormat sf = new SimpleDateFormat("yyy-MM-dd");
String startTime = sf.format(calendar.getTime());
calendar.add(Calendar.DAY_OF_MONTH, 1);
String endTime = sf.format(calendar.getTime());

As Rene rightly points out, start_time needs to be GREATER than startTime (start_time > startTime) and SMALLER than your endTime (start_time < endTime).

Let me also point you to joda-time, which makes handling of dates much more comfortable. But for your purpose, the code above is just fine.

Blacklight
  • 3,809
  • 2
  • 33
  • 39
0

Use java.util.Calendar

Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DATE, 1); // adds 1 day
calendar.getTime(); // returns the Date

Good Luck!

jvazquez
  • 29
  • 1
0

edit: I read it as if the question was only about the format, but what René Link pointed out is right: Your query can never yield any results.

With java.util.Date it's something like:

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
long now = System.currentTimeMillis();
Date today = new Date(now);
Date tomorrow = new Date(now + 86400000); // 24 * 60 * 60 * 1000
System.out.println("Today: " + dateFormat.format(today) + 
                   "\nTomorrow: " + dateFormat.format(tomorrow));

With java.sql.Date it becomes slightly simpler:

long now = System.currentTimeMillis();
Date today = new Date(now);
Date tomorrow = new Date(now + 86400000);
System.out.println("Today: " + today + "\nTomorrow: " + tomorrow);

And maybe you might want to use java.util.Calendar.getInstance().getTime() instead of the System.currentTimeMillis() to create the first date. You then simply have to get the first date's time in millis to add the 86400000 to the second date.

Sebastian Höffner
  • 1,864
  • 2
  • 26
  • 37