-1

I have a time variable startDate:

startDate = 2018-01-01 00:00:40.554

And I want to calculate endDay which is 24 hours from the startDate, and endHour which is 60 minutes from the startDate.

dayDiff = Days.daysBetween(startDate.toLocalDate(), 24hours.toLocalDate()).getDays()

So basically:

startDate + 1day = endDay
startDate + 60mins = endHour
Neuron
  • 5,141
  • 5
  • 38
  • 59
adu
  • 17
  • 3
  • 1
    I'm finding it very difficult to understand your question - your description sounds like you want to just compute `endDay`, but your sample *uses* `endDay`. Please take some time to make your question as clear as possible. – Jon Skeet Mar 07 '18 at 17:44
  • What you are look for is "how to add a time to a date in java?". Once you will find a quick guide, you should find the answer pretty fast since you need a dqte like "startDate + 1day" and "startDate + 1hour". But your question is presenting the opposite "how to get the difference between two dates". – AxelH Mar 07 '18 at 17:47
  • Yes..thats exactly what I meant – adu Mar 07 '18 at 17:49
  • 2
    @adu have you simply consulted the javadoc of the classes you're using? The answer is in there, and it's pretty obvious. http://joda-time.sourceforge.net/apidocs/org/joda/time/DateTime.html – JB Nizet Mar 07 '18 at 17:51
  • Possible duplicate of [Add one day into Joda-Time DateTime](https://stackoverflow.com/questions/16461361/add-one-day-into-joda-time-datetime) – Ole V.V. Mar 07 '18 at 20:53
  • Also related: [Problem adding minutes with plusMinutes](https://stackoverflow.com/questions/4540144/problem-adding-minutes-with-plusminutes). – Ole V.V. Mar 07 '18 at 20:55
  • 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 Mar 07 '18 at 22:03

2 Answers2

1

I assume you're using Joda-Time, because you used Days.daysBetween and I don't know another API with such class and method.

Anyway, Days.daysBetween calculates the difference between 2 date/times (a number that represents how many days there are between those dates), but what you really need is to add a duration (1 day or 1 hour) to a date.

Assuming that startDate is a DateTime, you can do:

DateTime endDay = startDate.plusHours(24);
DateTime endHour = startDate.plusMinutes(60);

If the type of startDate is another one (such as LocalDateTime), don't worry, all these core classes have similar methods as well.

I called plusHours(24), but I could also use plusDays(1). Just remind that, as noted in the comments, adding 1 day is not always the same as adding 24 hours, due to Daylight Saving Time issues (more about DST issues can be found here).

sbr1t
  • 90
  • 4
1

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.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154