Array and date format issue - java
I am a beginer in jquery and java.I have two string variables and a string array.I passed these 3 to a java servlet using a post request from javascript.Below is the code:
var stDate= $('#drpstart').jqxDateTimeInput('getText');
var edDate= $('#drpend').jqxDateTimeInput('getText');
var items = $("#target").jqxListBox('getItems');
var itemsid=[];
$("#jqxWidget").html('');
for (var i = 0; i < items.length; i++) {
itemsid[i]=items[i].value;
}
$.post('ActionItem',
{startdt:stDate,enddt:edDate,item:itemsid},function(responseJson) {
});
Then I access these variables and array at servlet side:
String starts=request.getParameter("startdt");
String ends=request.getParameter("enddt");
String[] myArray =request.getParameterValues("item");
if (myArray != null ) {
for (int i = 0; i < myArray.length; i++){
System.out.println(myArray[i]);
}
}
else {
System.out.println("null value");
}
but the myarray is geting a null value.How to get the values correctly to myarray?
Also my database table StockRegisterHeader contain field 'Date' with Date as datatype,so i converted the string variables starts and ends to Date format as below
SimpleDateFormat from = new SimpleDateFormat("dd/MM/yyyy");
SimpleDateFormat to = new SimpleDateFormat("yyyy-MM-dd");
Date startg = from.parse(starts); // 01/02/2014
Date endg = from.parse(ends);
Now I need to select data from 4 tables within this date range. I wrote the query as below:
String ss="SELECT Items.ParentID,Items.Name ,StockRegisterHeader.MovementType,
StockRegisterHeader.Date, UOM.Name,StockRegisterDetails.Quantity,
StockRegisterDetails.Rate from Items,StockRegisterHeader,UOM,StockRegisterDetails
where Items.ID = StockRegisterDetails.ItemID and
StockRegisterDetails.StockRegisterHeaderID = StockRegisterHeader.Id and
StockRegisterDetails.UOMID = UOM.ID and StockRegisterHeader.Date between '"+startg+"'
and '"+endg+"'";
rs=stmt.executeQuery(ss);
But the resultset object rs is showing null even if the database contain the requested data.I think this may be due to the date format. How to solve these two issues? I am using mysql database. Please help me.
try changing
String[] myArray =request.getParameterValues("item");
to
String[] myArray =request.getParameterValues("item[]"); //brackets must be specified to retrive array value
Related
Insert DateTime into BigQuery in Stream
I am trying to insert data into a datetime field in BigQuery via WriteStream in Java and keep getting the error java.util.concurrent.ExecutionException: io.grpc.StatusRuntimeException: INVALID_ARGUMENT: Cannot return an invalid datetime value of 1639484762470 microseconds relative to the Unix epoch. The range of valid datetime values is [0001-01-01 00:00:00, 9999-12-31 23:59:59.999999] on field date_time. I tried different options: seconds, milliseconds, microseconds, cast from Unix format to Microsoft, insert formatted string (then I get an error that it should be INT64). Where am I going wrong? Code is pretty simple try (JsonStreamWriter writer = JsonStreamWriter.newBuilder(writeStream.getName(), writeStream.getTableSchema(), client).build()) { for (int i = 0; i < 2; i++) { JSONArray jsonArr = new JSONArray(); for (int j = 0; j < 10; j++) { JSONObject record = new JSONObject(); Date date = new Date(); record.put("date_hash", date.hashCode()); record.put("date_string", date.toString()); record.put("date_time", date.getTime()); jsonArr.put(record); } ApiFuture<AppendRowsResponse> future = writer.append(jsonArr, i * 10); AppendRowsResponse response = future.get(); } }
Problem with Jackson ArrayNode.add Java function
I have a function that should change some values on Object that have some objects of array. This Object is: {"avg":{"00:00":[],"01:00":[],"02:00":[],"03:00":[],"04:00":[],"05:00":[],"06:00":[],"07:00":[],"08:00":[],"09:00":[],"10:00":[],"11:00":[],"12:00":[],"13:00":[],"14:00":[],"15:00":[],"16:00":[],"17:00":[],"18:00":[],"19:00":[],"20:00":[],"21:00":[],"22:00":[],"23:00":[]},"avg_hum":{"00:00":[],"01:00":[],"02:00":[],"03:00":[],"04:00":[],"05:00":[],"06:00":[],"07:00":[],"08:00":[],"09:00":[],"10:00":[],"11:00":[],"12:00":[],"13:00":[],"14:00":[],"15:00":[],"16:00":[],"17:00":[],"18:00":[],"19:00":[],"20:00":[],"21:00":[],"22:00":[],"23:00":[]},"max":{"00:00":[],"01:00":[],"02:00":[],"03:00":[],"04:00":[],"05:00":[],"06:00":[],"07:00":[],"08:00":[],"09:00":[],"10:00":[],"11:00":[],"12:00":[],"13:00":[],"14:00":[],"15:00":[],"16:00":[],"17:00":[],"18:00":[],"19:00":[],"20:00":[],"21:00":[],"22:00":[],"23:00":[]},"min":{"00:00":[],"01:00":[],"02:00":[],"03:00":[],"04:00":[],"05:00":[],"06:00":[],"07:00":[],"08:00":[],"09:00":[],"10:00":[],"11:00":[],"12:00":[],"13:00":[],"14:00":[],"15:00":[],"16:00":[],"17:00":[],"18:00":[],"19:00":[],"20:00":[],"21:00":[],"22:00":[],"23:00":[]},"max_hum":{"00:00":[],"01:00":[],"02:00":[],"03:00":[],"04:00":[],"05:00":[],"06:00":[],"07:00":[],"08:00":[],"09:00":[],"10:00":[],"11:00":[],"12:00":[],"13:00":[],"14:00":[],"15:00":[],"16:00":[],"17:00":[],"18:00":[],"19:00":[],"20:00":[],"21:00":[],"22:00":[],"23:00":[]},"min_hum":{"00:00":[],"01:00":[],"02:00":[],"03:00":[],"04:00":[],"05:00":[],"06:00":[],"07:00":[],"08:00":[],"09:00":[],"10:00":[],"11:00":[],"12:00":[],"13:00":[],"14:00":[],"15:00":[],"16:00":[],"17:00":[],"18:00":[],"19:00":[],"20:00":[],"21:00":[],"22:00":[],"23:00":[]},"beginDate":"","endDate":""} And I need to add values to arrays (like: {"00:00": [25],...}) So, I tried something like ("data_raw" is the received object): ArrayNode array = mapper.createArrayNode(); array = (ArrayNode) data_raw.get("avg").get("00:00"); array.add((double) 25.11666711171468); System.out.println("data_raw: " + data_raw.toString()); And the result ("data_raw" object) is: data_raw: {"avg":{"00:00":[25.11666711171468],"01:00":[],"02:00":[],"03:00":[],"04:00":[],"05:00":[],"06:00":[],"07:00":[],"08:00":[],"09:00":[],"10:00":[],"11:00":[],"12:00":[],"13:00":[],"14:00":[],"15:00":[],"16:00":[],"17:00":[],"18:00":[],"19:00":[],"20:00":[],"21:00":[],"22:00":[],"23:00":[]},"avg_hum":{"00:00":[25.11666711171468],"01:00":[],"02:00":[],"03:00":[],"04:00":[],"05:00":[],"06:00":[],"07:00":[],"08:00":[],"09:00":[],"10:00":[],"11:00":[],"12:00":[],"13:00":[],"14:00":[],"15:00":[],"16:00":[],"17:00":[],"18:00":[],"19:00":[],"20:00":[],"21:00":[],"22:00":[],"23:00":[]},"max":{"00:00":[25.11666711171468],"01:00":[],"02:00":[],"03:00":[],"04:00":[],"05:00":[],"06:00":[],"07:00":[],"08:00":[],"09:00":[],"10:00":[],"11:00":[],"12:00":[],"13:00":[],"14:00":[],"15:00":[],"16:00":[],"17:00":[],"18:00":[],"19:00":[],"20:00":[],"21:00":[],"22:00":[],"23:00":[]},"min":{"00:00":[25.11666711171468],"01:00":[],"02:00":[],"03:00":[],"04:00":[],"05:00":[],"06:00":[],"07:00":[],"08:00":[],"09:00":[],"10:00":[],"11:00":[],"12:00":[],"13:00":[],"14:00":[],"15:00":[],"16:00":[],"17:00":[],"18:00":[],"19:00":[],"20:00":[],"21:00":[],"22:00":[],"23:00":[]},"max_hum":{"00:00":[25.11666711171468],"01:00":[],"02:00":[],"03:00":[],"04:00":[],"05:00":[],"06:00":[],"07:00":[],"08:00":[],"09:00":[],"10:00":[],"11:00":[],"12:00":[],"13:00":[],"14:00":[],"15:00":[],"16:00":[],"17:00":[],"18:00":[],"19:00":[],"20:00":[],"21:00":[],"22:00":[],"23:00":[]},"min_hum":{"00:00":[25.11666711171468],"01:00":[],"02:00":[],"03:00":[],"04:00":[],"05:00":[],"06:00":[],"07:00":[],"08:00":[],"09:00":[],"10:00":[],"11:00":[],"12:00":[],"13:00":[],"14:00":[],"15:00":[],"16:00":[],"17:00":[],"18:00":[],"19:00":[],"20:00":[],"21:00":[],"22:00":[],"23:00":[]},"beginDate":"","endDate":""} I expect that just "avg":{"00:00":[X],...} be changed, but all objects with "00:00" name are being replaced regardless of the selected "primary field": "avg". Can someone help me? EDIT: Complete related code: String[] name_db_data = {"avg", "avg_hum", "max", "min", "max_hum", "min_hum"}; SimpleDateFormat hourFormatted = new SimpleDateFormat("HH:mm"); reType = reportType.detailedDailyReport; ResultSet rs = stm.executeQuery(query); // The String "query" is a dynamic MySQL query. while(rs.next()) { if(reType == reportType.detailedDailyReport) { int count4 = 0; Calendar c = Calendar.getInstance(); c.setTime(rs.getTime("hour")); for(count4 = 0; count4 < name_db_data.length; count4 ++) { ArrayNode array_aux2 = mapper.createArrayNode(); array_aux2 = (ArrayNode) data_raw.get(name_db_data[count4]).get(hourFormatted.format(c.getTime())); array_aux2.add(rs.getDouble(name_db_data[count4])); } }
java: how to get Timezone abbreviation (from offset)?
My code like belows, I am using android. I found different devices may have different result. Using different phone: I can get : "EST" or "GMT-05:00". However, I just want to get abbreviation(just like "EST"). How can I get the abbreviation (or change offset to abbreviation)? String timezone =Calendar.getInstance().getTimeZone().getDisplayName(false, TimeZone.SHORT);
If anyone else need the solution : I just tweak the code and get desired short name, which i needed to display to user. private static String getTimeZoneShortName() { String tz = TimeZone.getDefault().getDisplayName(); String[] stz = tz.split(" "); StringBuilder sName = new StringBuilder(); for (int i = 0; i < stz.length; i++) { sName.append(stz[i].charAt(0)); } return sName.toString(); }
setting a format for a date in ms access
I'm trying to format a date formatted result from a database into date only. ("dd"). The query results are formatted like this, 2014-05-17 00:00:00 I want to extract only "17", How can I achieved this? This is my code. String query = "SELECT DISTINCT date FROM Attendance;"; Object[][] queryResult = connectToDB(headerQuery); for(int x = 0; x < queryResult.length; x++){ for(int y=0; y < queryResult[x].length; y++){ Object temp = queryResult[x][y]; SimpleDateFormat format = new SimpleDateFormat("dd"); System.out.print(format.format(temp)); System.out.println(temp+ "<-----"); } System.out.println("---"); } this is my error Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.String cannot be cast to java.util.Date UPDATE: I've changed the for-loop into this: for(int x = 0; x < queryResult.length; x++){ for(int y=0; y < queryResult[x].length; y++){ String temp = (queryResult[x][y]).toString(); SimpleDateFormat format = new SimpleDateFormat("dd"); Date date = (Date) format.parse(temp); System.out.println(date+ "<-----"); } System.out.println("---"); } but it still does not format the date;
OK so you're trying to format a String. That is, temp is a String and you can't pass that into format. You need to turn that String into a Date then pass it into format. Here's how. But, even better, you should be returning a Date from the query instead of a String. That'll make your life much easier. Then you don't need to change any other part of the code. Show us your connectToDB. Assuming it's using JDBC, you should get the result by calling ResultSet.getDate
Why not use SQL to extract the day part? SELECT DatePart('d', [date]) as theDay FROM Attendance
How to change the value of a variable once per day in Java/javascript
I'm trying to make an application/script for my school which displays the day's schedule. The catch with this is that my school runs on an 8 day cycle, so that complicates things. I have a a variable called cycleDay , but how would I go about updating this just once per day, and not more than that? If there's another way you can think of doing this, please let me know. Thanks!
You can use, say, the getTime() function of the Date object, which returns the current time in milliseconds after January 1, 1970 (source: http://www.w3schools.com/jsref/jsref_gettime.asp), and save that value (e.g. in var lastUpdateTime). Then periodically check if the difference between the current time and that saved time is more than a day. If so, update cycleDay, and also update lastUpdateTime to the time when you updated. For instance, initialize with: var lastUpdateTime = new Date().getTime(); Somewhere else in your code: var currentTime = new Date().getTime(); if(currentTime - lastUpdateTime >= 24*60*60*1000) // number of milliseconds in a day { // update cycleDay lastUpdateTime = currentTime; // ... }
private Date lastUpdate = now()-1; private myDailyChahgedValue; Integer getMyDailyChangedValue() { if (lastUpdate <> now()) { myDailyChahgedValue ++; } return value; } Please note it is an a draft of code what shows main idea
Use Date and document.cookies to update your variable. Also i used two utility methods to manipulate document.cookies var today = parseInt(new Date().getTime()/(1000*3600*24)) var cookies = getCookies(); if(cookies["last_updated"] && cookies["last_updated"]<today) { /* update variable */ setCookie("last_updated", today, 1); } /* utility methods starts, adding utility methods to simplify setting and getting cookies */ function setCookie(name, value, daysToLive) { var cookie = name + "=" + encodeURIComponent(value); if (typeof daysToLive === "number") cookie += "; max-age=" + (daysToLive*60*60*24); document.cookie = cookie; } function getCookies() { var cookies = {}; // The object we will return var all = document.cookie; // Get all cookies in one big string if (all === "") // If the property is the empty string return cookies; // return an empty object var list = all.split("; "); // Split into individual name=value pairs for(var i = 0; i < list.length; i++) { // For each cookie var cookie = list[i]; var p = cookie.indexOf("="); // Find the first = sign var name = cookie.substring(0,p); // Get cookie name var value = cookie.substring(p+1); // Get cookie value value = decodeURIComponent(value); // Decode the value cookies[name] = value; // Store name and value in object } return cookies; } /* utility methods ends */