2

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?

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
user880386
  • 2,737
  • 7
  • 33
  • 41
  • 4
    Just use an external library for example [joda time](http://www.joda.org/joda-time/) – Youcef LAIDANI May 15 '18 at 14:46
  • 2
    [SimpleDateFormat](https://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html) –  May 15 '18 at 14:47
  • 2
    Agreed with @YCF_L. Use http://www.joda.org/joda-time/ to get trhead-safe library to work with dates for Java 6 – Ivan May 15 '18 at 14:50
  • Possible duplicate of [Convert java.util.Date to String](https://stackoverflow.com/questions/5683728/convert-java-util-date-to-string) – Petter Friberg May 15 '18 at 15:11
  • 2
    also related [Change date format in a Java string](https://stackoverflow.com/questions/4772425/change-date-format-in-a-java-string) and if you like to use Joda [Joda time : How to convert String to LocalDate?](https://stackoverflow.com/questions/2721614/joda-time-how-to-convert-string-to-localdate) – Petter Friberg May 15 '18 at 15:12
  • 2
    When we have the ThreeTen Backport for Java 6 and 7, I got no idea why anybody is mentioning old and outdated options like Joda-Time or worse, `Date` and the notoriously troublesome `SimpleDateFormat`. Fortunately a couple of the answers have got it right. – Ole V.V. May 15 '18 at 20:16
  • 3
    You generally shouldn’t format your date into a string for your database. It’s better to transfer a date object to your prepared statement or JPA implementation, and it will insert the date value into your `date` datatype column. Normally you could transfer the `LocalDate` directly. Unfortunately with Java 6 you need to give it a `java.sql.Date`, but the conversion is straightforward: `DateTimeUtils.toSqlDate(yourLocalDate)`. – Ole V.V. May 15 '18 at 20:23
  • 1
    @OleV.V. I agree with you and with Andrea its better to use ThreeTen-Backport, also for user880386 just accept the answer of Adreas its better then me, there is no shame to say the truth ;) – Youcef LAIDANI May 15 '18 at 20:29

4 Answers4

11

There are 3 main choices:

  • Use ThreeTen-Backport (Recommended)
    Advantage: Mostly same API as Java 8 Time API (JSR-310). Migration to Java 8 is very easy.
    Disadvantage: Package names are different, and it is not a full implementation of JSR-310.

    import org.threeten.bp.LocalDate;
    import org.threeten.bp.format.DateTimeFormatter;
    
    LocalDate.parse(input, DateTimeFormatter.ofPattern(inputFormat))
             .format(DateTimeFormatter.ofPattern(outputFormat));
    
  • Use Joda-Time
    Advantage: Will work with any Java version.
    Disadvantage: Deprecated, in favor of Java 8 Time API (JSR-310).

    import org.joda.time.LocalDate;
    import org.joda.time.format.DateTimeFormat;
    
    LocalDate.parse(input, DateTimeFormat.forPattern(inputFormat))
             .toString(DateTimeFormat.forPattern(outputFormat));
    
  • Use Java 6 Date and SimpleDateFormat
    Advantage: Will work for all future Java versions. No 3rd-party library.
    Disadvantage: The API is flawed, which is why Java 8 Time API (JSR-310) was created.

    import java.text.SimpleDateFormat;
    
    new SimpleDateFormat(outputFormat).format(
        new SimpleDateFormat(inputFormat).parse(input));
    

For comparison, your Java 8 code is:

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

LocalDate.parse(input, DateTimeFormatter.ofPattern(inputFormat))
         .format(DateTimeFormatter.ofPattern(outputFormat));
Andreas
  • 154,647
  • 11
  • 152
  • 247
4

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).

Meno Hochschild
  • 42,708
  • 7
  • 104
  • 126
1

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)
Community
  • 1
  • 1
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
  • Ok. I wii try it. it will also work with java7?Thanks a lot – user880386 May 15 '18 at 15:19
  • As mentioned in my answer, the pattern syntax needs to be changed in some cases, too. – Meno Hochschild May 15 '18 at 15:35
  • 3
    Since Joda-Time recommends *"from Java SE 8 onwards, users are asked to migrate to `java.time` (JSR-310)"*, wouldn't it be better for new Java 6/7 projects to use the [ThreeTen-Backport](http://www.threeten.org/threetenbp/) instead? The methods and patterns would remain the same. Anyway, that's what [I recommend](https://stackoverflow.com/a/50353937/5221149). – Andreas May 15 '18 at 15:40
-1

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.

butchyyyy
  • 355
  • 1
  • 10
  • 2
    Hi butchyyyy and welcome to Stack Overflow! Code-only answers are generally discouraged on Stack Overflow, which is probably why someone down-voted your answer. Could you add an English explanation of how/why your code solves the problem? Thanks! – Max von Hippel May 15 '18 at 18:35