Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Joda-Time: parse string

Tags:

java

jodatime

I have a string

 String time = "2012-09-12 15:04:01";

I want to parse that string to Joda-Time:

DateTimeFormatter dateStringFormat = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
DateTime time = dateStringFormat.parseDateTime(date);

But when I print time

time.toString()

The output is:

2012-09-12T15:04:01.000+03:00

Why output is different from input? What I do wrong? I mean what is 'T'?? Thanks in advance.

like image 773
Jim Avatar asked Dec 27 '22 14:12

Jim


2 Answers

I think you want...

dateStringFormat.print(time);
like image 22
Zutty Avatar answered Jan 08 '23 00:01

Zutty


When you parse a date or number you extra its value, not the format it was as a String.

When you toString() the value is converts it to a default format.

Why output is different from input ?

It would be surprising coincidence if it were the same.

What I doing wrong?

Assuming there is only one format of a value.

I mean what is 'T' ?

It means it is in ISO 8601 format.


BTW you have the same problem with numbers

System.out.println(Double.parseDouble("123456789"));
System.out.println(Double.parseDouble("1.1e2"));

prints

1.23456789E8
110.0

In each case the value is correct, but original format is not recorded or preserved.

like image 83
Peter Lawrey Avatar answered Jan 07 '23 22:01

Peter Lawrey