tl;dr
java.time.LocalDateTime.parse(
"2018-01-01 00:00:40.554".replace( " " , "T" )
).plusMinutes( 60 )
2018-01-01T01:00:40.554
java.time
The Joda-Time project is now in maintenance mode, with the team advising migration to the java.time classes.
ISO 8601
Your input is nearly in the standard ISO 8601 format used by default in the java.time classes. To comply, replace SPACE in the middle with a T
.
String input = "2018-01-01 00:00:40.554".replace( " " , "T" ) ;
LocalDateTime
Your string input lacks an indicator of time zone or offset-from-UTC. So parse as a java.time.LocalDateTime
.
LocalDateTime ldt = LocalDateTime.parse( input ) ;
ldt.toString(): 2018-01-01T00:00:40.554
Time zone
Note that you do not have an actual moment, this is not a specific point on the timeline. This is only a vague idea about potential moments along a range of about 26-27 hours. To determine an actual moment, place this in the context of a time zone (or offset): ZonedDateTime zdt = ldt.atZone( ZoneId.of( "Pacific/Auckland" ) ) ;
.
Date-time math
You can represent your spans of time unattached to the timeline as a Period
for years-months-days or a Duration
for hours-minutes-seconds.
Duration d24Hours = Duration.ofHours( 24 ) ;
d24Hours.toString(): PT24H
Duration d60Minutes = Duration.ofMinutes( 60 ) ;
d60Minutes.toString(): PT1H
Call the plus
method to add those spans of time.
LocalDateTime ldtLater = ldt.plus( d60Minutes ) ;
ldtLater.toString(): 2018-01-01T01:00:40.554
LocalDateTime ldtMuchLater = ldt.plus( d24Hours ) ;
ldtMuchLater.toString(): 2018-01-02T00:00:40.554
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.