Hazelcast SqlPredicate on Date object - sql

i'm trying to build a SqlPredicate with Hazelcast that should filter by a date.
The exception i get is the following:
java.lang.RuntimeException: Unable to parse date from value: '2013-06-21 03:15:44.000' !
Valid formats are: 'EEE MMM dd HH:mm:ss zzz yyyy', 'yyyy-MM-dd hh:mm:ss.SSS' and 'yyyy-mm-dd'.
The strange thing is that my date exacly match the second "valid" pattern.
What should i do?
Thanks in advance...

I'm looking at the hazelcast source code, version 2.5, in package com.hazelcast.query, and it seems that date-related expressions used in a Predicate need to be in the format according to their type:
java.util.Timestamp - "yyyy-MM-dd hh:mm:ss.SSS"
java.sql.Date - "yyyy-mm-dd"
java.util.Date - "EEE MMM dd HH:mm:ss zzz yyyy"
If you could easily use java.util.Timestamp, you can use the date expression in the format you have it.
Alternatively if you are using java.util.Date then can you try the same date in the format "EEE MMM dd HH:mm:ss zzz yyyy"?
Hope this helps.
Relevant hazelcast source code from Predicates.java pasted below:
} else if (type instanceof Timestamp) {
if (value instanceof Date) { // one of java.util.Date or java.sql.Date
result = value;
} else {
result = DateHelper.parseTimeStamp(valueString);
}
} else if (type instanceof java.sql.Date) {
if (value instanceof Date) { // one of java.util.Date or java.sql.Timestamp
result = value;
} else {
result = DateHelper.parseSqlDate(valueString);
}
} else if (type instanceof Date) {
if (value instanceof Date) { // one of java.sql.Date or java.sql.Timestamp
result = value;
} else {
result = DateHelper.parseDate(valueString);
}
And from DateHelper.java
static final String timestampFormat = "yyyy-MM-dd hh:mm:ss.SSS";
static final String dateFormat = "EEE MMM dd HH:mm:ss zzz yyyy";
static final String sqlDateFormat = "yyyy-mm-dd";

Related

Got wrong date when converting string date retrieved from the api

I'm calling an API to get some dates, but the API returns the date in string format so I'm using new Date() to convert it to date format, the problem is that after the conversion I'm getting a wrong date
For example new Date("2019-04-19") is returning Thu Apr 18 19:00:00 GMT-05:00 2019
this is my code
function HebrewCalAPI(fechas)
{
var response = UrlFetchApp.fetch("https://www.hebcal.com/hebcal/?v=1&cfg=json&maj=on&min=on&mod=on&nx=on&year=now&month=x&ss=on&mf=on&c=on&geo=geoname&geonameid=3530597&m=50&s=on");
var data = JSON.parse(response.getContentText());
var arrayEmpiezan = []
for(var i in fechas)
{
for(var i2 in data.items)
{
if(data.items[i2].title == fechas[i])
{
arrayEmpiezan.push(new Date(data.items[i2].date));
}
}
}
return arrayEmpiezan
}
var fiestasEmpiezan = ["Erev Pesach","Pesach VI (CH''M)","Erev Shavuot","Erev Rosh Hashana","Erev Yom Kippur","Erev Sukkot","Sukkot VII (Hoshana Raba)"]
var fiestasAcaban = ["Pesach II", "Pesach VIII","Shavuot II","Rosh Hashana II","Yom Kippur","Sukkot II","Simchat Torah"]
var FiestaEmpieza = date
var FiestaAcaba = date
FiestaEmpieza = HebrewCalAPI(fiestasEmpiezan)
FiestaAcaba = HebrewCalAPI(fiestasAcaban)
Any help please ?

ResultSet getDate(1)

How to return oracle.sql.DATE from ResultSet?
It returns a Java date, not the ORACLE date.
I get an error:
Type mismatch: cannot convert from Date to DATE
ResultSetIterator iter;
...
ResultSet rs = iter.getResultSet();
if (rs.next()) {
date = rs.getDate(1);
}
My query:
SELECT MIN(date_created) FROM tbl_user group by date_created;
What is oracle.sql.DATE? Do you mean java.sql.Date?
What do you mean by java Date? Do you know that java.sql.Date is subclass of java.util.Date?
try this what will be the output?
ResultSet rs = iter.getResultSet();
if (rs.next()) {
date = rs.getDate(1);
if (date instanceof java.sql.Date)
System.out.println("sql date");
if (date instanceof java.util.Date)
System.out.println("util date");
}
edit:
I see here you can probably do something like this:
ResultSet rs = iter.getResultSet();
if (rs.next()) {
date = rs.getDate(1);
if (date instanceof java.sql.Date)
return new oracle.sql.DATE(date);
}
You can cast the ResultSet to an OracleResultSet and then use the getDATE() method:
OracleResultSet rs = (OracleResultSet)iter.getResultSet();
if (rs.next()) {
oracle.sql.DATE date = rs.getDATE(1);
}

Date formatting not working in Swift 3

I'm having a problem with NSDate in a function. Essentially, the formatting is not working, and I need another set of eyes to see what I'm doing wrong.
Code:
#IBOutlet weak var dateField: NSTextField!
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MMMM dd yyyy"
let currentDate = NSDate()
convertedDateString = dateFormatter.string(from:currentDate as Date)
dateField.stringValue = convertedDateString as String
print("convertedDate: ", convertedDateString)
print("dateField.stringValue: ", dateField.stringValue)
Result:
convertedDate: July 25 2016
dateField.stringValue: 7/25/16
Date().toString() // convert date to string with userdefined format.
extension Date {
func toString() -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MMMM dd yyyy"
return dateFormatter.string(from: self)
}
}
Extension for getting string against date formats
extension Date {
func toString(format: String) -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = format
return dateFormatter.string(from: self)
}
}
Date().toString(format:"H:mm")
This is what i try on the playground and it works.
import Foundation
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "MMMM dd yyyy"
let currentDate = NSDate()
let convertedDateString = dateFormatter.stringFromDate(currentDate)
var dateField:NSTextField = NSTextField()
dateField.stringValue = convertedDateString as String
print(convertedDateString) //July 25 2016
print(dateField.stringValue) //July 25 2016
The only different was that i change to NSDateFormatter and instead of dateFormatter.string i have dateFormatter.stringFromDate.
Did you accidentally set some value in the storyboard or xib that you have the NSTextField?
The issue i had was not deleting the App from my phone before re installing and testing. The code was working in the simulator but not the phone. Hope this helps

