1

When I run the following code (Java):

DateTimeFormatter d_t = DateTimeFormat.forPattern("dd/m/yyyy h:mm:ss aa");
String date = "27/3/2015 2:47:08 AM";
DateTime result = DateTime.parse(date, d_t);

I always get the result as: 2015-01-27T02:47:08.000+02:00

As you can see the month and the hours are incorrect.

Any suggestions? Thanks

Tera
  • 11
  • 1

2 Answers2

0

Month of year is written with a capital M as well as hour of day H.

Try

DateTimeFormat.forPattern("dd/M/yyyy H:mm:ss aa");
Franz Becker
  • 705
  • 5
  • 8
  • Thanks, I tried it but unfortunately I got: Cannot parse "27/3/2015 2:47:08 AM": Illegal instant due to time zone offset transition – Tera Jul 09 '15 at 06:59
  • That's a different issue, see other questions here, e.g. http://stackoverflow.com/questions/26162702/cannot-parse-datetime-illegal-instant-due-to-time-zone-offset-transition-euro – Franz Becker Jul 09 '15 at 07:19
  • But this date is a valid date – Tera Jul 09 '15 at 08:44
  • I can execute the code on my machine and it works perfectly. If you follow the link above you'll get some more details for your error and a solution, try: ```DateTimeFormat.forPattern("dd/M/yyyy H:mm:ss aa").parseLocalDateTime("27/3/2015 2:47:08 AM");``` – Franz Becker Jul 09 '15 at 21:57
0

tl;dr

LocalDateTime.parse( 
    "27/3/2015 2:47:08 AM" , 
    DateTimeFormatter.ofPattern( "d/M/uuuu h:m:s a" , Locale.US ) 
)

2015-03-27T02:47:08

java.time

Your input string seems especially odd. The hour does not have a padding zero but the second does? Strange, but it can be parsed using the modern java.time classes.

The Joda-Time project is now in maintenance mode, with the team advising migration to the java.time classes.

You were inappropriately trying to store a date-time value lacking any offset or time zone information into a data type wielding a time zone.Parse your input string as a LocalDateTime since we have no indication of offset-from-UTC or time zone.

String input = "27/3/2015 2:47:08 AM";
DateTimeFormatter f = DateTimeFormatter.ofPattern( "d/M/uuuu h:m:s a" , Locale.US );
LocalDateTime ldt = LocalDateTime.parse( input , f );

Dump to console.

System.out.println( input + " = " + ldt );

27/3/2015 2:47:08 AM = 2015-03-27T02:47:08

Be aware that a LocalDateTime is not a point on the timeline. It represents only a set of possible moments over a range of about 26-27 hours. It has no real meaning until you place the value in context, either applying an offset to get a OffsetDateTime or a full time zone to get a ZonedDateTime.


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