-1

I have a problem while using Joda DateTimeFormatter. Please help me out !!

This is how my Formatter looks like-

final static DateTimeFormatter inJodaFmter = DateTimeFormat.forPattern("YYYY-MM-DD HH:mm:ss");

I am making a JDBC call and getting date as a String.

resultSet.getString(10) is returning "2013-12-06 00:00:00"

However, while formatting it with Joda DateTimeFormatter it is changing as below.

inJodaFmter.parseLocalDateTime(resultSet.getString(10)) = (org.joda.time.LocalDateTime) 2013-01-06T00:00:00.000

which is wrong. Please suggest.

Saabu1210
  • 25
  • 4
  • What is YYYY? What is DD? Why do you think so? – Sotirios Delimanolis Nov 18 '15 at 02:38
  • [link] http://joda-time.sourceforge.net/apidocs/org/joda/time/format/DateTimeFormat.html#forStyle(java.lang.String) my bad..should have gone through it before posting. :) – Saabu1210 Nov 18 '15 at 03:12
  • FYI, the [Joda-Time](http://www.joda.org/joda-time/) project is now in [maintenance mode](https://en.wikipedia.org/wiki/Maintenance_mode), with the team advising migration to the [java.time](http://docs.oracle.com/javase/9/docs/api/java/time/package-summary.html) classes. See [Tutorial by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Jan 26 '18 at 05:40

3 Answers3

2

tl;dr

Use smart objects, not dumb strings.

java.time.LocalDateTime ldt = myResultSet.getObject( … , LocalDateTime.class )

java.time

FYI, the Joda-Time project is now in maintenance mode, with the team advising migration to the java.time classes. See Tutorial by Oracle.

With JDBC 4.2 and later, you can directly exchange java.time objects with your database via getObject & setObject methods. Use objects rather than mere strings to communicate with a database.

No zone

If drawing data from a column defined in a type similar to the standard TIMESTAMP WITHOUT TIME ZONE, use LocalDateTime class.

LocalDateTime ldt = myResultSet.getObject( … , LocalDateTime.class ) ;

UTC

If drawing data from a column defined in a type similar to the standard TIMESTAMP WITH TIME ZONE, use Instant class or ZonedDateTime class.

Instant instant = myResultSet.getObject( … , Instant.class ) ;

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Where to obtain the java.time classes?

The ThreeTen-Extra 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 Interval, YearWeek, YearQuarter, and more.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
1

Resolved using pattern DateTimeFormat.forPattern("Y-M-d H:m:s")

Source - http://joda-time.sourceforge.net/apidocs/org/joda/time/format/DateTimeFormat.html

Saabu1210
  • 25
  • 4
0

Also, you can try like this;

public class Test {
    static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";

    public static void main(String[] args) {
        String dateTime = "2013-12-06 00:00:00";
        DateTimeFormatter formatter = DateTimeFormat.forPattern(DATE_FORMAT);
        DateTime jodatime = formatter.parseDateTime(dateTime);
        System.out.println(jodatime);

    }
}
Semih Eker
  • 2,389
  • 1
  • 20
  • 29