Strip timestamp from the date field in Datatables - java

I am using jquery Datatables plugin. I followed the link http://www.codeproject.com/Articles/190718/jQuery-DataTables-and-J2EE-web-application-integra
I am passing a Date in the JSON object to the datatables plugin. The format from webservice call is like
"Sat Jan 10 00:08:00 EST 2009"
, I need to strip off the time, EST and the day, I mean it should look something like
"Jan 10, 2009"
and the column is sorted on the server side. All I need is to strip off the data on the fly on the client side. I am still in the process of learning datatables plugin, I am not sure of implementing this. Experts please point me to the right direction.

[Edit] Since it looks like the date comes back from the server as a string then you're easiest solution is probably a regular expression. Try this:
function reformatDate(dateStr) {
var r = /^\w{3}\s+(\w{3})\s+(\d{1,2})\s+.*?(\d{4})$/
, m = (''+dateStr).match(r);
return (m) ? m[1]+' '+m[2]+', '+m[3] : dateStr;
}
[Original] Assuming you are working with an actual Date object and you don't want to incur the overhead of a proper JavaScript date wrangling library (such as the excellent Datejs) you could format the date like so:
var formatDate = (function() {
var months = ['Jan', 'Feb', 'Mar', 'Apr', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
return function(dt) {
return months[dt.getMonth()] + ' ' + dt.getDate() + ', ' + dt.getFullYear();
};
})();
formatDate(new Date()); // => "Mar 15, 2012"

I'm not sure if you can do this with bServerSide set to true but you might be able to setup column definitions using the aoColumns option for the datatable and then apply column rendering via
fnRender: function (o, val) {
parse your date here...
return newDateString;
}

Related

Java query string to JSON

Given a query string that contains simple types (string, date), arrays, and complex objects, how can I easily create the JSON represenation using Java?
For instance:
type=event&groups%5B%5D=a&groups%5B%5D=b&details%5Bclient_time%5D=Sat+Jan+30+2016+18%3A38%3A57+GMT-0500+(EST)
should produce:
{ type: 'event', groups: [ 'a', 'b' ], details: { client_time: 'Sat Jan 30 2016 18:38:57 GMT-0500 (EST)' } }
Such functionality exists in the Node.js Express framework (the result is readily available as request.query).
Im still unsure (per the comments on the question) that the original string is correctly formatted. I think some chars got moved around when you where replacing values with dummy data. I don't think there is a logical transition from your original string to the serialized string
Take a look at this
final String opString = "type=event&groups%5B%5D=a&groups%5B%5D=b&details%5Bclient_time%5D=Sat+Jan+30+2016+18%3A38%3A57+GMT-0500+(EST)";
System.out.println(URLDecoder.decode(opString, "UTF-8"));
which outputs
type=event&groups[]=a&groups[]=b&details[client_time]=Sat Jan 30 2016 18:38:57 GMT-0500 (EST)
Where is my interpretation of your string
final String myString = "type%3Devent%26groups%3D%5B%27a%27%2C%27b%27%5D%26details%5Bclient_time%5D=Sat+Jan+30+2016+18%3A38%3A57+GMT-0500+(EST)";
System.out.println(URLDecoder.decode(myString, "UTF-8"));
which output
type=event&groups=['a','b']&details[client_time]=Sat Jan 30 2016 18:38:57 GMT-0500 (EST)
I can logically work with that to produce the string you want
final String myStringDecoded = URLDecoder.decode(myString, "UTF-8");
System.out.println(myStringDecoded);
// Then we can break it down to its parts
// The & is used to operate values
String[] parts = myStringDecoded.split("&");
JsonObject json = new JsonObject();
for(String part: parts){
String[] keyVal = part.split("="); // The equal separates key and values
json.addProperty(keyVal[0], keyVal[1]);
}
System.out.println(json);
which results in
{"type":"event","groups":"['a','b']","details[client_time]":"Sat Jan 30 2016 18:38:57 GMT-0500 (EST)"}
I can improve on this answer to make it exactly what you want if my assumptions are correct

Is it possible for custom sorting in ExtJS 3.2 for date

I have one store which is getting date in dd-MM-yyyy format. I want to sort this date in my ExtJS grid in such way that all the date of same month should display one after the other and next other month date should be displayed. But what is happening is that in ExtJS all the dates are treated as string, so I am getting output as 01-01-2015 next date is 01-02-2015 which I don't want. So is it possible for custom sorting in ExtJS 3.2`?
Code snippet:
var memebersListStore = new Ext.data.JsonStore({
name: 'approverlist',
autoLoad: true,
url: 'json/loginLogout.do?Uid=' + userLdapId + '&startDate=' + selectedStartDate + '&endDate=' + selectedEndDate,
fields: ['weekDate'],
sortInfo: {
field: 'weekDate',
direction: 'ASC'
},
listeners: {
load: function (store, records, options) {
if (store.getTotalCount() === 0) {
memebersListStoreGrid.reconfigure(store, columnsTwo);
} else {
memebersListStoreGrid.reconfigure(store, columnsOne);
}
}
}
});
Anybody has any idea?
in fields you have to define type. like this
fields: [{name: 'weekDate', type: 'DATE'}]

play framework : Pass date to view

I need to pass the date parameter to view in play framework
My controller looks like
something.render(new Date());
And on my view what I've done is
#(myDate : Date)
<script lang="text/javascript">
var time = "#(myDate)";
</script>>
This time variable I further need to use in jQuery.
Thing is Play framework is converting the date to string object.
What I want is date object itself.
If I remove the quotes around "#(myDate)" Java script gives following output.
var backupTimeString = 2015-01-15 00:01:28.767;
Uncaught Syntax Error : expecte number
I really need the object to passed as Date object not as String represnetation of Date
1) If you work in local time, you could pass the time as a formatted string :
something.render( ... new java.text.SimpleDateFormat("yyyy/MM/dd hh:mm:ss").format(new java.util.Date()) ...)
and convert it to javascript date in the view :
<script>
var t = new Date("#mydate");
</script>
According to http://dygraphs.com/date-formats.html the format aaaa/mm/jj hh:mm:ss is the most robust.
2) In case you don't work in local time, recent browsers accept ISO-8601 date with offset from UTC, for example :
new Date('2015-01-22T12:00-0600')
3) As a last resort, you can pass a timestamp :
something.render(... new java.util.Date().getTime() ...)
<script>
var t = new Date(#mydate);
</script>

SQL "missing in or out parameter at index"

i have this code for create query and execute sql-procedure but i catch missing-exception
public String generateQuery(Integer idAccount, Long[] phoneList, Date date){
StringBuilder query=new StringBuilder();
query.append("declare arr_pn owa_util.vc_arr;\n");
query.append("begin\n");
int idx = 1;
for (Long p : phoneList)
{ query.append("arr_pn(" + idx + "):='" + String.valueOf(p) + "';\n"); idx++; }
query.append("call LOC_MAINCLIENT.set_client_relations(");
query.append("id_account_ => " + idAccount);
query.append(", phone_number_list_ => arr_pn");
query.append(", dt_ => " + date);
query.append("); end;");
return String.valueOf(query);
}
after that i get this query
declare arr_pn owa_util.vc_arr;
begin
arr_pn(1):='12345';
arr_pn(2):='678970';
arr_pn(3):='5675675';
call LOC_MAINCLIENT.set_client_relations(id_account_ => 123, phone_number_list_ => arr_pn, dt_ => Sun Mar 24 21:54:00 NOVT 2013); end;
what i did wrong?
At the moment the date string isn't quoted, so it's in valid anyway, but the colons will be interpreted by the parser as bind variable markers; specifically it'll be looking for bind variables :54 and :00.
The quick answer is to put the date string into quotes. But that date format is unlikely to match what your database (session) is expecting, so you'd need to format the date into a string it can use and provide the format as well to avoid ambiguity.
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
query.append(", dt_ => to_date('" + sdf.format(date)
+ "', 'YYYY-MM-DD HH24:MI:SS')");
You also don't need the call, so get rid of that. That would produce a string like this (with added line breaks and indentation to make it a little easier to see):
declare
arr_pn owa_util.vc_arr;
begin
arr_pn(1):='12345';
arr_pn(2):='678970';
arr_pn(3):='5675675';
LOC_MAINCLIENT.set_client_relations(id_account_ => 123,
phone_number_list_ => arr_pn,
dt_ => to_date('2013-03-24 21:54:00', 'YYYY-MM-DD HH24:MI:SS'));
end;
Assuming your package procedure is declared as:
procedure set_client_relations(id_account_ number,
phone_number_list_ owa_util.vc_arr, dt_ date);
... this this should run. Whether it does what you actually want is a different matter, of course.
You may also need to do something with the time zone if the database doesn't match your locale.
The proper way to do it is to use a prepared statement, provide all the values as bind variables, and pass the correct data types - passing the date as a date rather than as a string representation. That's a little more complicated for an array but certainly do-able. You've said this is legacy code and you have to use this, but you should still investigate switching to bind variables.

dd/MM/yyyy date format in jsp by scriptlet

I want to show the date coming from data base in the jsp page in dd/MM/yyyy format. Here is my code in javascript.
$(document).ready(function(){
<%
SDateDTO sDTO = (SDateDTO) request.getAttribute("sDTO");
if(null != scholAvailDTO){
System.out.println(scholAvailDTO.getEndDate());
%>
var end = <%=scholAvailDTO.getEndDate() %>;
$("#endDateId").val(end);
<%
}
%>
});
In console it is coming 27/04/2010 but in jsp it is getting populated like 0.0033582089552238806 which is actually division result of the date. Any help will be appreciated. Thanks, Amit
Well yes - your Javascript will presumably be rendered to the browser as:
var end = 27/04/2010;
If you want it to be in a string literal then you'll need to add the quotes yourself:
var end = "<%=scholAvailDTO.getEndDate() %>";
Note that you'll need to be confident that the value itself doesn't have quotes - or other values which aren't appropriate for JavaScript - in there. I suspect there may well be a better approach than the above.
If getEndDate() returns java.util.Date object then, use DateFormat for get the formatted string and then wrap the value in quotes as below:
<%DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");%>
var end = "<%=formatter.format(scholAvailDTO.getEndDate()) %>";
If getEndDate() returns java.lang.String then simply wrap the value in quotes as below:
var end = "<%=scholAvailDTO.getEndDate() %>";

Categories