10

I have a LocalDate and a LocalTime and would like to simply create a LocalDateTime struct from them. I thought of the following extension method which I believe would be the fastest but for an obscure reason the field localTime.TickOfMillisecond does not exist in the current version of the API. So it does not work.

    public static LocalDateTime At(this LocalDate localDate, LocalTime localTime) {
        return new LocalDateTime(localDate.Year, localDate.Month, localDate.Day, localTime.Hour, localTime.Minute, localTime.Second, localTime.Millisecond, localTime.TickOfMillisecond);
    }

So, am I stuck in the mean time to use:

    public static LocalDateTime At(this LocalDate localDate, LocalTime localTime) {
        return localDate.AtMidnight().PlusTicks(localTime.TickOfDay);
    }

Any advice is appreciated.

Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
Erwin Mayer
  • 18,076
  • 9
  • 88
  • 126

3 Answers3

13

It's much easier than the way you've got it at the moment - simply use the + operator:

LocalDate date = ...;
LocalTime time = ...;
LocalDateTime dateTime = date + time;
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Great! Thanks. For discoverability purposes, it could be worth including an equivalent method (especially as there are others for similar tasks). – Erwin Mayer Feb 11 '13 at 19:29
  • @ErwinMayer: Indeed. I'll think about that... I'm not sure whether it needs to be symmetric though: `LocalDate.At(LocalTime)` and `LocalTime.On(LocalDate)`? `AtTime` and `OnDate` instead of just `At` and `On`? Will ponder. – Jon Skeet Feb 11 '13 at 19:33
  • I'd tend to favor LocalDate.At(LocalTime) if one of the symetric alternatives has to be chosen, as it follows the time hierarchy used elsewhere (year, month, day, hour, minute, seconds...). For code readability however LocalTime.On(LocalDate) also makes sense and could be nice to have in use cases where time is the primary information (vs. the date). – Erwin Mayer Feb 11 '13 at 19:42
  • JSR-310 uses localDate.atTime(localTime) – JodaStephen Mar 12 '13 at 14:27
  • @JodaStephen: Thanks for that. Do you have an equivalent for `LocalTime.onDate(LocalDate)`? I'm not sure it's necessary to be symmetric *everywhere*, but... – Jon Skeet Mar 12 '13 at 14:28
  • localTime.atDate(localDate). Its always atXxx(). See http://threeten.github.com/threetenbp/apidocs/ – JodaStephen Mar 13 '13 at 07:48
  • It seems the `+` operator is not defined like this for Joda Time. Is there an easy way to do this same thing using Joda Time? At the moment I'm using `localDateTime = new LocalDateTime().withFields(localDate).withFields(localTime);` – FGreg May 02 '13 at 21:12
  • 1
    @FGreg: Use `LocalDate.toLocalDateTime(LocalTime)`. – Jon Skeet May 02 '13 at 21:16
  • LocalDateDate.FromLocalDateAndLocalTime(LocalDate, LocalTime) would be another one option. Also similar to whole other breed of FromXyz() methods. – Stepan Stulov Jul 15 '19 at 08:36
4

There is method available to combine LocalDate and LocalTime to form LocalDateTime

LocalDate date = ...;
LocalTime time = ...;
LocalDateTime dateTime=date.atTime(time);
Mahesh Kshirsagar
  • 390
  • 1
  • 3
  • 12
2

Well, I'm not sure why it's not in the API. Maybe Jon Skeet can answer that.

I don't see anything wrong with the way you went about it in your second example, but you could calculate the tickOfMillisecond like this:

public static LocalDateTime At(this LocalDate localDate, LocalTime localTime)
{
    var tickOfMillisecond = localTime.TickOfSecond - localTime.Millisecond * 10000;
    return new LocalDateTime(localDate.Year, localDate.Month, localDate.Day, localTime.Hour, localTime.Minute, localTime.Second, localTime.Millisecond, tickOfMillisecond);
}

Personally, I think there should be a constructor for LocalDateTime so you can do this instead:

var ldt = new LocalDateTime(localDate, localTime);

One other thing - perhaps your extension method should validate the date and time are both in the same calendar system, and then pass that through to the result? I've never used any calendar but ISO, so I'm not sure on that.

Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
  • Thanks for the tip. Do you know if it TickOfMillisecond is missing on purpose or was it just somehow forgotten in the current release? I don't see any Calendar property in LocalTime... – Erwin Mayer Feb 09 '13 at 03:51
  • I really don't know. Sorry. – Matt Johnson-Pint Feb 09 '13 at 23:46
  • @ErwinMayer: The whole TickOfMillisecond vs TickOfSecond difference is slightly annoying, to be honest. `TickOfSecond` is a more sensible value in general... but fortunately there's a better way to do what you want anyway, as per my ansewr :) – Jon Skeet Feb 11 '13 at 16:53