4

Is there a way to convert a Time and Date variable to a DateTime?

I have a period between two DateTime variables, for each Date in that period I want to store a period IN that Date with a begin DateTime and end DateTime, so a day can have multiple periods defined by a DateTime.

Can't seem to figure out how to combine Date and Time to a DateTime.

Thanks in advance!

Luc
  • 617
  • 3
  • 10
  • 18
  • 1
    What *exactly* do you mean by "a Time and Date variable"? It would be much easier if you would show us code rather than describing it. – Jon Skeet Mar 07 '13 at 15:56
  • java.sql.Time beginTime and java.sql.Date beginDate, I want to form a dateTime from them – Luc Mar 08 '13 at 08:41
  • Luc - give us an example of what you want to do. makes things clearer. – david blaine Mar 10 '13 at 18:22
  • Possible duplicate of [Convert from java.util.date to JodaTime](http://stackoverflow.com/questions/5042587/convert-from-java-util-date-to-jodatime) – Danielson Oct 10 '15 at 13:38

6 Answers6

11

Plain java Date and Joda-Time DateTime should serve the purpose.

Date date = new Date(); // java.util.Date; - This date has both the date and time in it already.
DateTime dateTime = new DateTime(date);

For more info about Joda-Time.

If you have two String objects, where 1 holds the Date and the other Time, you can combine the 2 Strings and use a SDF to parse it and get the Date object, which you can then convert to DateTime.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Rahul
  • 44,383
  • 11
  • 84
  • 103
  • 1
    Regarding that last paragraph… If you include a `T` between that date and time ([standard format](https://en.wikipedia.org/wiki/ISO_8601)), you can pass the string directly to a constructor of [DateTime](http://www.joda.org/joda-time/apidocs/org/joda/time/DateTime.html). No need to use the SimpleDateFormat & Date classes. Just a single line of code. Like this… `DateTime dateTime = new DateTime( "2013-01-02T03:04:05", DateTimeZone.UTC );` – Basil Bourque Dec 30 '13 at 23:25
5

Fixed it using this:

public DateTime dateAndTimeToDateTime(java.sql.Date date, java.sql.Time time) {
    String myDate = date + " " + time;
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    java.util.Date utilDate = new java.util.Date();
    try {
        utilDate = sdf.parse(myDate);
    } catch (ParseException pe){
        pe.printStackTrace();
    }
    DateTime dateTime = new DateTime(utilDate);

    return dateTime;
}
Luc
  • 617
  • 3
  • 10
  • 18
3

Even if this question is a bit older and already answered, I could not find a satisfying solution. The best method for this problem is probably the following code (without any parsing and exception handling required):

public DateTime dateAndTimeToDateTime(java.sql.Date date, java.sql.Time time) {
    DateTime t = new DateTime(time);
    return new DateTime(date).withTime(t.getHourOfDay(), t.getMinuteOfHour(), t.getSecondOfMinute(), t.getMillisOfSecond());
}
GreenTurtle
  • 1,144
  • 2
  • 21
  • 35
2

I am pretty sure that you can find how to construct date and time instances separately.

However on the datetime object itself you can specify the following.

dateTimeObject = dateTimeObject.withHourOfDay(12);
dateTimeObject = dateTimeObject.withMinuteofHour(59);
dateTimeObject = dateTimeObject.withSecondOfMinute(59);

Hope this helps!

Rahul
  • 44,383
  • 11
  • 84
  • 103
r0ast3d
  • 2,639
  • 1
  • 14
  • 18
1

java.time

With java.time, the modern Java date and time API, this is simple and straightforward:

    LocalTime time = LocalTime.of(23, 45);
    LocalDate date = LocalDate.of(2019, Month.NOVEMBER, 23);

    LocalDateTime combined = date.atTime(time);
    System.out.println(combined);

Output is:

2019-11-23T23:45

I know you asked about Joda-Time. However, the Joda-Time homepage says:

Note that Joda-Time is considered to be a largely “finished” project. No major enhancements are planned. If using Java SE 8, please migrate to java.time (JSR-310).

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
0
DateTime dateTime = new DateTime("yyyy-MM-dd");.

System.out.println("============> " + dateTime.toString());

INPUT : 2018-01-04
OUTPUT: ============> 2018-01-05T00:00:00.000+05:30

Kailash Karki
  • 2,106
  • 1
  • 12
  • 6