Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert PostgreSQL timestamp with time zone to DateTime?

This is a date which I get in my application:

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

What is the best way to convert psqlDate to joda DateTime?

[EDIT]

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

This should work with the following pattern:

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

What is the correct pattern for a PostgreSQL timestamp with time zone?

like image 968
Lukas Hajdu Avatar asked Nov 04 '25 21:11

Lukas Hajdu


2 Answers

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);
like image 74
Meno Hochschild Avatar answered Nov 07 '25 09:11

Meno Hochschild


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
like image 40
Ruchira Gayan Ranaweera Avatar answered Nov 07 '25 09:11

Ruchira Gayan Ranaweera



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!