Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h3>Joda time</h3> <p><strong>Create a <a href="http://www.joda.org/joda-time/apidocs/org/joda/time/format/DateTimeFormatter.html" rel="noreferrer"><code>DateTimeFormatter</code></a> using <a href="http://www.joda.org/joda-time/apidocs/org/joda/time/format/DateTimeFormat.html#forPattern%28java.lang.String%29" rel="noreferrer"><code>DateTimeFormat.forPattern(String)</code></a></strong></p> <p>Using Joda time you would do it like this:</p> <pre><code>String dateTime = "11/15/2013 08:00:00"; // Format for input DateTimeFormatter dtf = DateTimeFormat.forPattern("MM/dd/yyyy HH:mm:ss"); // Parsing the date DateTime jodatime = dtf.parseDateTime(dateTime); // Format for output DateTimeFormatter dtfOut = DateTimeFormat.forPattern("MM/dd/yyyy"); // Printing the date System.out.println(dtfOut.print(jodatime)); </code></pre> <hr> <h3>Standard Java &geq; 8</h3> <p>Java 8 introduced a <a href="http://www.oracle.com/technetwork/articles/java/jf14-date-time-2125367.html" rel="noreferrer">new Date and Time library</a>, making it easier to deal with dates and times. If you want to use standard Java version 8 or beyond, you would use a <a href="https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html" rel="noreferrer">DateTimeFormatter</a>. Since you don't have a time zone in your <code>String</code>, a <a href="https://docs.oracle.com/javase/8/docs/api/java/time/LocalDateTime.html" rel="noreferrer">java.time.LocalDateTime</a> or a <a href="https://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html" rel="noreferrer">LocalDate</a>, otherwise the time zoned varieties <a href="https://docs.oracle.com/javase/8/docs/api/java/time/ZonedDateTime.html" rel="noreferrer">ZonedDateTime</a> and <a href="https://docs.oracle.com/javase/8/docs/api/java/time/ZonedDate.html" rel="noreferrer">ZonedDate</a> could be used. </p> <pre><code>// Format for input DateTimeFormatter inputFormat = DateTimeFormatter.ofPattern("MM/dd/yyyy HH:mm:ss"); // Parsing the date LocalDate date = LocalDate.parse(dateTime, inputFormat); // Format for output DateTimeFormatter outputFormat = DateTimeFormatter.ofPattern("MM/dd/yyyy"); // Printing the date System.out.println(date.format(outputFormat)); </code></pre> <hr> <h3>Standard Java &lt; 8</h3> <p>Before Java 8, you would use the a <a href="https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html" rel="noreferrer">SimpleDateFormat</a> and <a href="https://docs.oracle.com/javase/8/docs/api/java/util/Date.html" rel="noreferrer">java.util.Date</a></p> <pre><code>String dateTime = "11/15/2013 08:00:00"; // Format for input SimpleDateFormat dateParser = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss"); // Parsing the date Date date7 = dateParser.parse(dateTime); // Format for output SimpleDateFormat dateFormatter = new SimpleDateFormat("MM/dd/yyyy"); // Printing the date System.out.println(dateFormatter.format(date7)); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload