tl;dr
LocalDateTime.parse(
"20180123123456" ,
DateTimeFormatter.ofPattern( “uuuuMMddHHmmss” )
).plusSeconds( 5 )
java.time
Use modern java.time classes rather than terrible old legacy classes.
Parse as a LocalDateTime
as your input apparently lacks any indicator of offset or time zone.
DateTimeFormatter f = DateTimeFormatter.ofPattern( “uuuuMMddHHmmss” ) ;
LocalDateTime ldt = LocalDateTime.parse( “20180123123456” , f ) ;
Duration d = Duration.ofSeconds( 30 ) ; // Represent a span of time unattached to the timeline.
LocalDateTime later = ldt.plus( d ) ;
String output = later.format( f ) ; // Generate a String representing the value of this object.
Tip: Use ISO 8601 standard formats instead.
Search Stack Overflow for more info. This has been covered many times already.
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.
Using a JDBC driver compliant with JDBC 4.2 or later, you may exchange java.time objects directly with your database. No need for strings nor 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.