3

I want to convert a string to joda DateTime in the form of "yyyy-MM-dd HH:mm:ss". The string should be in one of two formats,"yyyy-MM-dd HH:mm:ss" or "yyyy-MM-dd", although it is user input, so I may need to kick it back if it isn't in either format. How can I check for the case where no timestamp is present and append 00:00:00 to it? Below is my code for the case where the full date and time are given.

    DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
    DateTime formattedDate= formatter.parseDateTime(dateString);
yellavon
  • 2,821
  • 12
  • 52
  • 66
  • 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 Dec 30 '17 at 22:51

2 Answers2

1

Can't you check for the length of the string ?

if ( dateString.length() == "yyyy-MM-dd".length() )
    dateString += "00:00:00";
Shmil The Cat
  • 4,548
  • 2
  • 28
  • 37
  • I have updated my question to be more clear. The strings should be in those formats, but I will need to do some additional validation to be sure. – yellavon Mar 15 '13 at 00:18
1

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.

ISO 8601

Your input string nearly complies with the standard ISO 8601 formats. To comply, replace that SPACE with a T. The java.time classes use standard formats by default when parsing/generating strings. So no need to specify a formatting pattern. If the input is of the date-only type, the replace has no effect.

String input = "2017-01-23 12:34:56".replace( " " , "T" ) ;  // Replace SPACE with `T` to comply with ISO 8601.
String input = "2017-01-23".replace( " " , "T" ) ;  // Has no effect.

Various inputs

To handle both the date-only or date-time inputs, either test for length or attempt a parse and trap for exception.

if( input.length() == 10 ) {
    LocalDate ld = LocalDate.parse( input ) ;
} else {
    LocalDateTime ldt = LocalDateTime.parse( input ) ;
}

Zone/Offset

Note that a LocalDateTime purposely lacks any offset-from-UTC & time zone. If your situation is known to intend either offset or zone, assign to create an OffsetDateTime or ZonedDateTime object.

ZoneId z = ZoneId.of( "Africa/Tunis" ) ;
ZonedDateTime z = ldt.atZone( z ) ;

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.

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

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