cast a String to a Date for a CriteriaBuilder Predicate - jpa

Predicate datePredicate = cb.lessThan(field.get(MyTable_.value), new Date());
But, value is a String and cannot be compared properly to a Date.
My question is, is there some way to convert/cast the string into a date for comparison?
I have a column type String contains date

Related

type 'String' is not a subtype of type 'DateTime'

I tried calling this code:
var test = int.parse(DateFormat('yyyy').format(formattedTemporaryDate));
but I get the error:
type 'String' is not a subtype of type 'DateTime'
my formattedTemporaryDate is '2022-01-08'
I think I need to transform the formattedTemporaryDate to an int, but I don't know how. And I even don't know if this really is the problem... Do you need more information to fix my problem?
Just use DateTime.parse('2022-01-08') instead of just String.
Something like var formattedTemporaryDate = DateTime.parse('2022-01-08');
What would you like var test to be at the end? A datetime? An int representation of the date?
You can convert your string to a DateTime object by calling DateTime.parse()
var formattedTemporaryDate = '2022-01-08';
DateTime dt = DateTime.parse(formattedTemporaryDate);
print(dt); // 2022-01-08 00:00:00.000
If you then want to convert it to an Int (maybe because you're doing UNIX Timestamp stuff?) You could try reverse-engineering some of the code people added here going the other direction.

How to validate an input string as a valid date in Kotlin?

So my Kotlin app is accepting an input String that should be a date in a certain format:
fun haveFun(dateStr: String){
var formatter = DateTimeFormatter.ofPattern("dd-MMM-yyyy")
var formattedDate = dateStr.format(formatter)
println(formattedDate)
}
The issue is that no matter which input string I send, it seems that everything is valid and no error is thrown.
The program is cool with whatever text I sent it: 61-Boby-2019 or even I'm not a date
From Java I'm used to some exception to be thrown or an isValid method, but I didn't find such yet in Kotlin
This should work. instead of using the format() use parse() it will throw exception if it fails and handle that at calling side.
#Throws(ParseException::class)
fun haveFun(dateStr: String) {
var formatter = SimpleDateFormat("dd-MMM-yyyy", Locale.getDefault())
val date = formatter.parse(dateStr)
println(date)
}
it will throw error like below:
java.text.ParseException: Unparseable date: "im not date"
You are using the wrong methods to format your date.
The method format which you are using (dateStr.format(formatter)) is for formatting the input which takes the current string as a format string as stated here:
fun String.format(vararg args: Any?): String Uses this string as a
format string and returns a string obtained by substituting the
specified arguments, using the default locale.
You need to do something else in order to achieve what you are looking for.
var formatter = DateTimeFormatter.ofPattern("dd-MMM-yyyy")
formatter.format(LocalDate.now()) //12-Dec-2019

How to parse Firestore FieldValue to Date in Swift

I am storing a document within a collection in a Cloud Firestore database. In this document, I want a reference to when the document was stored so I am using Firestore's FieldValue object which has a serverTimeStamp() function.
I am unable to parse this FieldValue object on the client as either a Date/NSDate or String. I have spent some time reading the Firestore iOS documentation and cannot find any leads on this.
There is no issue getting the FieldValue from the database to the client, however, I am unable to cast/convert the timeStamp of type FieldValue to anything.
Attempts to convert to string and date:
let timeStampString : String = message.timeStamp
let timeStampDate = Date(timeIntervalSince1970: message.timeStamp)
Cannot assign value of type FieldValue to type String
Cannot convert value of type FieldValue to expected argument type TimeInterval (aka Double)
Edit: After reviewing Doug Stevenson's answer, the best way to handle this is by casting your timeStamp value to a TimeStamp (not FieldValue) when reading the info on the client.
let timeStamp = document["yourTimeStampKey"] as! TimeStamp
rather than
let timeStamp = document["yourTimeStampKey"] as! FieldValue
There's no parsing needed. Firestore timestamp fields are of type Timestamp, which you should use directly. It represents a point in time with nanosecond precision.
Code:
let timeStamp = document["yourTimeStampKey"] as! TimeStamp
rather than
let timeStamp = document["yourTimeStampKey"] as! FieldValue

How search in mongo with using custom structure?

How to ignore the default values of the time field in the query?
Because they are set in 0001-01-01 00:00:00 +0000 UTC, I can not find the right document
// User model
type User struct {
Mail string `json:"mail" bson:"mail,omitempty"`
Password string `json:"password" bson:"password,omitempty"`
CreatedAt time.Time `json:"created_at" bson:"created_at,omitempty"`
UpdatedAt time.Time `json:"updated_at" bson:"updated_at,omitempty"`
}
Example https://play.golang.org/p/P2P30PPtl0
time.Time is a struct type, and its zero value is a valid time value and is not considered "empty". So in case of time.Time if you need to differentiate between the zero and empty values, use a pointer to it instead, that is *time.Time. The nil pointer value will be the empty value, and any non-nil pointer values will denote non-empty time values.
type User struct {
Mail string `json:"mail" bson:"mail,omitempty"`
Password string `json:"password" bson:"password,omitempty"`
CreatedAt *time.Time `json:"created_at" bson:"created_at,omitempty"`
UpdatedAt *time.Time `json:"updated_at" bson:"updated_at,omitempty"`
}
See related question: Golang JSON omitempty With time.Time Field

Can I create a Date object from a predefined string (typescript)?

I have a value returned in a string (numbers separated by commas) and I'd like to make a Date object out of it. It looks like this is not possible, can someone confirm and/or suggest me a solution.
This does not work :
let dateString='2017,3,22,0';
let dateFromString = new Date(dateString);
This works though (when I pass a list of numbers) :
let dateFromString = new Date(2017,3,22,0);
And this works also :
let dateString = '2008/05/10 12:08:20';
let dateFromString = new Date(dateString);
The goal would be to create a Date object from a uniform string. Is that possible ?
Can I create a Date object from a predefined string, which has only one type of separator (comma, colon, slash or whatever) ?
If your environment is compatible with ES6 (eg. Babel, TypeScript, modern Chrome/Firefox etc), you can use the string's .split(',') and decompose the array into arguments like the following:
const dateString = '2017,3,22,0';
const date = new Date(...dateString.split(',')); // date object for 2017/03/22
ES5 compatible version:
var dateString = '2017,1,2,0';
var date = new (Function.prototype.bind.apply(Date, [null].concat(dateString.split(','))));
As for how the .bind.apply method works with new, you can take a look at Use of .apply() with 'new' operator. Is this possible?
Note: Thanks to the two comments below for spotting my errors 👍