4

This is a date which I get in my application:

String psqlDate = "2013-11-17 14:08:33+01";

What is best way to convert psqlDate to joda DateTime?

[EDIT]

I can use DateTime parse method. It works fine with timestamp whitch have splitted information about date and time with T - 2013-11-17T14:08:33+01.

This should work with good pattern:

DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd...");
DateTime dt = DateTime.parse((String) obj, formatter);

What is correct pattern to PostgreSQL timestamp with time zone?

Lukas Hajdu
  • 806
  • 7
  • 18
  • 1
    Surprised your code sees a `String` for the date, not a `java.sql.Date`. How are you fetching this from the DB? – Craig Ringer Jan 07 '14 at 13:24
  • in other words, why not `SELECT` it out as a `date` in the first place? Then you can use JodaTime's conversion routines from `java.sql.Date` / `java.util.Date`. – Craig Ringer Jan 07 '14 at 13:31
  • 1
    I'm not fetching data stright from PostgreSQL. I get data from Kafka in JSON format. – Lukas Hajdu Jan 07 '14 at 13:32

3 Answers3

5

Here a genuine Joda answer. Have you tried following pattern in org.joda.time.format.DateTimeFormat?

"yyyy-MM-dd HH:mm:ssZ"

I see that your time zone offset is only specified in hours, not minutes. So maybe you need simple preprocessing and double Z like this:

String psqlDate = "2013-11-17 14:08:33+01";
DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ssZZ");
DateTime dt = DateTime.parse(psqlDate + ":00", formatter);
Meno Hochschild
  • 42,708
  • 7
  • 104
  • 126
2

you can try this

    String psqlDate = "2013-11-17 14:08:33+01";
    Date date=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss'+'01").parse(psqlDate);
    System.out.println(date);

Out put:

    Sun Nov 17 14:08:33 IST 2013
Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
0

using the SimpleDateFormat Something like: SimpleDateFormat("yyyy/mm/dd HH:mm:ss").parse() to convert it into a java date object

Saad Attieh
  • 1,396
  • 3
  • 21
  • 42