Skip to main content

Posts

Showing posts with the label string

unexpected output while converting a string to date in java

I have a string "12/9/2010 4:39:38 PM" which i have to convert to a date object. I am using the following code to do it: String str = "12/9/2010 4:39:38 PM"; DateFormat formatter ; Date date ; formatter = new SimpleDateFormat("M/dd/yyyy H:m:s a"); date =(Date)formatter.parse(str); System.out.println("date printed"+date); However, when im printing the output, i see Thu Dec 09 04:39:38 IST 2010 How do I get the date exactly the way I declared in the string i.e 12/9/2010 4:39:38 PM as output? Pls help

Why does JSON.stringify screw up my datetime object?

{ id: 1533, story_type_id: 1, content_id: 470, created_at: Sun, 05 Feb 2012 07:02:43 GMT, updated_at: Sun, 05 Feb 2012 07:02:43 GMT, type_name: 'post' } I have a JSON object with the "datetime" field like above. It's perfect. But when I stringify it (I want to store it in cache), I get this format: "created_at":"2012-02-05T07:02:43.000Z" This causes problems, because when I want to JSON.parse this, suddenly it's no longer datetime format and it's incompatible with my other format. What can I do to solve this problem? I have 'created_at' littered everywhere throughout my application. I don't want to manually change each one.

How do I determine if a string is date format?

I have some strings like this: 2012-02-05T07:42:47.000Z mixed with other strings. It is always in this format. (but the numbers are not the same, of course...the times are different) (not Sun, 05 Feb 2012 07:42:47 GMT ) I want to know whether a string matches that format. How can I determine that? It's so complicated with the colons and dots and stuff.

Having issue with substr function in PHP

I have a string like this: $geometry = "POINT (1.5041909054501184 0.39827301781943014)" I have to split the two decimal values 1.5041909054501184 and 0.39827301781943014 based on space, into an array. For that, as expected, I have to chop-off 'POINT (' and ')' from the $geometry . I tried the following lines: $temp = substr($geometry , strpos($geometry, "(")+1, strlen($geometry)-2); and $temp = substr($geometry , strpos($geometry, "(")+1, strpos($geometry, ")")-1); Echoing $temp in both the cases displays string as: "1.5041909054501184 0.39827301781943014)" How can I remove the ')' from the string $geometry ? UPDATE How can I generalize it to strings like these? $geometry = "POINT (1.5041909054501184 0.39827301781943014)"; and $geometry = "POLYGON ((1.5049088554391572 0.39805485932781448, 1.5049135685638309 0.39805660717232405, 1.5049147247575003 0.39805462248168044, 1.504

Java string splitting error

I need to split array of string and then save it into smaller string. Plz help me....what i am doing wrong..... for(int i=0; i<suburl.size(); i++){ String temp = suburl.get(i); String[] data = temp.split(" "); Log.i("DATA 0", data[0]); Log.i("DATA 1", data[1]); Log.i("DATA 2", data[2]); } here public static ArrayList<String> suburl = new ArrayList<String>(); where, suburl.get(0) = "alex 21 engineer" suburl.get(1) = "mike 22 lawyer" suburl.get(2) = "sunny 26 deisnger" suburl.get(3) = "kim 24 painter" and String[] data; But what i am getting error when splitting is ....... 01-19 20:35:09.820: E/AndroidRuntime(1672): Caused by: java.lang.ArrayIndexOutOfBoundsException 01-19 20:35:09.820: E/AndroidRuntime(1672): at flash.com.MainActivity.onCreate(MainActivity.java:119) 01-19 20:35:09.820: E/AndroidRuntime(1672): at android.app.Instrumentation.cal

Reading Unicode characters from MySQL with PHP

I've inherited a MySQL database which contains a field named Description of type text and collation of latin1_swedish_ci . The problem with this field is it contains utf-8 data with some Unicode characters, e.g. character 733, etc. Sometimes this character also exists in the field represented as HTML encoded "&#733" as well. I'm trying to read the table and export the data to a CSV file and I need to represent this character as a double quote. Reading the HTML encoded character is easy enough. However, it appears that the actual Unicode character is converted to utf-8 before I can do anything with it resulting in a "?". How do I read in the Unicode character 733 (U+02DD), recognize it and convert it? Here's a simplified (not tested) version of the code. <? $testconn=odbc_connect ("TESTLIB", "......", "......"); $query="SELECT Description FROM TestTable"; $rsWeb=mysql_query($query)); $WebRow=mysq

Replacing special characters like dots in javascript

I have a search query from the user and I want to process it before applying to browser. since I'm using SEO with htaccess and the search url looks like this : /search/[user query] I should do something to prevent user from doing naughty things.. :) Like searching ../include/conf.php which will result in giving away my configuration file. I want to process the query like removing spaces, removing dots(which will cause problems), commas,etc. var q = document.getElementById('q').value; var q = q.replace(/ /gi,"+"); var q = q.replace(/../gi,""); document.location='search/'+q; the first replace works just fine but the second one messes with my query.. any solution to replacing this risky characters safely?

Writing huge string data to file in java, optimization options

I have a chat like desktop java swing app, where i keep getting String type data. Eventually the String variable keeps growing larger and larger. 1) Is it wise idea to keep the large variable in memory and only when the logging is finished save this to disk. 2) If not, then should i continue saving everytime i get a new string (of length about 30-40). How should i go about optimizing such a desgin?

String.intern() vs manual string-to-identifier mapping?

I recall seeing a couple of string-intensive programs that do a lot of string comparison but relatively few string manipulation, and that have used a separate table to map strings to identifiers for equality efficiency and memory use, e.g.: public class Name { public static Map<String, Name> names = new SomeMap<String, Name>(); public static Name from(String s) { Name n = names.get(s); if (n == null) { n = new Name(s); names.put(s, n); } return n; } private final String str; public Name(String str) { this.str = str; } @Override public String toString() { return str; } // equals() and hashCode() are not overridden! } I'm pretty sure one of those program was javac from OpenJDK, so not some toy application. Of course the actual class was more complex (and also I think it implemented CharSequence), but you get the idea; the entire program was littered with Name in any location you would