How to format datetime like java?
<p>I have the following java code:</p>

<pre><code>SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SXXX");
String timestamp = simpleDateFormat.format(new Date());
System.out.println(timestamp); 
</code></pre>

<p>which prints the date in the following format:</p>

<pre><code>2016-09-20T01:28:03.238+02:00
</code></pre>

<p>How do I achieve the same formatting in Python?</p>

<p>What I have so far is very cumbersome..:</p>

<pre><code>import datetime
import pytz

def now_fmt():
    oslo = pytz.timezone('Europe/Oslo')
    otime = oslo.localize(datetime.datetime.now())
    msecs = otime.microsecond
    res = otime.strftime('%Y-%m-%dT%H:%M:%S.')
    res += str(msecs)[:3]
    tz = otime.strftime('%z')
    return res + tz[:-2] + ':' + tz[-2:]
</code></pre>

<h1>ISO 8601</h1>

<p>That format is one of a family of date-time string formats defined by the <a href="https://en.wikipedia.org/wiki/ISO_8601" rel="nofollow">ISO 8601</a> standard.</p>

<h1>Python</h1>

<p>Looks like many duplicates of your Question for Python.</p>

<p><a href="http://stackoverflow.com/search?q=python%20%22ISO%208601%22">Search Stack Overflow for: Python "ISO 8601"</a>. Get 233 hits.</p>

<h1>java.time</h1>

<p>On the Java side, you are working too hard. And you are using troublesome old legacy classes now supplanted by the java.time classes.</p>

<p>The java.time classes use ISO 8601 formats by default when parsing/generating strings that represent date-time values. So no need to define a formatting pattern. Note that Java 8 has some bugs or limitations with parsing all variations of ISO 8601, remedied in Java 9.</p>

<pre><code>String input = "2016-09-20T01:28:03.238+02:00" ;
OffsetDateTime odt = OffsetDateTime.parse ( input ) ;
</code></pre>

<p>Ditto, for going the other direction in ISO 8601 format, no need to define a formatting pattern.</p>

<pre><code>String output = odt.toString () ;  // 2016-09-20T01:28:03.238+02:00
</code></pre>

<p>If you really need a <code>java.util.Date</code> for old code not yet updated to the java.time types, you can convert using new methods added to the old classes.</p>

<h1>Fractional second</h1>

<p>Beware of the fractional second. </p>

<ul>
<li>The java.time classes handle values with up to <a href="https://en.wikipedia.org/wiki/Nanosecond" rel="nofollow">nanosecond</a> resolution, nine digits of decimal fraction. </li>
<li>The <a href="https://docs.python.org/2/library/datetime.html" rel="nofollow">Python <code>datetime</code> type</a> seems to have a <a href="https://en.wikipedia.org/wiki/Microsecond" rel="nofollow">microseconds</a> resolution, for six decimal places.</li>
<li>The old legacy date-time classes in Java are limited to <a href="https://en.wikipedia.org/wiki/Millisecond" rel="nofollow">millisecond</a> resolution, for three digits of decimal fraction.</li>
<li>The ISO 8601 standard prescribes no limit to the fraction, any number of decimal fraction digits.</li>
</ul>

<p>When going from Python to Java, this is another reason to avoid the old date-time classes and stick with java.time classes. When going from Java to python, you may want to truncate any nanoseconds value to microseconds as defined by <a href="http://docs.oracle.com/javase/8/docs/api/java/time/temporal/ChronoUnit.html#MICROS" rel="nofollow"><code>ChronoUnit.MICROS</code></a>.</p>

<pre><code>Instant instant = Instant.now().truncatedTo( ChronoUnit.MICROS ) ;
</code></pre>

<h2>About java.time</h2>

<p>The <a href="http://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html" rel="nofollow">java.time</a> framework is built into Java 8 and later. These classes supplant the troublesome old date-time classes such as <a href="https://docs.oracle.com/javase/8/docs/api/java/util/Date.html" rel="nofollow"><code>java.util.Date</code></a>, <a href="https://docs.oracle.com/javase/8/docs/api/java/util/Calendar.html" rel="nofollow"><code>.Calendar</code></a>, &amp; <a href="http://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html" rel="nofollow"><code>java.text.SimpleDateFormat</code></a>.</p>

<p>The <a href="http://www.joda.org/joda-time/" rel="nofollow">Joda-Time</a> project, now in <a href="https://en.wikipedia.org/wiki/Maintenance_mode" rel="nofollow">maintenance mode</a>, advises migration to java.time.</p>

<p>To learn more, see the <a href="http://docs.oracle.com/javase/tutorial/datetime/TOC.html" rel="nofollow">Oracle Tutorial</a>. And search Stack Overflow for many examples and explanations.</p>

<p>Much of the java.time functionality is back-ported to Java 6 &amp; 7 in <a href="http://www.threeten.org/threetenbp/" rel="nofollow">ThreeTen-Backport</a> and further adapted to <a href="https://en.wikipedia.org/wiki/Android_(operating_system)" rel="nofollow">Android</a> in <a href="https://github.com/JakeWharton/ThreeTenABP" rel="nofollow">ThreeTenABP</a> (see <a href="http://stackoverflow.com/q/38922754/642706"><em>How to useâ¦</em></a>).</p>

<p>The <a href="http://www.threeten.org/threeten-extra/" rel="nofollow">ThreeTen-Extra</a> project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as <a href="http://www.threeten.org/threeten-extra/apidocs/org/threeten/extra/Interval.html" rel="nofollow"><code>Interval</code></a>, <a href="http://www.threeten.org/threeten-extra/apidocs/org/threeten/extra/YearWeek.html" rel="nofollow"><code>YearWeek</code></a>, <a href="http://www.threeten.org/threeten-extra/apidocs/org/threeten/extra/YearQuarter.html" rel="nofollow"><code>YearQuarter</code></a>, and <a href="http://www.threeten.org/threeten-extra/apidocs/index.html" rel="nofollow">more</a>.</p>

java python