Java 6 alternatives for LocalDate and DateTimeFormatter

java localdate to date
localdate vs date
how to convert string to date in java in yyyy-mm-dd format
java sql date to localdate
localdate java 8
java simpledateformat
convert calendar to localdate in java
simpledateformat java 8

In a Java 8 project, I can parse the date this way:

LocalDate.parse(
    newDateString, 
    DateTimeFormatter.ofPattern(expectedDateFormat) 
)
.format(
    DateTimeFormatter.ofPattern(expectedDBDateFormat)
);

My problem is that I have a Java 6 project, and LocalDate and DateTimeFormatter are not defined for Java 6.

How can I parse the date in my Java 6 project?


I would like to use Joda time, you can just make some changes in your code, your code should like like this :

LocalDate.parse(
     newDateString, DateTimeFormat.forPattern(expectedDateFormat)
).toString(DateTimeFormat.forPattern(expectedDBDateFormat));

The imports

The imports should be :

import org.joda.time.LocalDate;
import org.joda.time.format.DateTimeFormat;

Instead of :

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

Method names

If you note there are a small changes :

In Java 8 Time                            In Joda Time 
ofPattern(String pattern)           =>    forPattern(String pattern)   
format(DateTimeFormatter format)    =>    toString(DateTimeFormat format)

Migrating to the New Java 8 Date Time API, A quick and practical guide on transitioning to Java 8's new DateTime API. LocalDateTime – same as LocalDate, but includes time with nanosecond precision 6. 7. // Old. GregorianCalendar calendar = new GregorianCalendar(); DateTimeFormatter is a replacement for the old SimpleDateFormat that is thread-safe  The ISO date formatter that formats or parses a date with the offset if available, such as '2011-12-03' or '2011-12-03+01:00'. This returns an immutable formatter capable of formatting and parsing the ISO-8601 extended date format.


Convert String to Date in Java, Learn how to get an object representing a Date from a String in Java. at runtime. In our first example, let's convert a String to a java.time.LocalDate: ? LocalDate date = LocalDate.parse(dateInString, DateTimeFormatter. An alternative to the core Java Date and Time library is Joda-Time. Although the  This page will walk through java.time.LocalDate example.LocalDate introduced in Java 8, represents a date in the format yyyy-MM-dd such as 2018-12-05. It does not store time or time-zone.


The java.time-API has a backport operating for Java 6 or 7. So I think this is the simplest approach with as few changes as possible.

Other answers have also advocated Joda-Time, but the main disadvantage is a change of format pattern syntax so more than just changing import statements or the replacement of constructors versus factory methods is necessary.

Finally (for completeness), there is also my lib Time4J (v3.x for Java 6) which contains an alternative parse/format-engine based on the class ChronoFormatter (and the equivalent PlainDate instead of LocalDate).

DateTimeFormatter (Java Platform SE 8 ), For example: LocalDate date = LocalDate.now(); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy MM dd"); String text = date.format(formatter);  Atendendo a pedidos: mostro como formatamos Datas no JAva 8 com DateTimeFormatter yuhuuu.


In vanilla Java 6 you can use the Date type and SimpleDateFormat class to parse string/format string represented dates.

SimpleDateFormat parser = new SimpleDateFormat(expectedDateFormat);
Date date = parser.parse(input);
SimpleDateFormat formatter = new SimpleDateFormat(expectedDBDateFormat);
String formattedDate = formatter.format(date);

As mentioned in other answers, both classes have their issues hence they have been superseeded by the Date/Time API since Java 8. Prefer one of those options or bettery yet avoid storing string formatted dates in the database.

Still using java.util.Date? Don't!, New Date and Time API is available since Java 8. It is simpler For example: Using DateTimeFormatter. public static LocalDate asLocalDate(java.util. Java examples to use DateTimeFormatter for formatting ZonedDateTime, LocalDateTime, LocalDate and LocalTime to string with predefined and custom patterns.. 1. Create DateTimeFormatter


How to convert a string to date in Java, A DateTimeFormatter instance is used as a formatter for formatting and Unlike Date , LocalDate provides utility methods to parse and format dates, add or The following example demonstrates how you can convert an ISO-8601 It provided an excellent alternative to core Java date and time classes  Java 8 LocalDate class represents a calendar date without time (hour/minute/seconds) and timezone information. Learn to convert a date in string to LocalDate object in Java 8.


Date/Time Formatting/Parsing, Java 8 Style, One example of this is the ability of JDK 8's DateTimeFormatter to provide a descending instance of the pre-JDK 8 abstract Format class via the  How to format the date using DateTimeFormatter? | Java 8 LocalDate Class | Java 8 Date and Time How to format the date using DateTimeFormatter | Java 8 LocalDate Class | Java 8 Date and Time


LocalDate xref, 15 */ 16 package org.joda.time; 17 18 import java.io. DateTimeFormatter; 40 import org.joda.time.format. 400 * For example, passing a {@code LocalDate} and a different chronology 401 * will return a date with the 1849 * <p> 1850 * The simplest use of this class is as an alternative get method, here used to 1851 * get  Java 8 has provided DateTimeFormatter and DateTimeFormatterBuilder to play with formatting date, time or both in different ways. DateTimeFormatter has in-built formats that can directly be used to parse a character sequence.