How to format date and time in Android?

Use the standard Java DateFormat class. For example to display the current date and time do the following: Date date = new Date(location.getTime()); DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(getApplicationContext()); mTimeText.setText(“Time: ” + dateFormat.format(date)); You can initialise a Date object with your own values, however you should be aware that the constructors have been deprecated and you should … Read more

How to prettyprint a JSON file?

The json module already implements some basic pretty printing in the dump and dumps functions, with the indent parameter that specifies how many spaces to indent by: >>> import json >>> >>> your_json = ‘[“foo”, {“bar”:[“baz”, null, 1.0, 2]}]’ >>> parsed = json.loads(your_json) >>> print(json.dumps(parsed, indent=4, sort_keys=True)) [ “foo”, { “bar”: [ “baz”, null, 1.0, … Read more

Convert a date format in PHP [duplicate]

Use strtotime() and date(): $originalDate = “2010-03-21”; $newDate = date(“d-m-Y”, strtotime($originalDate)); (See the strtotime and date documentation on the PHP site.) Note that this was a quick solution to the original question. For more extensive conversions, you should really be using the DateTime class to parse and format 🙂

How to add leading zeros?

The short version: use formatC or sprintf. The longer version: There are several functions available for formatting numbers, including adding leading zeroes. Which one is best depends upon what other formatting you want to do. The example from the question is quite easy since all the values have the same number of digits to begin … Read more