Any idea how to achieve the Date query in Hazelcast 3.2 ? I looked at the source code for 3.2 and I do not find anything.
Is there something like a DatePredicate using which I can write queries like
new DatePredicate("joiningDate > 1/1/2014 and joiningDate <
10/1/2014")
??
Any help is appreciated.
You can use SqlPredicate with the following way :-
new SqlPredicate("joiningDate > "+joiningMinDate+" AND joiningDate < "+joiningMaxDate+" ").
Here joiningMinDate and joiningMaxDate are your values and joiningDate is the value in db.
If not wrong it should work using a SqlPredicate just as you wrote above.
Please see: com.hazelcast.query.SqlPredicateTest::testSql_withDate
why not write a custom predicate:
final Date fromDate = /*1/1/2014 date object*/;
final Date toDate = /*10/1/2014 date object*/;
map.values(new Predicate<Object, Date>() {
public boolean apply(Entry<Object, Date> entry) {
Date date = entry.getValue();
if(date.after(fromDate) && date.before(toDate))
return true;
else
return false;
}
});
Related
In my app the api can return strings or dates as strings. I need to check if the api returns a date first. if it is not a date, just populate the field with the string. If it is a date, do the conversion before populating the field. The conversion and the populate the field part I have figured out, but how can I tell if it's returning a date as a string or a string? This is what the api returns:
data?.data?.nextAvailability
So I need it be something like
if data?.data?.nextAvailability is a string {
// do something
} else if data?.data?.nextAvailability is a date as a string {
// do this
}
You could maybe do something like this:
let string = data?.data?.nextAvailability
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
guard let date = dateFormatter.date(from: string) else {
print("not a date") // And do what you need to do
return
}
// is a date... do what you need to do with a date.
print(date)
Does anyone know how to create the date type predicate for Hazelcast?
I use Predicates.equal("date","value"); It doesn't work properly. I pass an existing date value in Hazelcast. It returns nothing. java.util.date should be comparable.
I don't know why it doesn't compare properly. Anybody can help, appreciate very much!
you can also try out your own predicate. i.e. if you have a map with key being Object and value being Date then you can do the following:
final Date requiredDate = /*your date object*/;
map.values(new Predicate<Object, Date>() {
public boolean apply(Entry<Object, Date> arg0) {
Date date = arg0.getValue();
if(requiredDate.equals(date))
return true;
else
return false;
}
});
you can do other forms of comparisons inside the apply method as well.
Using J2ME, netbeans 7.2, Developing a mobile app..
I have converted the Datefield value to a String and Now want to put it back to a Datefield. To do this I need to convert the String back to Datefield, I am using the following code but its not happening.
long myFileTime = dateField.getDate().getTime(); // getting current/set date from the datefield into long
String date = String.valueOf(myFileTime); // converting it to a String to put it back into a different datefield
Date updatedate= stringToDate(date); // passing the string 'date' to the Method stringToDate() to convert it back to date.
dateField1.setDate(updatedate); // updating the date into the new datefield1
public Date stringToDate(String s)
{
Calendar c = Calendar.getInstance();
c.set(Calendar.DAY_OF_MONTH, Integer.parseInt(s.substring(0, 2)));
c.set(Calendar.MONTH, Integer.parseInt(s.substring(3, 5)) - 1);
c.set(Calendar.YEAR, Integer.parseInt(s.substring(6, 10)));
return c.getTime();
}
Since you have mentioned that you have the long myFileTime around, you should be able to use:
Date updatedate=new Date(myFileTime);
To convert back to your date. If only your String is available, you should modify your function to this:
public Date stringToDate(String s){
Calendar c = Calendar.getInstance();
c.set(Calendar.DAY_OF_MONTH, Integer.parseInt(s.substring(0, 2)));
c.set(Calendar.MONTH, Integer.parseInt(s.substring(2, 4))-1 );
c.set(Calendar.YEAR, Integer.parseInt(s.substring(4, 8)));
return c.getTime();
}
Note the changed indexes.
In Java SE, you should be able to use the following line, instead of setting each fields separately:
c.setTimeInMillis(Long.parseLong(s));
Since in s you have the dateField.getDate().getTime() that is equal to myFileTime, the number of seconds starting with January 1, 1970, based on your provided code.
Your stringToDate should work only if your string will have the following format: ddMMyyyy. Also note that in this case you should use a SimpleDateFormat to parse, something like:
Date updatedate = new java.text.SimpleDateFormat("ddMMyyyy HH:mm:ss").parse(date);
I have asp.net application and am using Entity Framework to connect it with the database. in this application, I have a textbox to get the date(am using calender css style here), and its in the string type.
I have a column in my database and its in Date Time format, I need to compare the textbox value with the date column in my database, for this I just used the code as
public StudentAttendances(string date)
{
if (date != "")
{
DateTime date1 = Convert.ToDateTime(date);
foreach (DataAccess.StudentAttendance studentAttendance in buDataEntities.StudentAttendances.Where(s => s.Date == date1))
{
this.Add(new StudentAttendance(studentAttendance.StudentId));
}
}
}
for example if I select a date in my textbox(the format is 04/05/2012) and when I compare this with the database its not showing any data, but actually some datas are there for this date.
Your code is comparing both day and time (hours, minutes etc will have to match). Try comparing just the day part like this:
buDataEntities.StudentAttendances.Where(s => s.Date.Subtract(date1).Days == 0)
I also think that you should specify what format the input date from the users is in.
04/05/2012 may mean both 4th April or 5th of May depending on your computers regional setting.
Here is an example (below) for converting a date string in American format to DateTime object:
DateTime date1 = DateTime.Parse(date, new CultureInfo("en-US"));
Hope that helps!
your ask is very limited, but try to see this
http://docs.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html
Please check whether the following works for you:
public StudentAttendances(string date)
{
if (date != "")
{
// please see the change from your given code
DateTime date1 = DateTime.ParseExact(date, "MM/dd/yyyy",
System.Globalization.CultureInfo.InvariantCulture);
foreach (DataAccess.StudentAttendance studentAttendance in buDataEntities.StudentAttendances.Where(s => s.Date == date1))
{
this.Add(new StudentAttendance(studentAttendance.StudentId));
}
}
}
I'm using C# and I get data from database like this:
da.SelectCommand = new SqlCommand("SELECT name,company,startdate,enddate,DATEDIFF(day,GETDATE(),enddate) AS days FROM Person", cn);
ds.Clear();
da.Fill(ds);
dgvViolation.DataSource = ds.Tables[0];
and the data will dispaly in datagridview like this :
name company startdate enddate days
---------------------------------------
john IBM 12/2/2012 20/2/2012 5
steven IBM 1/2/2012 12/2/2012 -3
I need the datagridview to display the positive values that appear in the days columns only. How do I do this?
Change the query to
"SELECT name,company,startdate,enddate,DATEDIFF(day,GETDATE(),enddate) AS days FROM Person WHERE days > 0"
Why don't you just change your SQL command to return the values above 0?
But if you want to do it with all the data returned, you can use the RowDataBound event of the gridview, something like this:
protected void gridview_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
if (e.Row.RowType == System.Web.UI.WebControls.DataControlRowType.DataRow)
{
//look at the value of the days column and use e.Row.Style["Display"] = "none"; to hide it.
}
}