1

I have two type dates that I get from my database :

Date : 2017-01-28 || Time : 12:59

And I want to combine it into a one Date variable.

this is the origin of the two variables :

@Temporal(value = TemporalType.DATE)
@Future
@DateTimeFormat(pattern = "dd/MM/YY")
@Column(name = "dateDebut", nullable = true)
private Date date;

@Temporal(TemporalType.TIME)
@Column(name="Start_Hour")
private Date startHour;

any help will be appreciated.Thank you.

Oddkills
  • 67
  • 1
  • 8
  • 1
    *FYI:* Your `DateTimeFormat` pattern is wrong. Change `YY` to `yy`. – Andreas Jan 19 '17 at 23:16
  • 1
    FYI, the troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/10/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/10/docs/api/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [*java.time*](https://docs.oracle.com/javase/10/docs/api/java/time/package-summary.html) classes built into Java 8 and later. See [*Tutorial* by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Apr 08 '18 at 22:58

3 Answers3

5

Use a Calendar object:

private static Date combine(Date date, Date time) {
    Calendar cal = Calendar.getInstance();
    cal.setTime(time);
    int hour = cal.get(Calendar.HOUR_OF_DAY);
    int min = cal.get(Calendar.MINUTE);
    cal.setTime(date);
    cal.set(Calendar.HOUR_OF_DAY, hour);
    cal.set(Calendar.MINUTE, min);
    return cal.getTime();
}

Test

Date date = new SimpleDateFormat("yyyy-MM-dd").parse("2017-01-28");
Date startHour = new SimpleDateFormat("HH:mm").parse("12:59");
System.out.println(combine(date, startHour));

Output

Sat Jan 28 12:59:00 EST 2017
Andreas
  • 154,647
  • 11
  • 152
  • 247
4

Now you need to do some conversions of your date and hour, you may as well convert them to one of the Java 8 date and time classes — provided you can use Java 8, of course. These classes are much nicer to work with downstream than the old-fashioned Date class. It’s even more straightforward than the other answers. For example:

    LocalDate d = date.toInstant().atZone(ZoneOffset.systemDefault()).toLocalDate();
    LocalTime t = startHour.toInstant().atZone(ZoneOffset.systemDefault()).toLocalTime();
    LocalDateTime dt = d.atTime(t);
    System.out.println(dt);

This prints:

2017-01-28T12:29

Depending on you requirements, it may be that you’ll prefer to stay with a ZonedDateTime or some other Java 8 type. These classes are quite versatile, so chances are that you can get what you want with few lines of code.

Edit: Some JPA implementations may support the Java 8 date and time classes directly, so you may spare the first two lines and only need the third. See JPA support for Java 8 new date and time API.

Community
  • 1
  • 1
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
0

I got the idea you wanted to parse a date from that String (as I'm not sure what do you mean by that origin format).

So maybe this will help:

private static final String STRING_TO_FORMAT = "Date : 2017-01-28 || Time : 12:59";

public static void main(String[] args) {
    Matcher matcher = Pattern.compile(".*(\\d{4}-\\d{2}-\\d{2}).*(\\d{2}:\\d{2})").matcher(STRING_TO_FORMAT);
    matcher.find();

    Date date = null;
    try {
        date = new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(matcher.group(1) + " " + matcher.group(2));
        System.out.println(date);
    } catch (ParseException e) {
    }

    SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yy HH:mm");
    System.out.println(formatter.format(date));
}
SlumpA
  • 882
  • 12
  • 24