How to convert a JTextField String to java.sql.date Date

A frequently asked question (also for me) was: How to convert a simple String from e.g. a JTextField to a java.sql.date Date - to insert some GUI-User-Input as correctly formatted Date into a table through a Prepared Statement.
I'd write a new convert-specified Class, which has this option.
import java.text.SimpleDateFormat;
import java.util.Date;
public class convertText {
public java.sql.Date formatStringToSQLDate(String strDate) throws Exception{
Date utilDate = new Date(); //DateFormat
SimpleDateFormat dfFormat = new SimpleDateFormat("dd.MM.yyyy"); // parse string into a DATE format
utilDate = dfFormat.parse(strDate); // convert a util.Date to milliseconds via its getTime() method
long time = utilDate.getTime(); // get the long value of java.sql.Date
java.sql.Date sqlDate = new java.sql.Date(time);
return sqlDate;
}
}
Afterwards, to use it like this.
Connection dp = DriverManager.getConnection("jdbc:ucanaccess://" + Filepath + Filename);
Statement stat = dp.createStatement();
String sql = "INSERT INTO user_table (Name, Birth) VALUES(?, ?)"; //statement as string
PreparedStatement pstmt = dp.prepareStatement(sql);
pstmt.setString(1, textFieldVorname.getText()); //replaces the first '?'
pstmt.setDate(2, Date.formatStringToSQLDate(textFieldGeburtsdatum.getText())); //replaces the second '?'
pstmt.executeUpdate(); //executes the insert-query

Using SQL Server Date datatype and C# DateTime to select records

I have an ActionResult where I want to select records based on a date column in SQL Server. This date is in a column of type Date. I can't directly compare the dates since C# DateTime includes the time component and the Date datatype does not. Is there a nice way to do this?
public ActionResult AbsencesByDate(DateTime date)
{
var absences = from attendance in db.Attendances
where attendance.Date == date
select new
{
returnedPersonID = attendance.PersonID,
FullName = attendance.Person.FName + " " + attendance.Person.LName,
};
return Json(absences, JsonRequestBehavior.AllowGet);
}
You could remove the time part from your date parameter in your function.
Something like this :
public ActionResult AbsencesByDate(DateTime date)
{
date = date.Date;
var absences = from attendance in db.Attendances
where attendance.Date == date
select new
{
returnedPersonID = attendance.PersonID,
FullName = attendance.Person.FName + " " + attendance.Person.LName,
};
return Json(absences, JsonRequestBehavior.AllowGet);
}
try using:
where attendance.Date == date.Date