18

my dao page is receiving date and time from two different field now i want know how to merge these both date and time in a single object so that i calculate time difference and total time. I have this code to merge but it is not working what am i doing wrong in this code please help.

    Date d = new SimpleDateFormat("yyyy-MM-dd").parse("2013-01-02");
    Date t = new SimpleDateFormat("hh:mm:ss").parse("04:05:06");
    LocalDate datePart = new LocalDate(d);
    LocalTime timePart = new LocalTime(t);
    LocalDateTime dateTime = datePart.toLocalDateTime(timePart);
    System.out.println(dateTime);
deepak rawat
  • 270
  • 1
  • 2
  • 9
  • Duplicate of http://stackoverflow.com/questions/22463062/how-to-parse-format-dates-with-localdatetime-java-8?rq=1 – Ivan Pronin May 03 '17 at 06:25
  • 1
    Your `t` isn't actually a time. It's a date. 1st January 1970 at 4:05 am. – Boris the Spider May 03 '17 at 06:30
  • 2
    @Boris the Spider: that doesn’t stop you from treating it like a time and extract the time part, e.g. `t.toInstant().atZone(ZoneId.systemDefault()).toLocalTime()`. You have to deal with this pattern, e.g. when you encounter a [`java.sql.Time`](https://docs.oracle.com/javase/8/docs/api/?java/sql/Time.html) instance… – Holger May 03 '17 at 08:54

3 Answers3

36

You just need to use the correct methods, instead of calling constructors. Use parse to create local date and local time objects, then pass the two objects to the of method of LocalDateTime:

    LocalDate datePart = LocalDate.parse("2013-01-02");
    LocalTime timePart = LocalTime.parse("04:05:06");
    LocalDateTime dt = LocalDateTime.of(datePart, timePart);

EDIT

Apparently, you need to combine two Date objects instead of 2 strings. I guess you can first convert the two dates to strings using SimpleDateFormat. Then use the methods shown above.

String startingDate = new SimpleDateFormat("yyyy-MM-dd").format(startDate);
String startingTime = new SimpleDateFormat("hh:mm:ss").format(startTime);
Sweeper
  • 213,210
  • 22
  • 193
  • 313
  • 2
    Perfect demonstration of the simplicity of the new DateTime API. I might suggest pointing out why the above it's working - i.e. that `t` is actually a date and there is no support for "time" pre Java 8. – Boris the Spider May 03 '17 at 06:31
  • its throwing error incompatible type string cannot converted to LocalDate – deepak rawat May 03 '17 at 06:40
  • 1
    Are you sure you are using methods like `parse` and `of` and not directly assigning a string to `datePart`? @deepakrawat – Sweeper May 03 '17 at 06:42
  • String startingDate = this.startDate; String startingTime = this.startTime; LocalDateTime dt = LocalDateTime.of(startingDate, startingTime); – deepak rawat May 03 '17 at 06:44
  • i am hoding date and time in a string then merge it in a object. – deepak rawat May 03 '17 at 06:46
  • 1
    @deepak rawat: the beginning of this answer shows how to convert a `String` into a `LocalDate` resp. `LocalTime`. – Holger May 03 '17 at 08:51
  • 2
    Its important to note that this require api 26 and higher – Mustafa Ibrahim Feb 24 '21 at 09:26
  • It's require API 26 and higher, so what about 21+? – Konstantin Konopko Jul 21 '21 at 16:49
  • @KonstantinKonopko You _can_ use this without requiring API 26 if you use Android Gradle Plugin 4.0.0. [There is now desugaring support.](https://developer.android.com/studio/write/java8-support#library-desugaring) Otherwise, [there is also a backport](https://github.com/JakeWharton/ThreeTenABP). – Sweeper Jul 22 '21 at 00:30
5

To combine date and time in java 8 you can use java.time.LocalDateTime. This also allows you to format with java.time.format.DateTimeFormatter.

Example program:

public static void main(String[] args) {
        LocalDate date = LocalDate.of(2013, 1, 2);
        LocalTime time = LocalTime.of(4, 5, 6);
        LocalDateTime localDateTime = LocalDateTime.of(date, time);
        DateTimeFormatter format = DateTimeFormatter.ofPattern("MMM d yyyy  hh:mm a");
        System.out.println(localDateTime.format(format));
    }
Jay Smith
  • 2,331
  • 3
  • 16
  • 27
  • 1
    I got a warning here. Call requires API level 26 (current min is 15): java.time.LocalDate#of – DevOma Mar 01 '18 at 14:22
3

Simple yet effective would be:

LocalDateTime dateTime = LocalDateTime.of(datePart, timePart);
Eric Smith
  • 31
  • 1
  • 5