UnsupportedOperationException for updateItem Date attribute in Dynamodb - amazon-dynamodb

I am trying to update one date attribute of an item in dynamodb by following piece of code:
AttributeUpdate attributeUpdates = new AttributeUpdate("workDate");
Date workDate = finalMap.get(tID);
attributeUpdates.put(workDate);
PrimaryKey primaryKey = new PrimaryKey("tID", tID);
UpdateItemOutcome outcome = table.updateItem(primaryKey, attributeUpdates);
Last line, where I am calling table.updateItem, is giving UnSupportedOperationException as follows.
Exception in thread "main" java.lang.UnsupportedOperationException: value type: class java.util.Date
Does anyone have any clue regarding how to do that. Thanks.

DynamoDB does not accept dates as a data type. You will need to convert that date into a string, then do the update.

To facilitate easy date-handling, try saving the long integer millisecond time represented by the Date object:
Date workDate = finalMap.get(tID);
attributeUpdates.put(workDate.getTime());
This can be easily and unambiguously translated back to a Date object when you load the record, by using
new Date(long Date)

Related

How to cast an object in sqlite as only Date and pass date from a parameterised query

So I'm trying to check if some of my values match, for that I'm using an SQLite object and casting it as date and passing it as a parameter which is also a Date, I just want to know if this is the right way to do it?
"AND R1.TIMING = ?
AND R1.VARIETY = ?
AND R1.JOBACRES = ?
AND CAST (R1.PLANTINGDATE AS DATE) = ?
new object[] { item.TIMING, item.VARIETY, item.JOBACRES, item.PLANTINGDATE.Date }).ToList();
This is written in C#, since I want to execute this query in C# with SQLite, any inputs would be helpful

Compare date part of datetime column with NamedQuery

I have a table containing entries with date and time. I try to create a NamedQuery which only compares the date part.
#NamedQuery(name = "Vote.findForDate", query = "SELECT v FROM Vote v WHERE v.createdAt = :date")
...
createNamedQuery("Vote.findForDate").setParameter("date", date, TemporalType.DATE).getResultList();
But it seems that it always tries to compare the whole datetime.
Is there no way without using date() function in SQL?
I try to be independent from the database. For example h2 has no date() function.
One possible solution is the usage of date as column type and reduce the information.
#Column(name = "voteDate")
#Temporal(value = TemporalType.DATE)
private Date voteDate;

Capture only month and year (or null) - then convert that to a date

