EzDevInfo.com

Format

A Swift Formatter Kit

Using time command in bash script

I'd like to use the time command in a bash script to calculate the elapsed time of the script and write that to a log file. I only need the real time, not the user and sys. Also need it in a decent format. e.g 00:00:00:00 (not like the standard output). I appreciate any advice.


Source: (StackOverflow)

How do you format date and time in Android?

How do you format correctly according to the device configuration a date and time when having year, month, day, hour and minute?


Source: (StackOverflow)

Advertisements

How to parse date string to Date?

How to parse the Date string below to Date object???

String target = "Thu Sep 28 20:29:30 JST 2000";
DateFormat df = new SimpleDateFormat("E MM dd kk:mm:ss z yyyy");
Date result =  df.parse(target);  

Throws exception...

java.text.ParseException: Unparseable date: "Thu Sep 28 20:29:30 JST 2000"
        at java.text.DateFormat.parse(DateFormat.java:337)

Source: (StackOverflow)

Convert one date format into another in PHP

Is there a simple way to convert one date format into another date format in PHP?

I have this:

$old_date = date('y-m-d-h-i-s');            // works

$middle = strtotime($old_date);             // returns bool(false)

$new_date = date('Y-m-d H:i:s', $middle);   // returns 1970-01-01 00:00:00

But I'd of course like it to return a current date rather than the crack 'o dawn. What am I doing wrong?


Source: (StackOverflow)

How do I render a partial of a different format in Rails?

I'm trying to generate a JSON response that includes some HTML. Thus, I have /app/views/foo/bar.json.erb:

{
  someKey: 'some value',
  someHTML: "<%= h render(:partial => '/foo/baz') -%>"
}

I want it to render /app/views/foo/_baz.html.erb, but it will only render /app/views/foo/_baz.json.erb. Passing :format => 'html' doesn't help.


Source: (StackOverflow)

How to format a Java string with leading zero?

Here is the String, for example:

"Apple"

and I would like to add zero to fill in 8 chars:

"000Apple"

How can I do so?


Source: (StackOverflow)

How can I print a literal "{}" characters in python string and also use .format on it?

x = " \{ Hello \} {0} "
print x.format(42)

gives me : Key Error: Hello\\

I want to print the output: {Hello} 42


Source: (StackOverflow)

How to use java.String.format in Scala?

I am trying to use a .format method of a string. But if I place %1, %2, etc. in the string, java.util.UnknownFormatConversionException is thrown pointing to a confusing Java source code piece:

private void checkText(String s) {

    int idx;

    // If there are any '%' in the given string, we got a bad format
    // specifier.
    if ((idx = s.indexOf('%')) != -1) {
        char c = (idx > s.length() - 2 ? '%' : s.charAt(idx + 1));
        throw new UnknownFormatConversionException(String.valueOf(c));
    }
}

From this I understand that % char is forbidden. If so, then what should I use for argument placeholders?

I use Scala 2.8.


Source: (StackOverflow)

Formatting a number with leading zeros in PHP

I want have a variable which contains the value 1234567.

I want it to contain exactly 8 digits i.e. 01234567.

Is there a PHP function for that?


Source: (StackOverflow)

Android TextView Justify Text

How do you get the text of a TextView to be Justified (with text flush on the left- and right- hand sides)?

I found a possible solution here, but it does not work (even if you change vertical-center to center_vertical, etc).


Source: (StackOverflow)

How to auto-format code in Eclipse?

How do you auto-format code in Eclipse?


Source: (StackOverflow)

Difference between format specifiers %i and %d in printf

What is the difference between %d and %i when used as format specifiers in printf?


Source: (StackOverflow)

What's the difference between ISO 8601 and RFC 3339 Date Formats?

ISO 8601 and RFC 3339 seem to be two formats that are common the web. Should I use one over the other? Is one just an extension? Do I really need to care that bad?


Source: (StackOverflow)

How to nicely format floating numbers to String without unnecessary decimal 0?

An 64-bit double can represent integer +/- 253 exactly

Given this fact I choose to use a double type as a single type for all my types, since my largest integer is unsigned 32-bit.

But now I have to print these pseudo integers, but the problem is they are also mixed in with actual doubles.

So how do I print these doubles nicely in Java?

I have tried String.format("%f", value), which is close, except I get a lot of trailing zeros for small values.

Here's an example output of of %f

232.00000000
0.18000000000
1237875192.0
4.5800000000
0.00000000
1.23450000

What I want is:

232
0.18
1237875192
4.58
0
1.2345

Sure I can write a function to trim those zeros, but that's lot of performance loss due to String manipulation. Can I do better with another format code?

EDIT

The answers by Tom E. and Jeremy S. are unacceptable as they both arbitrarily rounds to 2 decimal places. Please understand the problem before answering.

EDIT 2

Please note that String.format(format, args...) is locale-dependent (see answers below).


Source: (StackOverflow)

Python format timedelta to string

I'm a Python newbie (2 weeks) and I'm having trouble formatting a datetime.timedelta object.

Here's what I'm trying to do: I have a list of objects and one of the members of the class of the object is a timedelta object that shows the duration of an event. I would like to display that duration in the format of hours:minutes.

I have tried a variety of methods for doing this and I'm having difficulty. My current approach is to add methods to the class for my objects that return hours and minutes. I can get the hours by dividing the timedelta.seconds by 3600 and rounding it. I'm having trouble with getting the remainder seconds and converting that to minutes.

By the way, I'm using Google AppEngine with Django Templates for presentation.

If anyone can help or knows of a better way to resolve this, I would be very happy.

Thanks,


Source: (StackOverflow)