I'm currently working with a client that has a VB.NET web application that was developed internally. They've got everything storing to an Access database which they cannot alter or change for their own reasons. I'm not familiar with any of these technologies, so I'm hoping you may have a solution.
The client has a date field that they are only capturing mm/yyyy or blank. They need this information to save to a datetime field in the database. I'm trying to work up a statement that will automatically take the date entered and convert from mm/yyyy to mm/01/yyyy if the date is provided, or 01/01/1970 if the field was left blank. Can anyone assist?
If we are talking about MS Access functions DateSerial is what you are looking for. The basic syntax is below. If the stored value is text you will need to use the Mid function to parse the text into the year and month and you can use use a hard coded 1 for the day.
DateSerial ( year, month, day )
This function can be used in a select or update. Additional logic will be required to provide a default value for the blank result. Typically in Access this type of logic is done with an IIF.
You can use a combination of the IIf,IsNull and CDate functions, like so:
IIf(IsNull([YourDateFField]),#1/1/1970#,CDate([YourDateFField]))
This tests if your field is null and if yes it returns 1/1/1970, if no it will convert your date string to an actual date (e.g. CDate("04/2014") will return 4/1/2014)
This (MSAccess/VBA) function will do what you are asking. If you pass-in a string like mm/yyyy, it will return a datetime like mm/01/yyyy. However, if the string does not fit that pattern (or equiv), the function will return a date time of 1/1/1970, like you asked.
'in MSAccess:
Public Function mmyyyyToDate(mmyyyy As String) As Datetime
If IsDate(Replace(mmyyyy, "/", "/01/")) Then
Return CDate(Replace(mmyyyy, "/", "/01/"))
Else
Return #1/1/1970#
End If
End Function
It would be more efficient to run it in MSAccess, but if you want to run it in VB.net instead, the syntax is different:
'in VB.NET
Public Function mmyyyyToDate(mmyyyy As Object, Optional defaultDate As DateTime = "1/1/1970") As DateTime
Dim re As DateTime
If Convert.IsDbNull(mmyyyy)
return defaultDate
ElseIf DateTime.TryParse(Replace(mmyyyy, "/", "/01/"), re) Then
Return re
Else
Return defaultDate
End If
End Function
Example of running it:
'MSAccess query syntax
INSERT INTO NewDateTable (NewDateColumn)
SELECT mmyyyyToDate(oldColumn) FROM OldTable
If you can't add a new function to the MSAccess DB, you could turn this function into an inline statement (by using an IIF), but it looks pretty ugly:
'MSAccess query syntax
INSERT INTO NewDateTable (NewDateColumn)
SELECT IIF(IsDate(Replace(oldColumn, "/", "/01/")), Replace(mmyyyy, "/", "/01/"), #1/1/1970#)
FROM OldTable

Using dates with Cassandra

I've just started my adventure with Cassandra database. I've managed to learn some basics but what I still can't understand is how to work with dates in Cassandra?
So for example in MySQL we have a datetime type for a field and we can query (for example) all fields with creation date less then 2010-01-01. Furthermore we can order the result by creation date field.
How can we achieve the same with Cassandra? How to define the corresponding Column Family and how to query (CQL) it to get the same result?
You can use type DateType to define a column of type DateType in your column family. You should really read this page, it has description and example how to do range query (that is creationdate < 2010-01-01). For ordering, you can refer to the SliceRange but this will probably cover in the cassandra client already. You will probably want to look into the cassandra client to do the query.
This is a snippet on how to do query in cassandra using hector client.
// 2010-01-01
Date date = new Date(1262275200L);
try
{
getConnection();
IndexedSlicesQuery<String, String, String> indexedSlicesQuery = HFactory.createIndexedSlicesQuery(keyspace, ss, ss, ss);
indexedSlicesQuery.setColumnNames("name");
indexedSlicesQuery.addLtExpression("timestamp", ByteBufferUtil.string(date_s.toByteBuffer(date)));
indexedSlicesQuery.addEqualsExpression("searchall", ByteBufferUtil.string(bs.toByteBuffer(true)));
indexedSlicesQuery.setColumnFamily(column_family);
indexedSlicesQuery.setStartKey("");
System.out.println(indexedSlicesQuery.toString());
QueryResult<OrderedRows<String, String, String>> res = indexedSlicesQuery.execute();
List<Row<String, String, String>> list = res.get().getList();
for (Row<?, ?, ?> row : list)
{
System.out.println(row.getKey());
}
}

Failed to convert parameter value from a TimeSpan to a DateTime

I am trying to store a time from my textbox field value but I got this exception error, what am I doing wrong? Thanks!:
Failed to convert parameter value from a TimeSpan to a DateTime.
Passing textbox value:
.CamActiveDateFrom = CDate(uitxtCamDateStart.Text.Trim)
My variable and property:
Private _camActiveTimeFrom As Nullable(Of TimeSpan)
Public Property CamActiveTimeFrom() As Nullable(Of TimeSpan)
Get
Return Me._camActiveTimeFrom
End Get
Set(ByVal value As Nullable(Of TimeSpan))
Me._camActiveTimeFrom = value
End Set
End Property
Parameter:
AddInParameter(dbCommand,
"#ActiveTimeFrom", DbType.Time, 6, DBNull.Value)
If (.CamActiveTimeFrom).HasValue Then
dbCommand.Parameters("#ActiveTimeFrom").Value = .CamActiveTimeFrom
End If
DbType.Time is documented as:
A type representing a SQL Server DateTime value. If you want to use a SQL Server time value, use SqlDbType.Time.
So either use SqlDbType.Time instead, or set the value to a DateTime value instead of a TimeSpan value.
Admittedly this mapping table show the DbType.Time mapping to TimeSpan, so there's some inconsistency here - but if you can use a more specific type, that can only be good.
Of course it's easy to construct a DateTime from a TimeSpan if you want to - you should probably decide on a specific date to always use (e.g. 1st January 2000) so that you can then compare values within the database and effectively get a comparison of just the time bits.
I hav the same problem. And I soloved it just used "ToString()".
my code as following:
the date type of "info.START_TIME" is TimeSpan.
db.AddInParameter(dbCommand, "#START_TIME", DbType.Time, info.START_TIME.ToString());
In order to use AddInParameter with a column of data type time, you need to use the SqlDbType instead of DbType (as described in Jon Skeet's answer)
db.AddInParameter(dbCommand, "#ActiveTimeFrom", SqlDbType.Time, 6, DBNull.Value)
instead of
db.AddInParameter(dbCommand, "#ActiveTimeFrom", DbType.Time, 6, DBNull.Value)
BUT.
In order you use SqlDbType.Time instead of DbType.Time, you must declare your Database object as an SqlDatabase, that is
SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
instead of
Database db = DatabaseFactory.CreateDatabase();
as otherwise it won't take the parameter.

Resources